Class: Admin::Packages::MenusController
Overview
Responsible to manage package's menus
Constant Summary
BaseController::INTERNAL_SERVER_ERROR_MESSAGE
Instance Method Summary
collapse
#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session
#append_info_to_payload
#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?
#setup_locale
#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=
#check_boolean_param, #get_banners, #inventory_params, #reservation_params
Instance Method Details
#create ⇒ Object
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_section ⇒ Object
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
|
#destroy ⇒ Object
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..destroy_all
else
= HhPackage::PackageMenu.find_by(id: params.require(:id))
if .blank?
return render json: { message: 'Menu not found' }, status: :not_found
end
if .destroy
render json:
else
render json: { message: .errors.full_messages.to_sentence }, status: :unprocessable_entity
end
end
end
|
#import ⇒ Object
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
= {}
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? 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,
}
[@section_id] = rank
= HhPackage::PackageMenu.new(payload)
unless .save
errors = .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
|
#index ⇒ Object
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..build
end
format.json do
render json: package..order_by_rank
end
end
end
|
21
22
23
24
25
26
27
28
|
# File 'app/controllers/admin/packages/menus_controller.rb', line 21
def
= HhPackage::PackageMenu.find(params.require(:menu_id))
json = .attributes
json['sections_attributes'] = .subsections.order_by_rank.map do ||
Admin::Packages::MenuSectionSerializer.new().as_json
end
render json: json
end
|
#update ⇒ Object
162
163
164
165
166
167
168
|
# File 'app/controllers/admin/packages/menus_controller.rb', line 162
def update
if .update params.require(:package_menu).permit(:index_number)
render json:
else
render json: { message: .errors.full_messages.to_sentence }, status: :unprocessable_entity
end
end
|
#update_image ⇒ Object
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_section ⇒ Object
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
= HhPackage::PackageMenu.find params.require(:menu).require(:package_menu_id)
= ActiveSupport::JSON.decode params.require(:menu)[:section]
.map do |sm|
sm.tap do |h|
h['quantity_limit'] = 1
end
end
success = true
ActiveRecord::Base.transaction do
.subsections_attributes =
unless .save
raise ActiveRecord::Rollback, .errors.full_messages
success = false
end
end
if success
render json: { success: true }
else
render json: { success: false, message: errors }
end
end
|