Class: RestaurantTag
Overview
RestaurantTag has relation to the Restaurant model through two models
-
PrimaryTag
-
RestaurantTagsRestaurant
PrimaryTag is the primary tag RestaurantTagsRestaurant is the secondary tag as we mentioned it in some issues like
Primary and Secondary tag has distance column
-
PrimaryTag, column name distance_to_restaurant
-
RestaurantTagsRestaurant, column name distance_to_restaurant
Constant Summary
collapse
- REQUIRED_LOCALES =
Required locales for restaurant tags
%i[en th cn].freeze
- HASHTAGS =
'Hashtags'
- CUISINE =
'Cuisine'
- POPULAR_ZONE =
'PopularZone'
- LOCATION =
'Location'
- BTS_ROUTE =
'BtsRoute'
- MRT_ROUTE =
'MrtRoute'
- SHOPPING_MALL =
'ShoppingMall'
- DINING_STYLE =
'DiningStyle'
- FACILITY =
'Facility'
- AWARD_BADGE =
'AwardBadge'
- AWARD_TYPE =
'AwardType'
- LOCATION_CATEGORIES =
[
POPULAR_ZONE,
LOCATION,
BTS_ROUTE,
MRT_ROUTE,
SHOPPING_MALL,
].freeze
- CATEGORY_FILTER =
{
'BtsRoute' => 'BTS',
'MrtRoute' => 'MRT',
'Facility' => 'Facilities',
'Hashtags' => 'Hashtag',
}.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
sync_carrierwave_url
Class Method Details
.categories ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'app/models/restaurant_tag.rb', line 181
def categories
Rails.cache.fetch "categories:#{RestaurantTag.maximum(:updated_at)}", expires_in: 1.hour do
data = []
exclude = %w[marketing fwe]
RestaurantTag.all.group_by(&:category).map do |category, tags|
next if exclude.include?(category.downcase)
category = CATEGORY_FILTER[category] if CATEGORY_FILTER[category].present?
data << {
id: tags.first.id,
title: category,
title_format: category.gsub(/([a-z])([A-Z])/) { "#{$1} #{$2}" },
}
end
data
end
end
|
.cuisines_cache_key ⇒ Object
257
258
259
|
# File 'app/models/restaurant_tag.rb', line 257
def cuisines_cache_key
RestaurantTag.where('title_en LIKE ?', '%cuisine%').cache_key
end
|
.find_by(params) ⇒ Object
Patch writer method to be compatible with Globalize
262
263
264
265
266
267
268
|
# File 'app/models/restaurant_tag.rb', line 262
def find_by(params)
if params.has_key? :title
title_param = params.delete(:title)
params.merge!({ title_en: title_param })
end
original_find_by(params)
end
|
.locations_cache_key ⇒ Object
253
254
255
|
# File 'app/models/restaurant_tag.rb', line 253
def locations_cache_key
RestaurantTag.where_category('location').cache_key
end
|
.order(*args) ⇒ Object
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'app/models/restaurant_tag.rb', line 289
def order(*args)
params = []
args.each do |arg|
case arg
when String
if arg.include?('title_en') || arg.include?('title_th')
params.push arg
else
params.push arg.dup.gsub 'title', 'title_en'
end
when Hash
if arg.has_key? :title
title_param = arg.dup.delete(:title)
params.push({ title_en: title_param })
end
end
end
original_order(params)
end
|
.original_find_by ⇒ Object
251
|
# File 'app/models/restaurant_tag.rb', line 251
alias original_find_by find_by
|
.original_order ⇒ Object
288
|
# File 'app/models/restaurant_tag.rb', line 288
alias original_order order
|
.original_where ⇒ Object
270
|
# File 'app/models/restaurant_tag.rb', line 270
alias original_where where
|
.where(params = :chain, *rest) ⇒ Object
Patch writer method to be compatible with Globalize
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'app/models/restaurant_tag.rb', line 273
def where(params = :chain, *rest)
params = params.dup
case params
when String
params.gsub! 'title', 'title_en' unless params.include?('title_en') || params.include?('title_th')
when Hash
if params.has_key? :title
title_param = params.delete(:title)
params.merge!({ title_en: title_param })
end
end
original_where(params, *rest)
end
|
Instance Method Details
#category ⇒ Object
206
207
208
|
# File 'app/models/restaurant_tag.rb', line 206
def category
title_en.to_s.split(':').first
end
|
#fetch_restaurants ⇒ Object
200
201
202
203
204
|
# File 'app/models/restaurant_tag.rb', line 200
def fetch_restaurants
Rails.cache.fetch "#{cache_key}/restaurants", expires_in: 1.hour do
restaurants
end
end
|
#radius ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'app/models/restaurant_tag.rb', line 233
def radius
return AdminSetting.popular_zone_distance_limit_max_x_km.to_i unless respond_to?(:radius_in_km)
return radius_in_km if radius_in_km.present?
case category
when 'PopularZone'
3
when 'ShoppingMall', 'BtsRoute', 'MrtRoute'
1
else
AdminSetting.popular_zone_distance_limit_max_x_km.to_i
end
end
|
#run_callback_for_geo_changes_on_create ⇒ Object
311
312
313
314
315
|
# File 'app/models/restaurant_tag.rb', line 311
def run_callback_for_geo_changes_on_create
if lat.present? && lng.present?
Tagging::RestaurantTagToRestaurantsWorker.perform_async(id)
end
end
|
#run_callback_for_geo_changes_on_update ⇒ Object
317
318
319
320
321
|
# File 'app/models/restaurant_tag.rb', line 317
def run_callback_for_geo_changes_on_update
if (lat.present? && lng.present?) && (saved_changes.keys & ['lat', 'lng', 'radius_in_km']).any?
Tagging::RestaurantTagToRestaurantsWorker.perform_async(id)
end
end
|
#title ⇒ String
Return RestaurantTag title according to selected I18n language
Return RestaurantTag title (without category) according to selected I18n language
211
212
213
214
215
216
217
218
219
220
221
|
# File 'app/models/restaurant_tag.rb', line 211
def title_format(lang = MyLocaleManager.normalize_locale, for_api: false)
return send("title_#{lang}").to_s.split(':').last unless for_api
title_length = send("title_#{lang}").to_s.split(':').size
if title_length > 1
send("title_#{lang}").split(':').last
else
send("title_#{MyLocaleManager.default_locale}").to_s.split(':').last
end
end
|
#update_search_icon_city ⇒ Object
323
324
325
326
327
328
329
|
# File 'app/models/restaurant_tag.rb', line 323
def update_search_icon_city
search_icon = SearchIcon.find_by(restaurant_tag_id: id)
return if search_icon.blank?
SearchIconsCity.where(search_icon_id: search_icon.id).delete_all
search_icon.search_icons_cities.create(city_id: city_id) if city_id.present?
end
|