Class: Admin::RestaurantSectionsController

Inherits:
BaseController show all
Defined in:
app/controllers/admin/restaurant_sections_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

Instance Method Summary collapse

Methods inherited from BaseController

#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from AdminHelper

#dynamic_pricings_formatter, #link_to_admin_reservations_path_by_id, #link_to_admin_restaurants_path_by_id, #link_to_log, #optional_locales, #optional_locales_with_labels, #staff_signed_in?

Methods included from UpdateLocaleConcern

#setup_locale

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Method Details

#activationObject



124
125
126
127
128
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 124

def activation
  @restaurant_section.active_mobile = !@restaurant_section.active_mobile if params[:setting_for] == 'mobile'
  @restaurant_section.active_web = !@restaurant_section.active_web if params[:setting_for].blank? || params[:setting_for] == 'web'
  @restaurant_section.save
end

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 32

def create
  @section_templates = HomeSection.section_templates(:unfixed)
  ActiveRecord::Base.transaction do
    @restaurant_section = HomeSection.new restaurant_section_params
    @restaurant_section.section_type = 'custom_section'
    @restaurant_section.fixed_type = false
    if @restaurant_section.save
      if HomeSection.section_templates(:outlet_sortable).keys.include?(@restaurant_section.section_template.to_sym)
        @restaurant_section.update_compact_restaurants_rank params[:compact_restaurants]&.map(&:permit!)
      end
      redirect_to edit_admin_restaurant_section_path(@restaurant_section.id), notice: 'Data updated'
    else
      flash[:error] = @restaurant_section.errors.full_messages.to_sentence
      render :new
    end
  end
end

#delete_restaurant_rankObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 110

def delete_restaurant_rank
  compact_restaurant_id = params[:cr_id]
  cr = @restaurant_section.compact_restaurants.find(compact_restaurant_id)

  if cr.update("rank_#{@restaurant_section.section_type}": nil) # remove rank section in compact restaurant
    Tagging::RemoveTagSpecialSectionWorker.perform_async cr.id

    redirect_to edit_admin_restaurant_section_path(@restaurant_section.id), notice: 'Data updated'
  else
    flash[:error] = cr.errors.full_messages.to_sentence
    redirect_to edit_admin_restaurant_section_path(@restaurant_section.id)
  end
end

#delete_sectionObject



103
104
105
106
107
108
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 103

def delete_section
  if request.post?
    @restaurant_section.destroy
    redirect_to admin_restaurant_sections_path, notice: 'Data deleted'
  end
end

#destroyObject



98
99
100
101
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 98

def destroy
  @restaurant_section.destroy
  redirect_to admin_restaurant_sections_path, notice: 'Data deleted'
end

#duplicateObject



151
152
153
154
155
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 151

def duplicate
  @new_restaurant_section = @restaurant_section.dup
  @new_restaurant_section.save
  redirect_to edit_admin_restaurant_section_path(@new_restaurant_section.id), notice: 'Data duplicated'
end

#editObject



67
68
69
70
71
72
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 67

def edit
  @compact_restaurants = @restaurant_section.compact_restaurants.
    order("rank_#{@restaurant_section.section_type}": :asc)
  @section_type = @restaurant_section.section_type
  @section_templates = HomeSection.section_templates(:unfixed)
end

#indexObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 11

def index
  set_meta_tags title: 'Homepage Setting'
  device_position = if params[:setting_for] == 'mobile'
                      'position_mobile'
                    else
                      'position_web'
                    end

  @grid = ::Admin::RestaurantSectionsGrid.new(params[:admin_restaurant_sections_grid]) do |scope|
    scope.order("CASE WHEN section_type = 'section_banner' THEN 0 ELSE 1 END, #{device_position} ASC").page(params[:page]).per(50)
  end
  @tbody_id = 'sortable-list'
  @custom_cache_key = params[:setting_for]
  fresh_when @grid.assets.cache_key
end

#newObject



27
28
29
30
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 27

def new
  @restaurant_section = HomeSection.new
  @section_templates = HomeSection.section_templates(:unfixed)
end

#showObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 50

def show
  set_meta_tags title: 'Compact Restaurant List'

  restaurant_section = RestaurantSection.new
  restaurant_section.section_number(@restaurant_section.section_type)

  filter = CompactRestaurantsFilter.new
  filter.sort_by(restaurant_section.sort_by.to_sym)
  @compact_restaurants = filter.collections.uniq

  @grid = ::Admin::RestaurantSectionRestaurantsGrid.new(params[:admin_restaurant_sections_grid]) do |scope|
    scope = scope.find_by_sql(filter.collections.to_sql)

    Kaminari.paginate_array(scope).page(params[:page]).per(50)
  end
end

#sortObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 130

def sort
  sorted_ids = params[:sorted_ids]
  setting_for = params[:setting_for]

  # Update the position attribute for each section based on the sorted order
  sorted_ids.each_with_index do |id, index|
    if setting_for == 'mobile'
      h = HomeSection.find(id)
      h.update_column(:position_mobile, index + 1)
      h.touch
    else
      h = HomeSection.find(id)
      h.update_column(:position_web, index + 1)
      h.touch
    end
  end
  HomeSection.trigger_build_home

  render json: { status: 'success' }
end

#tagsObject



157
158
159
160
161
162
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 157

def tags
  I18n.locale = params[:locale].to_s.to_sym.presence || :th
  tag_category = RestaurantTag::CATEGORY_FILTER.invert[params[:tag_category].to_s]
  @tags = RestaurantTag.tags_like(tag_category)
  render json: @tags.map { |t| { id: t.id, title: t.title_format } }
end

#updateObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/admin/restaurant_sections_controller.rb', line 74

def update
  ActiveRecord::Base.transaction do
    if @restaurant_section.update restaurant_section_params
      @restaurant_section.update_compact_restaurants_rank params[:compact_restaurants]&.map(&:permit!)
      respond_to do |format|
        format.json { render json: { success: true, message: 'success' } }
        format.html do
          redirect_to edit_admin_restaurant_section_path(@restaurant_section.id), notice: 'Data updated'
        end
      end
    else
      respond_to do |format|
        format.json do
          render json: { success: false, message: @restaurant_section.errors.full_messages.to_sentence }
        end
        format.html do
          flash[:error] = @restaurant_section.errors.full_messages.to_sentence
          redirect_to edit_admin_restaurant_section_path(@restaurant_section.id)
        end
      end
    end
  end
end