Class: RestaurantTag

Inherits:
ApplicationRecord show all
Includes:
IdentityCache
Defined in:
app/models/restaurant_tag.rb

Overview

RestaurantTag has relation to the Restaurant model through two models

  1. PrimaryTag

  2. 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

Direct Known Subclasses

RestaurantTagCpt::ModelDecorator

Constant Summary collapse

REQUIRED_LOCALES =

Required locales for restaurant tags

%i[en th cn].freeze
HASHTAGS =
'Hashtags'
CUISINE =
'Cuisine'
'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

Methods inherited from ApplicationRecord

sync_carrierwave_url

Class Method Details

.categoriesObject



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_keyObject



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_keyObject



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_byObject



251
# File 'app/models/restaurant_tag.rb', line 251

alias original_find_by find_by

.original_orderObject



288
# File 'app/models/restaurant_tag.rb', line 288

alias original_order order

.original_whereObject



270
# File 'app/models/restaurant_tag.rb', line 270

alias original_where where

.tagged_as_ayceObject



172
173
174
175
176
177
178
179
# File 'app/models/restaurant_tag.rb', line 172

HhPackage::PACKAGE_SHORT_LIST_AND_LONG_NAME.each do |type, name|
  define_method :"tagged_as_#{type}" do
    title = "Offer:#{name}"
    tag = RestaurantTag.find_by(title_en: title)
    tag = RestaurantTag.create!(title_en: title, title_ko: title, title_th: title) if tag.blank?
    tag
  end
end

.tagged_as_bfpObject



172
173
174
175
176
177
178
179
# File 'app/models/restaurant_tag.rb', line 172

HhPackage::PACKAGE_SHORT_LIST_AND_LONG_NAME.each do |type, name|
  define_method :"tagged_as_#{type}" do
    title = "Offer:#{name}"
    tag = RestaurantTag.find_by(title_en: title)
    tag = RestaurantTag.create!(title_en: title, title_ko: title, title_th: title) if tag.blank?
    tag
  end
end

.tagged_as_ppObject



172
173
174
175
176
177
178
179
# File 'app/models/restaurant_tag.rb', line 172

HhPackage::PACKAGE_SHORT_LIST_AND_LONG_NAME.each do |type, name|
  define_method :"tagged_as_#{type}" do
    title = "Offer:#{name}"
    tag = RestaurantTag.find_by(title_en: title)
    tag = RestaurantTag.create!(title_en: title, title_ko: title, title_th: title) if tag.blank?
    tag
  end
end

.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

#categoryObject



206
207
208
# File 'app/models/restaurant_tag.rb', line 206

def category
  title_en.to_s.split(':').first
end

#fetch_restaurantsObject



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

#radiusObject



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'
    # default radius value for popular zone is 3 km
    3
  when 'ShoppingMall', 'BtsRoute', 'MrtRoute'
    # default radius value for shopping mall, bts, and mrt is 1 km
    1
  else
    AdminSetting.popular_zone_distance_limit_max_x_km.to_i
  end
end

#run_callback_for_geo_changes_on_createObject



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_updateObject



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

#titleString

Return RestaurantTag title according to selected I18n language

Returns:

  • (String)

    return RestaurantTag title according to selected I18n language



224
225
226
# File 'app/models/restaurant_tag.rb', line 224

def title
  send(:"title_#{MyLocaleManager.normalize_locale}")
end

#title=(t) ⇒ Object

Parameters:

  • t (String)

    en, or th. to return RestaurantTag title according to selected I18n language



229
230
231
# File 'app/models/restaurant_tag.rb', line 229

def title=(t)
  send(:"title_#{MyLocaleManager.normalize_locale}=", t)
end

#title_format(lang = MyLocaleManager.normalize_locale, for_api: false) ⇒ String

Return RestaurantTag title (without category) according to selected I18n language

Returns:

  • (String)

    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

  # Fallback to default locale if title is empty in selected locale 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_cityObject



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