Class: Admin::SearchIconsController
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'app/controllers/admin/search_icons_controller.rb', line 35
def create
@search_icon = SearchIcon.new(search_icon_params)
if SearchIcon.find_by(restaurant_tag_id: params[:search_icon][:restaurant_tag_id]).present? &&
params[:search_icon][:icon_type] == 'tag_base'
return render json: { success: false, message: 'Tag already exist' }
end
city_ids_params = params[:search_icon][:city_ids]
new_city_ids = city_ids_params.split(',').map(&:strip).reject(&:empty?).map(&:to_i)
result = false
ActiveRecord::Base.transaction do
@search_icon.save!
restaurant_tag = RestaurantTag.find_by(id: @search_icon.restaurant_tag_id)
if restaurant_tag.present? && restaurant_tag.city_id.present?
@search_icon.search_icons_cities.create!(city_id: restaurant_tag.city_id)
end
if new_city_ids.present? && @search_icon.allowed_to_edit_city?
new_city_ids.each do |city_id|
@search_icon.search_icons_cities.create!(city_id: city_id)
end
end
result = true
rescue StandardError => e
@search_icon.errors.add(:base, e.message)
raise ActiveRecord::Rollback
end
respond_to do |format|
if result
format.html { redirect_to admin_search_icons_path }
format.json { render json: { success: true, message: 'Search icon created successfully' } }
else
set_data_tags
format.html { render :new }
format.json do
render json: {
success: false,
errors: @search_icon.errors.full_messages,
}, status: :unprocessable_entity
end
end
end
end
|
#destroy ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'app/controllers/admin/search_icons_controller.rb', line 147
def destroy
result = false
ActiveRecord::Base.transaction do
@search_icon.destroy!
SearchIconsCity.where(search_icon_id: @search_icon.id).delete_all
result = true
rescue StandardError => e
@search_icon.errors.add(:base, e.message)
raise ActiveRecord::Rollback
end
respond_to do |format|
if result
format.json do
render json: { success: true, message: 'Search icon destroyed successfully' }
end
else
format.json do
render json: {
success: false,
errors: @search_icon.errors.full_messages,
}, status: :unprocessable_entity
end
end
end
end
|
#edit ⇒ Object
88
89
90
|
# File 'app/controllers/admin/search_icons_controller.rb', line 88
def edit
set_data_tags
end
|
#index ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'app/controllers/admin/search_icons_controller.rb', line 8
def index
@search_icon_settings = SearchIcon.order(position: :asc).
page(params[:page] || 1).
per(params[:per_page] || 50)
respond_to do |format|
format.html do
set_meta_tags title: 'Search Icon Setting'
render
end
format.json do
render json: @search_icon_settings,
meta: {
total_count: SearchIcon.count,
},
each_serializer: SearchIconSettingSerializer,
adapter: :json
end
end
end
|
#new ⇒ Object
30
31
32
33
|
# File 'app/controllers/admin/search_icons_controller.rb', line 30
def new
@search_icon = SearchIcon.new
set_data_tags
end
|
#show ⇒ Object
84
85
86
|
# File 'app/controllers/admin/search_icons_controller.rb', line 84
def show
redirect_to admin_search_icons_path
end
|
#update ⇒ Object
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'app/controllers/admin/search_icons_controller.rb', line 92
def update
if SearchIcon.find_by(restaurant_tag_id: params[:search_icon][:restaurant_tag_id]).present? &&
params[:search_icon][:icon_type] == 'tag_base' &&
params[:search_icon][:restaurant_tag_id]&.to_i != @search_icon.restaurant_tag_id&.to_i
return render json: { success: false, message: 'Tag already exist' }
end
city_ids_params = params[:search_icon][:city_ids]
new_city_ids = city_ids_params.split(',').map(&:strip).reject(&:empty?).map(&:to_i)
current_city_ids = @search_icon.city_ids
remove_city_ids = current_city_ids - new_city_ids
add_city_ids = new_city_ids - current_city_ids
result = false
ActiveRecord::Base.transaction do
@search_icon.update!(search_icon_params)
restaurant_tag = RestaurantTag.find_by(id: @search_icon.restaurant_tag_id)
if restaurant_tag.present? && restaurant_tag.city_id.present?
@search_icon.search_icons_cities.find_or_create_by!(city_id: restaurant_tag.city_id)
end
if add_city_ids.present? && @search_icon.allowed_to_edit_city?
add_city_ids.each do |city_id|
@search_icon.search_icons_cities.create!(city_id: city_id)
end
end
if remove_city_ids.present?
SearchIconsCity.where(city_id: remove_city_ids, search_icon_id: @search_icon.id).delete_all
end
result = true
rescue StandardError => e
@search_icon.errors.add(:base, e.message)
raise ActiveRecord::Rollback
end
respond_to do |format|
if result
format.json do
render json: { success: true, message: 'Search icon data updated successfully' }
end
else
format.json do
render json: {
success: false,
errors: @search_icon.errors.full_messages,
}, status: :unprocessable_entity
end
end
end
end
|
#update_cities ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'app/controllers/admin/search_icons_controller.rb', line 191
def update_cities
city_ids_param = params.require(:city_ids)
city_ids = city_ids_param.map(&:to_i).reject { |id| id == 0 }
current_city_ids = @search_icon.city_ids
new_city_ids = city_ids - current_city_ids
remove_city_ids = current_city_ids - city_ids
unless @search_icon.allowed_to_edit_city?
return render json: {
success: false,
message: 'Cannot edit the city for this search icon because it is tag-based with a predefined city.
Please save your changes first before managing the city',
},
status: :unprocessable_entity
end
if new_city_ids.present?
new_city_ids.each do |city_id|
@search_icon.search_icons_cities.create(city_id: city_id)
end
end
if remove_city_ids.present?
SearchIconsCity.where(city_id: remove_city_ids, search_icon_id: @search_icon.id).delete_all
end
render json: { success: true, message: 'Cities updated successfully' }
rescue ActionController::ParameterMissing => e
render json: { success: false, message: e.message }, status: :unprocessable_entity
rescue StandardError => e
render json: { success: false, message: e.message }, status: :internal_server_error
end
|
#update_position ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'app/controllers/admin/search_icons_controller.rb', line 174
def update_position
positions = SearchIcon.all
positions.each do |position|
params[:update_position].each do |update_position|
if position.id == update_position[:id]
position.position = update_position[:position]
position.save
end
end
end
respond_to do |format|
format.json do
render json: { success: true, message: 'Position updated successfully' }
end
end
end
|