Class: HomeSection

Inherits:
ApplicationRecord show all
Extended by:
Enumerize, FriendlyId
Defined in:
app/models/home_section.rb

Overview

Schema Information

Table name: home_sections

id                   :bigint           not null, primary key
active               :boolean
active_mobile        :boolean          default(TRUE)
active_web           :boolean          default(TRUE)
description          :text(65535)
fixed_type           :boolean
footer_description   :text(65535)
group_section_type   :string(191)
icon                 :string(191)
link                 :string(191)
position             :integer
position_mobile      :integer          default(1)
position_web         :integer          default(1)
punch_line           :text(65535)
section_image        :string(191)
section_template     :string(191)
section_type         :string(191)
slug                 :string(191)
specific_city_filter :string(191)
tag_category         :string(191)
tag_line             :text(65535)
title                :text(65535)
created_at           :datetime         not null
updated_at           :datetime         not null
city_id              :bigint
group_section_id     :bigint
home_section_id      :bigint           not null

Indexes

index_home_sections_on_city_id                                  (city_id)
index_home_sections_on_group_section_type_and_group_section_id  (group_section_type,group_section_id)
index_home_sections_on_slug                                     (slug) UNIQUE

Constant Summary collapse

SECTION_RULES =

fixed means this section template is specific for special section

{
  outlet_4_card: {
    label: '4 Outlets Card',
    fixed: false,
    outlet_sortable: true,
  },
  outlet_6_card: {
    label: '6 Outlets Card',
    fixed: false,
    outlet_sortable: true,
  },
  tag_section: { # customable tag
    label: 'Tag Section',
    fixed: false,
    outlet_sortable: false,
  },
  tag_default: { # default logic tag
    label: 'Tag Default',
    fixed: true,
    outlet_sortable: false,
  },
  group_card: {
    label: 'Group Card',
    fixed: false,
    outlet_sortable: false,
  },
  hh_specials: {
    label: 'Node Display',
    fixed: true,
    outlet_sortable: false,
  },
  desktop_group_section: {
    label: 'Desktop Group Section',
    fixed: true,
    outlet_sortable: false,
  },
  banner: {
    label: 'Banner Section',
    fixed: true,
    outlet_sortable: false,
  },
  homepage_icon: {
    label: 'Hungry Hub Icon',
    fixed: true,
    outlet_sortable: false,
  },
}.freeze
SECTION_TEMPLATES =
SECTION_RULES.transform_values { |value| value[:label] }.freeze
SPECIFIC_CITY_FILTER =

Default (Same as user selected city) All outlet city can be show in this section Specific city

{
  default: 'Default (Same as user selected city)',
  all_outlet: 'All outlet city can be show in this section',
  specific_city: 'Specific City',
}.freeze
SECTION_ROUTE =

example: route section_3: -> target :section_11 see config/locales/home.en.yml to see target section name

