Class: Admin::Packages::MenusController

Inherits:
BaseController show all
Includes:
AdminPackageConcern
Defined in:
app/controllers/admin/packages/menus_controller.rb

Overview

Responsible to manage package's menus

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

#createObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/admin/packages/menus_controller.rb', line 143

def create
  permitted_params = params.require(:hh_package_package_menu).permit(:package_id, :package_type, image: [])

  payloads = permitted_params.fetch(:image, []).map do |p|
    {
      package_id: permitted_params[:package_id],
      package_type: permitted_params[:package_type],
      image: p,
    }
  end
  operation = HhPackage::PackageMenu.create payloads
  if operation.first&.id&.present?
    flash[:notice] = 'Success'
  else
    flash[:alert] = operation.first&.errors&.full_messages&.to_sentence || 'something went wrong'
  end
  redirect_back fallback_location: back_fallback_location
end

#create_for_sectionObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/admin/packages/menus_controller.rb', line 30

def create_for_section
  operation = HhPackage::PackageMenu.new image: params.require(:file), section_id: params.require(:section_id)
  if operation.valid? && operation.save
    if operation.image.height != 1000 || operation.image.width != 1000
      operation.image.recreate_versions!
      operation.reload
    end
    render json: Admin::Packages::MenuSerializer.new(operation).as_json
  else
    render json: { message: operation.errors.full_messages.to_sentence }
  end
end

#destroyObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/admin/packages/menus_controller.rb', line 170

def destroy
  if params[:id] == 'all'
    package.package_menus.destroy_all
  else
    package_menu = HhPackage::PackageMenu.find_by(id: params.require(:id))
    if package_menu.blank?
      return render json: { message: 'Menu not found' }, status: :not_found
    end

    if package_menu.destroy
      render json: package_menu
    else
      render json: { message: package_menu.errors.full_messages.to_sentence }, status: :unprocessable_entity
    end
  end
end

#importObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/admin/packages/menus_controller.rb', line 68

def import
  file = params.require(:import)
  package_type = params.require(:package_type)
  package_id = params.require(:package_id)
  package = "HhPackage::Package::#{HhPackage::PACKAGE_SHORT_TO_LIST[package_type.to_sym]}".constantize.find(package_id)

  @section_id = nil
  errors = nil
  ActiveRecord::Base.transaction do
    section_rank = 0
    menu_rank = {}
    CSV.foreach(file.tempfile,
                force_quotes: true, encoding: Encoding::UTF_8,
                headers: false, col_sep: "\t", liberal_parsing: true) do |row|
      row_section = row[0]

      if row_section.present?
        section_rank += 1
        payload = {
          name_en: row[0],
          name_th: row[1],
          quantity_limit: row[3].to_i,
          priority: section_rank,
          package: package,
        }
        section = HhPackage::Package::MenuSection.new(payload)
        if section.save
          @section_id = section.id
        else
          errors = section.errors.full_messages
          raise ActiveRecord::Rollback
        end
      elsif row_section.blank? && row[1].present? # create menu section
        rank = menu_rank[@section_id].presence || 0
        rank += 1
        payload = {
          name_en: row[1],
          name_th: row[2],
          index_number: rank,
          package: package,
          remote_image_url: row[8].to_s.presence || nil,
          section_id: @section_id,
          price_cents: row[4].to_i * 100,
          price_currency: 'THB',
          description_en: row[5],
          description_th: row[6],
          active: true,
        }
        menu_rank[@section_id] = rank
        package_menu = HhPackage::PackageMenu.new(payload)
        unless package_menu.save
          errors = package_menu.errors.full_messages
          raise ActiveRecord::Rollback
        end
      end
    end
  end
  if errors.blank?
    flash[:notice] = 'Import Success'
  else
    flash[:alert] = errors&.to_sentence || 'something went wrong'
  end
  redirect_back fallback_location: back_fallback_location
end

#indexObject



10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/admin/packages/menus_controller.rb', line 10

def index
  respond_to do |format|
    format.html do
      @package_menu = package.package_menus.build
    end
    format.json do
      render json: package.package_menus.order_by_rank
    end
  end
end

#single_menuObject



21
22
23
24
25
26
27
28
# File 'app/controllers/admin/packages/menus_controller.rb', line 21

def single_menu
  package_menu = HhPackage::PackageMenu.find(params.require(:menu_id))
  json = package_menu.attributes
  json['sections_attributes'] = package_menu.subsections.order_by_rank.map do |menu_section|
    Admin::Packages::MenuSectionSerializer.new(menu_section).as_json
  end
  render json: json
end

#updateObject



162
163
164
165
166
167
168
# File 'app/controllers/admin/packages/menus_controller.rb', line 162

def update
  if package_menu.update params.require(:package_menu).permit(:index_number)
    render json: package_menu
  else
    render json: { message: package_menu.errors.full_messages.to_sentence }, status: :unprocessable_entity
  end
end

#update_imageObject



133
134
135
136
137
138
139
140
141
# File 'app/controllers/admin/packages/menus_controller.rb', line 133

def update_image
  operation = HhPackage::PackageMenu.find(params.require(:menu_id))
  operation.image = params.require(:file)
  if operation.valid? && operation.save
    render json: { success: true }
  else
    render json: { success: false, message: operation.errors.full_messages.to_sentence }
  end
end

#update_sub_sectionObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/admin/packages/menus_controller.rb', line 43

def update_sub_section
  package_menu = HhPackage::PackageMenu.find params.require(:menu).require(:package_menu_id)
  section_menus = ActiveSupport::JSON.decode params.require(:menu)[:section]

  section_menus.map do |sm|
    sm.tap do |h|
      h['quantity_limit'] = 1
    end
  end

  success = true
  ActiveRecord::Base.transaction do
    package_menu.subsections_attributes = section_menus
    unless package_menu.save
      raise ActiveRecord::Rollback, package_menu.errors.full_messages
      success = false
    end
  end
  if success
    render json: { success: true }
  else
    render json: { success: false, message: errors }
  end
end