Class: RestaurantSection

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/restaurant_section.rb

Overview

Restaurant Section filter

Instance Method Summary collapse

Constructor Details

#initializeRestaurantSection

Returns a new instance of RestaurantSection.



5
6
7
# File 'app/my_lib/restaurant_section.rb', line 5

def initialize
  @config = {}
end

Instance Method Details

#as_json(context, minor_version_param) ⇒ Object



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
83
84
85
86
87
88
89
90
91
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
# File 'app/my_lib/restaurant_section.rb', line 56

def as_json(context, minor_version_param)
  if restaurant_tag_section?
    filter = build_restaurant_tag
    filter.as_json(context, minor_version_param, {}).
      merge(
        success: true,
        message: nil,
        title: get_title,
        description: get_description,
        footer: get_footer,
        tag_line: get_tag_line,
        slug: get_slug,
        icon: get_icon,
        clevertap_event_name: get_clevertap_event_name,
      ).merge(home_section_attributes)
  elsif branch_section?
    options = {
      serialization_context: context,
      serializer: ActiveModel::Serializer::CollectionSerializer,
      adapter: :json_api,
      each_serializer: Api::V5::BranchSerializer,
    }

    json = ActiveModelSerializers::SerializableResource.new(build_branch,
                                                            options).as_json

    ads = fetch_ads('branch')
    json[:ads] = ads if ads.present?
    json.merge(success: true,
               message: nil,
               title: get_title,
               description: get_description,
               footer: get_footer,
               tag_line: get_tag_line,
               slug: get_slug,
               icon: get_icon,
               clevertap_event_name: get_clevertap_event_name).merge(home_section_attributes)
  elsif nearest_promotion?
    options = {}
    options[:each_serializer] = Api::V5::RestaurantSerializer

    options.merge! serialization_context: context,
                   serializer: ActiveModel::Serializer::CollectionSerializer,
                   adapter: :json_api,
                   minor_version: minor_version_param
    json = ActiveModelSerializers::SerializableResource.new(collections, options).as_json
    json.merge(success: true,
               message: nil,
               title: get_title,
               description: get_description,
               footer: get_footer,
               tag_line: get_tag_line,
               slug: get_slug,
               icon: get_icon,
               clevertap_event_name: get_clevertap_event_name).merge(home_section_attributes)
  else
    ads = fetch_ads('restaurant')
    filter = build_compact_restaurant
    json = filter.as_json(context, minor_version_param, { include: 'group_landing_page' }).
      merge(success: true,
            message: nil,
            title: get_title,
            description: get_description,
            footer: get_footer,
            tag_line: get_tag_line,
            slug: get_slug,
            icon: get_icon,
            next_reset_trending_at: next_reset_trending_at,
            clevertap_event_name: get_clevertap_event_name).merge(home_section_attributes)

    json[:ads] = ads if ads.present?
    json
  end
end

#branch_section?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'app/my_lib/restaurant_section.rb', line 224

def branch_section?
  config[:section_number] == 'section_5'
end

#by_city(city_id) ⇒ Object



29
30
31
32
# File 'app/my_lib/restaurant_section.rb', line 29

def by_city(city_id)
  config[:city_id] = city_id if city_id.to_i.positive?
  self
end

#cache_keyObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
173
174
# File 'app/my_lib/restaurant_section.rb', line 131

def cache_key
  return '' if config[:section_number].blank?

  ads_key = (Advertisement.send(config[:section_number].to_sym).cache_key if section_ads?)
  ck = if branch_section?
         CityHash.hash32([self.class.to_s, config[:section_number],
                          start_date, end_date, config,
                          Branch.maximum(:updated_at),
                          Report.cached_all_cache_key,
                          home_restaurant_section&.cache_key,
                          I18n.locale,
                          thai_today])
       elsif restaurant_tag_section?
         CityHash.hash32([self.class.to_s, config[:section_number], config,
                          RestaurantTag.maximum(:updated_at),
                          Report.cached_all_cache_key,
                          home_restaurant_section&.cache_key,
                          I18n.locale,
                          config[:city_id], thai_today])
       elsif nearest_promotion?
         CityHash.hash32([self.class.to_s, config[:section_number],
                          config,
                          Restaurant.maximum(:updated_at),
                          I18n.locale,
                          config[:longitude],
                          config[:latitude],
                          thai_today,
                          Report.cached_all_cache_key,
                          home_restaurant_section&.cache_key,
                          AdminSetting.enable_free_delivery_just_for_you])
       else
         CityHash.hash32([self.class.to_s,
                          config[:section_number],
                          config,
                          CompactRestaurant.maximum(:updated_at),
                          I18n.locale,
                          config[:city_id],
                          Report.cached_all_cache_key,
                          home_restaurant_section&.cache_key,
                          next_reset_trending_at,
                          thai_today])
       end
  "#{cache_name_space}:#{ck}:#{ads_key}"