{
  # 1st, CompactRestaurant
  section_3: :section_11,

  # 2nd, CompactRestaurant
  section_1: :section_12,

  # 3rd, special section, don't change this!
  # restaurant card, not compact
  section_14: :section_14,

  # 4rd popular brand, Branch
  section_5: :section_5,

  # top categories location
  # 5th, Restaurant Tag
  section_4: :section_4,

  # top categories cuisine
  # 6th, Restaurant Tag
  section_7: :section_7,

  # 7th, CompactRestaurant
  section_8: :section_3,

  # 8th, CompactRestaurant
  section_6: :section_6,

  # 9th, CompactRestaurant
  section_9: :section_1,

  # 10th CompactRestaurant
  section_11: :section_8,

  # 11th CompactRestaurant (all restaurants)
  section_13: :section_13,

  # 12th NOT USED by app
  section_12: :section_13,

  # 10th
  section_10: :section_10,
}.freeze
COMPACT_RESTAURANT_SECTIONS =
%i[
  section_1
  section_3
  section_5
  section_6
  section_8
  section_11
  section_12
  section_13
].freeze
SECTION_TYPE =
%i[
  section_1
  section_3
  section_5
  section_4
  section_6
  section_7
  section_8
  section_10
  section_11
  section_12
  section_13
  section_14
  section_banner
  promotion_banner
  hh_specials
  custom_section
  homepage_icon
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Class Method Details

.restaurant_section_typesObject



379
380
381
382
383
# File 'app/models/home_section.rb', line 379

def self.restaurant_section_types
  SECTION_TYPE.index_by do |location|
    I18n.t("home_screen.#{location}")
  end
end

.section_templates(condition = nil) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/models/home_section.rb', line 230

def self.section_templates(condition = nil)
  return SECTION_RULES.transform_values { |value| value[:label] } if condition.blank?

  condition = condition.to_sym
  uncondition = condition.to_s.start_with?('un')

  SECTION_RULES.select do |_, properties|
    # if condition include un perfix then check with unperfix
    if uncondition
      condition = condition.to_s.gsub('un', '').to_sym
      properties[condition] == false
    else
      properties[condition]
    end
  end.transform_values { |value| value[:label] }
end

.trigger_build_homeObject



400
401
402
403
# File 'app/models/home_section.rb', line 400

def self.trigger_build_home
  webhook_url = ENV['HOMESECTION_WEBHOOK_URL']
  DefaultFaradayClient.create_faraday_connection.post(webhook_url)
end

Instance Method Details

#compact_restaurantsObject



226
227
228
# File 'app/models/home_section.rb', line 226

def compact_restaurants
  CompactRestaurant.where(active: true).where("rank_#{section_type} IS NOT NULL")
end

#description(lang = MyLocaleManager.normalize_locale, for_api: false) ⇒ Object



340
341
342
343
344
# File 'app/models/home_section.rb', line 340

def description(lang = MyLocaleManager.normalize_locale, for_api: false)
  return send("description_#{lang}") unless for_api

  send("description_#{lang}").presence || send("description_#{MyLocaleManager.default_locale}") || ''
end

#end_point_apiObject



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'app/models/home_section.rb', line 293

def end_point_api
  if non_custom_section?
    case section_type
    when 'section_banner'
      '/homes/section_banner.json'
    when 'promotion_banner'
      '/banners.json'
    when 'hh_specials'
      '/hungryhub_specials.json'
    when 'homepage_icon'
      '/homes/deals.json'
    else
      "/homes/#{HomeSection::SECTION_ROUTE.invert[section_type.to_s.to_sym]}"
    end
  elsif section_template == 'tag_section'
    "/homes/tags/#{slug}.json"
  elsif group_section_type == 'Branch'
    "/branch/#{group_section_id}/restaurants.json"
  elsif group_section_type == 'RestaurantTag'
    "/restaurant_tags/#{group_section_id}/restaurants.json"
  elsif group_section_type == 'RestaurantTagGroup'
    "/restaurant_tag_groups/#{group_section_id}.json"
  else
    ''
  end
end


352
353
354
355
356
357
# File 'app/models/home_section.rb', line 352

def footer_description(lang = MyLocaleManager.normalize_locale, for_api: false)
  return send("footer_description_#{lang}") unless for_api

  send("footer_description_#{lang}").presence ||
    send("footer_description_#{MyLocaleManager.default_locale}") || ''
end

#model_typeObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'app/models/home_section.rb', line 263

def model_type
  return 'restaurants' if slug.to_s.include?('near-me')
  return 'restaurant_tags' if ['tag_default', 'tag_section'].include?(section_template)

  case section_type
  when 'section_1', 'section_3', 'section_6', 'section_8', 'section_11', 'section_12', 'section_13', 'section_10'
    'compact_restaurants'
  when 'section_5'
    'branches'
  when 'section_14'
    'restaurants'
  when 'section_4', 'section_7'
    'restaurant_tags'
  when 'section_banner'
    'banners'
  when 'hh_specials'
    'hh_specials'
  when 'promotion_banner'
    'banners'
  when 'homepage_icon'
    'homepage_icon'
  else
    if group_section_id.present?
      'restaurants'
    else
      ''
    end
  end
end

#non_custom_section?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'app/models/home_section.rb', line 222

def non_custom_section?
  section_type != 'custom_section'
end

#punch_line(lang = MyLocaleManager.normalize_locale, for_api: false) ⇒ Object



334
335
336
337
338
# File 'app/models/home_section.rb', line 334

def punch_line(lang = MyLocaleManager.normalize_locale, for_api: false)
  return send("punch_line_#{lang}") unless for_api

  send("punch_line_#{lang}").presence || send("punch_line_#{MyLocaleManager.default_locale}") || ''
end

#slug_candidatesObject



320
321
322
323
324
325
326
# File 'app/models/home_section.rb', line 320

def slug_candidates
  [
    :title_en,
    %i[title_en title_th],
    %i[title_en title_th id],
  ]
end

#tag_line(lang = MyLocaleManager.normalize_locale, for_api: false) ⇒ Object



346
347
348
349
350
# File 'app/models/home_section.rb', line 346

def tag_line(lang = MyLocaleManager.normalize_locale, for_api: false)
  return send("tag_line_#{lang}") unless for_api

  send("tag_line_#{lang}").presence || send("tag_line_#{MyLocaleManager.default_locale}") || ''
end

#title(lang = MyLocaleManager.normalize_locale, for_api: false) ⇒ Object



328
329
330
331
332
# File 'app/models/home_section.rb', line 328

def title(lang = MyLocaleManager.normalize_locale, for_api: false)
  return send("title_#{lang}") unless for_api

  send("title_#{lang}").presence || send("title_#{MyLocaleManager.default_locale}") || ''
end

#update_compact_restaurants_rank(params) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'app/models/home_section.rb', line 247

def update_compact_restaurants_rank(params)
  return true if params.blank?

  compact_restaurants.update_all("rank_#{section_type}": nil)

  Array.wrap(params).map(&:to_h).each do |h|
    next if h[:id].blank? || h[:rank].blank?

    cr = CompactRestaurant.find(h[:id])
    cr.send("rank_#{section_type}=".to_sym, h[:rank].to_i)
    cr.save
  end

  Tagging::UpdateSpecialSectionWorker.perform_async if section_type == 'section_12'
end