Class: Admin::BannersController
Overview
Banner is used by Restaurant Tag and Restaurant Tag Group so if we want to manage Restaurant Tag's Banner or Restaurant Tag Group's Banner, we can pass restaurant_tag_id or restaurant_tag_group_id param to filter banners based on given ID
Constant Summary
Admin::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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'app/controllers/admin/banners_controller.rb', line 51
def create
@banner = Banner.new permitted_params
@banner.restaurant_tag_group_id = restaurant_tag_group_id_param
@banner.restaurant_tag_id = restaurant_tag_id_param
@banner.fix_attributes
if banner.valid? && banner.save
redirect_to admin_banners_path(restaurant_tag_id: restaurant_tag_id_param,
restaurant_tag_group_id: restaurant_tag_group_id_param),
notice: 'Banner created successfully'
else
flash[:alert] = banner.errors.full_messages.to_sentence
render :new
end
end
|
#destroy ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'app/controllers/admin/banners_controller.rb', line 104
def destroy
if banner.destroy
flash[:notice] = "#{banner.name} deleted successfully"
else
flash[:alert] = banner.errors.full_messages.to_sentence
end
redirect_back fallback_location: back_fallback_location
end
|
#destroy_image ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'app/controllers/admin/banners_controller.rb', line 114
def destroy_image
banner = Banner.find params.require(:banner_id)
index = params.require(:index).to_i
%i[mobile_versions desktop_versions mobile_retina_versions desktop_retina_versions].each do |method|
if index.zero? && banner.send(method).one?
banner.send("remove_#{method}!".to_sym)
else
remain_images = banner.send(method).dup
deleted_image = remain_images.delete_at(index)
deleted_image.try(:remove!)
banner.send("#{method}=".to_sym, remain_images)
end
end
if banner.save
render json: {
message: 'Image deleted successfully',
}
else
render json: {
message: banner.errors.full_messages.to_sentence,
}, status: :unprocessable_entity
end
end
|
#edit ⇒ Object
66
67
68
69
70
71
72
73
74
75
|
# File 'app/controllers/admin/banners_controller.rb', line 66
def edit
@banner = @banner.as_json.merge(
name_en: banner.name_en,
name_th: banner.name_th,
description_en: banner.description_en,
description_th: banner.description_th,
banner_cities_attributes: banner.banner_cities,
home_section: banner.home_section,
)
end
|
#index ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'app/controllers/admin/banners_controller.rb', line 12
def index
sort = params[:sort]
order = params[:order] || 'asc'
@banners = Banner.includes(:restaurant_tag, :restaurant_tag_group).
order(order_clause(sort, order)).
page(params[:page]).
per(10)
if restaurant_tag_id_param.present?
@banners = @banners.where(restaurant_tag_id: restaurant_tag_id_param)
end
etag = CityHash.hash32([self.class.to_s, @banners.cache_key])
return unless stale?(etag: etag)
end
|
#new ⇒ Object
44
45
46
47
48
49
|
# File 'app/controllers/admin/banners_controller.rb', line 44
def new
@banner = Banner.new
banner.restaurant_tag_group_id = restaurant_tag_group_id_param
banner.restaurant_tag_id = restaurant_tag_id_param
banner.fix_attributes
end
|
#order_clause(sort, order) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'app/controllers/admin/banners_controller.rb', line 29
def order_clause(sort, order)
case sort
when 'id'
"id #{order.upcase}"
when 'name'
"name #{order.upcase}"
when 'active'
"active #{order.upcase}"
when 'timestamp'
"created_at #{order.upcase}"
else
'order_number ASC' end
end
|
#update ⇒ Object
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
|
# File 'app/controllers/admin/banners_controller.rb', line 77
def update
banner.attributes = permitted_params
if banner.valid? && banner.save
if banner.home_section.present?
redirect_to admin_restaurant_sections_path, notice: 'Banner updated successfully'
else
redirect_to admin_banners_path(restaurant_tag_id: banner.restaurant_tag_id,
restaurant_tag_group_id: banner.restaurant_tag_group_id),
notice: 'Banner updated successfully'
end
else
flash[:alert] = banner.errors.full_messages.to_sentence
render :edit
end
end
|