end

#collectionsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/my_lib/restaurant_section.rb', line 42

def collections
  return [] if config[:section_number].blank?

  if nearest_promotion?
    build_nearest_promotion_restaurants
  elsif restaurant_tag_section?
    build_restaurant_tag
  elsif branch_section?
    build_branch
  else
    build_compact_restaurant.collections
  end
end

#compact_section?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'app/my_lib/restaurant_section.rb', line 232

def compact_section?
  !branch_section? && !restaurant_tag_section?
end

#get_clevertap_event_nameObject



212
213
214
# File 'app/my_lib/restaurant_section.rb', line 212

def get_clevertap_event_name
  I18n.t("home_screen.#{config[:section_number]}") || ''
end

#get_descriptionObject



188
189
190
# File 'app/my_lib/restaurant_section.rb', line 188

def get_description
  home_restaurant_section&.description(for_api: true)
end


196
197
198
# File 'app/my_lib/restaurant_section.rb', line 196

def get_footer
  home_restaurant_section&.footer_description(for_api: true)
end

#get_iconObject



204
205
206
# File 'app/my_lib/restaurant_section.rb', line 204

def get_icon
  home_restaurant_section&.icon
end

#get_punch_lineObject



184
185
186
# File 'app/my_lib/restaurant_section.rb', line 184

def get_punch_line
  home_restaurant_section&.punch_line(for_api: true)
end

#get_slugObject



200
201
202
# File 'app/my_lib/restaurant_section.rb', line 200

def get_slug
  home_restaurant_section&.friendly_id || ''
end

#get_tag_lineObject



192
193
194
# File 'app/my_lib/restaurant_section.rb', line 192

def get_tag_line
  home_restaurant_section&.tag_line(for_api: true)
end

#get_titleObject



176
177
178
179
180
181
182
# File 'app/my_lib/restaurant_section.rb', line 176

def get_title
  if home_restaurant_section.blank?
    return I18n.t("home_screen.#{config[:section_number]}").presence || ''
  end

  home_restaurant_section.title(for_api: true)
end

#home_restaurant_sectionObject



208
209
210
# File 'app/my_lib/restaurant_section.rb', line 208

def home_restaurant_section
  HomeSection.find_by(section_type: config[:section_number])
end

#nearest_promotion?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'app/my_lib/restaurant_section.rb', line 228

def nearest_promotion?
  config[:section_number] == 'section_14'
end

#page_number(number) ⇒ Object



14
15
16
17
# File 'app/my_lib/restaurant_section.rb', line 14

def page_number(number)
  config[:page_number] = number.to_i if number.to_i.positive?
  self
end

#per_page(number) ⇒ Object



19
20
21
22
# File 'app/my_lib/restaurant_section.rb', line 19

def per_page(number)
  config[:per_page] = number.to_i if number.to_i.positive?
  self
end

#restaurant_tag_section?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'app/my_lib/restaurant_section.rb', line 220

def restaurant_tag_section?
  ['section_7', 'section_4', 'section_10'].include?(config[:section_number])
end

#section_number(section_no) ⇒ Object



24
25
26
27
# File 'app/my_lib/restaurant_section.rb', line 24

def section_number(section_no)
  config[:section_number] = section_no if section_no.present?
  self
end

#set_context(context) ⇒ Object



9
10
11
12
# File 'app/my_lib/restaurant_section.rb', line 9

def set_context(context)
  config[:serialization_context] = context if context.present?
  self
end

#set_location(location) ⇒ Object



34
35
36
37
38
39
40
# File 'app/my_lib/restaurant_section.rb', line 34

def set_location(location)
  return if location.blank?

  config[:longitude] = location[:lon]
  config[:latitude] = location[:lat]
  self
end

#sort_byObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/my_lib/restaurant_section.rb', line 236

def sort_by
  case config[:section_number].to_s
  when 'section_1'
    'new'
  when 'section_3'
    'trending'
    # 'seven_seven_hashtag'
  when 'section_6'
    'under_1000b'
  when 'section_8'
    'staycation'
  when 'section_9'
    'premium_delivery_offers_hashtag'
  when 'section_11'
    # 'top_dine_in'
    'w1_recommendation'
  when 'section_12'
    'section_12'
  when 'section_13'
    'rank'
  else
    ''
  end
end

#thai_todayObject



216
217
218
# File 'app/my_lib/restaurant_section.rb', line 216

def thai_today
  Time.thai_time.to_date
end