Class: RestaurantDecorator

Inherits:
Draper::Decorator
  • Object
show all
Includes:
ImageHelper
Defined in:
app/decorators/restaurant_decorator.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Methods included from ImageHelper

#fix_image_url

Instance Method Details

#all_package_pricingsObject

to find price per person



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/decorators/restaurant_decorator.rb', line 50

def all_package_pricings
  @all_package_pricings ||= cached_packages.select do |package|
    if package.respond_to?(:is_add_on)
      package.is_add_on == false
    else
      true
    end
  end.map do |package|
    package.dynamic_pricings.map do |p|
      per_pack = p.per_pack || 1
      (p.price_cents / per_pack.to_f).ceil
    end
  end.flatten
end

#award_badge(for_api: false, lang: nil) ⇒ String

Returns the primary award badge for the restaurant.

Parameters:

  • for_api (Boolean) (defaults to: false)

    whether to format for API output

  • lang (String, nil) (defaults to: nil)

    optional language code for localization

Returns:

  • (String)

    the formatted award badge or '-' if not found



283
284
285
286
287
288
# File 'app/decorators/restaurant_decorator.rb', line 283

def award_badge(for_api: false, lang: nil)
  tag = object.primary_tag_by_category(RestaurantTag::AWARD_BADGE)&.restaurant_tag
  return '-' unless tag

  lang.present? ? tag.title_format(lang) : tag.title_format(for_api: for_api)
end

#award_type(for_api: false, lang: nil) ⇒ String

Returns the primary award type for the restaurant.

Parameters:

  • for_api (Boolean) (defaults to: false)

    whether to format for API output

  • lang (String, nil) (defaults to: nil)

    optional language code for localization

Returns:

  • (String)

    the formatted award type or '-' if not found



295
296
297
298
299
300
# File 'app/decorators/restaurant_decorator.rb', line 295

def award_type(for_api: false, lang: nil)
  tag = object.primary_tag_by_category(RestaurantTag::AWARD_TYPE)&.restaurant_tag
  return '-' unless tag

  lang.present? ? tag.title_format(lang) : tag.title_format(for_api: for_api)
end

set locale to nil if you want to follow current user language setting when they click the link



10
11
12
13
14
# File 'app/decorators/restaurant_decorator.rb', line 10

def canonical_link(locale: I18n.locale)
  link = "#{web_v2_host}/restaurants/#{object.friendly_id}"
  link = "#{link}?locale=#{locale}" if locale.present?
  link
end

#cuisine(for_api: false, lang: nil) ⇒ String

Returns the primary cuisine of the restaurant.

Parameters:

  • for_api (Boolean) (defaults to: false)

    whether to format for API output

  • lang (String, nil) (defaults to: nil)

    optional language code for localization

Returns:

  • (String)

    the formatted cuisine or '-' if not found



259
260
261
262
263
264
# File 'app/decorators/restaurant_decorator.rb', line 259

def cuisine(for_api: false, lang: nil)
  tag = object.primary_tag_by_category(RestaurantTag::CUISINE)&.restaurant_tag
  return '-' unless tag

  lang.present? ? tag.title_format(lang, for_api: for_api) : tag.title_format(for_api: for_api)
end

#cuisines(for_api: false) ⇒ Object



20
21
22
23
24
25
# File 'app/decorators/restaurant_decorator.rb', line 20

def cuisines(for_api: false)
  tags = object.cuisine_tags
  return [] if tags.blank?

  tags.map { |tag| tag.title_format(for_api: for_api) }
end

#descriptionObject



27
28
29
30
31
32
# File 'app/decorators/restaurant_decorator.rb', line 27

def description
  actual_desc = object.send(:"misc_#{MyLocaleManager.normalize_locale}")
  return actual_desc if actual_desc.present?

  object.misc_en
end

#dining_style(for_api: false, lang: nil) ⇒ String

Returns the primary dining style of the restaurant.

Parameters:

  • for_api (Boolean) (defaults to: false)

    whether to format for API output

  • lang (String, nil) (defaults to: nil)

    optional language code for localization

Returns:

  • (String)

    the formatted dining style or '-' if not found



271
272
273
274
275
276
# File 'app/decorators/restaurant_decorator.rb', line 271

def dining_style(for_api: false, lang: nil)
  tag = object.primary_tag_by_category(RestaurantTag::DINING_STYLE)&.restaurant_tag
  return '-' unless tag

  lang.present? ? tag.title_format(lang, for_api: for_api) : tag.title_format(for_api: for_api)
end


341
342
343
344
# File 'app/decorators/restaurant_decorator.rb', line 341

def google_map_link
  query = { api: 1, query: "#{object.lat},#{object.lng}" }
  "https://www.google.com/maps/search/?#{query.to_query}"
end

#image_cover_url(width: 300, height: 180, original: false, include_size: false, only_path: true, version: nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/decorators/restaurant_decorator.rb', line 208

def image_cover_url(width: 300, height: 180, original: false, include_size: false, only_path: true, version: nil)
  width = 300 if width.nil?
  height = 180 if height.nil?

  cover = object.pictures.cover

  if cover && cover.item.present?
    if !version.nil?
      fix_image_url(cover.item_url)
    elsif original
      if include_size
        {
          url: fix_image_url(cover.item_url, only_path),
          width: cover.width,
          height: cover.height,
        }
      else
        fix_image_url(cover.item_url, only_path)
      end
    elsif cover && cover.item.present?
      url = if width == height
              fix_image_url(cover.item_url, only_path)
            else
              fix_image_url(cover.item_url, only_path)
            end
      if include_size
        {
          url: url,
          width: width,
          height: height,
        }
      else
        url
      end
    else
      {
        url: url,
        width: width,
        height: height,
      }
    end
  else
    Rails.env.production? ? '' : "https://placeimg.com/#{width}/#{height}/any"
  end
end

#last_booking_time {|lbt| ... } ⇒ Object

Yields:

  • (lbt)


196
197
198
199
200
# File 'app/decorators/restaurant_decorator.rb', line 196

def last_booking_time
  lbt = last_reservation.present? ? last_reservation.created_at.rfc2822 : nil

  yield lbt unless lbt.nil?
end

#last_booking_time_jsonObject



202
203
204
205
206
# File 'app/decorators/restaurant_decorator.rb', line 202

def last_booking_time_json
  return nil if last_reservation.blank?

  last_reservation.present? ? last_reservation.created_at.rfc2822 : nil
end


335
336
337
338
339
# File 'app/decorators/restaurant_decorator.rb', line 335

def link_to_self(params = {}, _include_root_path = false)
  url = "#{web_v2_host}/restaurants/#{object.friendly_id}?locale=#{I18n.locale}"
  url += "?#{params.to_query}" if params.present?
  url
end

#location(for_api: false, lang: nil) ⇒ String

Returns the primary location tag for the restaurant. This includes popular zones, locations, BTS routes, MRT routes, and shopping malls.

Parameters:

  • for_api (Boolean) (defaults to: false)

    whether to format for API output

  • lang (String, nil) (defaults to: nil)

    optional language code for localization

Returns:

  • (String)

    the formatted location or '-' if not found



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/decorators/restaurant_decorator.rb', line 308

def location(for_api: false, lang: nil)
  tag_categories = [
    RestaurantTag::POPULAR_ZONE,
    RestaurantTag::LOCATION,
    RestaurantTag::BTS_ROUTE,
    RestaurantTag::MRT_ROUTE,
    RestaurantTag::SHOPPING_MALL,
  ]

  tag = tag_categories.lazy.map do |category|
          object.primary_tag_by_category(category)
        end.detect { |t| t&.restaurant_tag }

  return '-' unless tag&.restaurant_tag

  if lang.present?
    tag.restaurant_tag.title_format(lang,
                                    for_api: for_api)
  else
    tag.restaurant_tag.title_format(for_api: for_api)
  end
end

#lowest_package_price_and_pricing_type(with_original_price: false) ⇒ Object



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
130
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
# File 'app/decorators/restaurant_decorator.rb', line 65

def lowest_package_price_and_pricing_type(with_original_price: false)
  pricing_type = :per_person

  all_tickets = object.ticket_groups.not_expired.map do |ticket_group|
    calculated_lowest_pricing = if ticket_group.party_size.to_i.positive?
                                  ticket_group.discount_price_cents / ticket_group.party_size.to_f
                                else
                                  ticket_group.discount_price_cents
                                end
    {
      price_cents: calculated_lowest_pricing,
    }
  end

  price_finder ||= HhPackage::ReservationPackages::PriceFinder.new use_dynamic_pricing: true
  all_pricings = object.restaurant_packages.exclude_preview.select do |restaurant_package|
    package = restaurant_package.package
    if package.respond_to?(:is_add_on)
      package.is_add_on == false
    else
      true
    end
  end.map do |restaurant_package|
    package = restaurant_package.package

    cmpl_pricing = price_finder.come_more_pay_less_sample_price_for_preview(package, restaurant_id: object.id)
    cmpl_lowest_pricing = cmpl_pricing&.dig(:sample_price_per_person).to_i

    lowest_pricing = package.lowest_pricing
    calculated_lowest_pricing = if lowest_pricing.blank?
                                  0
                                elsif lowest_pricing.per_pack.blank?
                                  lowest_pricing.price_cents
                                else
                                  (lowest_pricing.price_cents / lowest_pricing.per_pack.to_f).ceil
                                end

    original_price_cents = package.package_attr&.original_price_cents
    calculated_original_pricing = if original_price_cents.blank?
                                    0
                                  elsif lowest_pricing&.per_pack.blank?
                                    original_price_cents
                                  else
                                    (original_price_cents / lowest_pricing.per_pack.to_f).ceil
                                  end

    price = if cmpl_lowest_pricing > 0
              [cmpl_lowest_pricing, calculated_lowest_pricing].min
            else
              calculated_lowest_pricing
            end

    {
      type: restaurant_package.package_type,
      price_cents: price,
      original_price_cents: calculated_original_pricing,
    }
  end

  lowest_price = [all_tickets + all_pricings].flatten.min_by { |pr| pr[:price_cents] }

  money = HhMoney.new(
    lowest_price.present? ? lowest_price[:price_cents] : 0,
    object.default_currency,
  )

  money_original_price = HhMoney.new(
    lowest_price.present? ? lowest_price[:original_price_cents] : 0,
    object.default_currency,
  )

  result = {
    amount: money.amount.to_i,
    amount_cents: money.amount_cents,
    currency: money.currency.to_s,
    symbol: money.currency.symbol,
    format: money.format_rounded,
    pricing_type: pricing_type,
  }

  if with_original_price.to_s == 'true'
    result[:original_price] = {
      amount: money_original_price.amount.to_i,
      currency: money_original_price.currency.to_s,
      symbol: money_original_price.currency.symbol,
      format: money_original_price.format_rounded,
    }
  end

  result
end

#package_pricingsObject



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'app/decorators/restaurant_decorator.rb', line 346

def package_pricings
  result = []
  HhPackage::PACKAGE_LIST.each do |constant|
    restaurant_packages = object.restaurant_packages.where(package_type: "HhPackage::Package::#{constant}")
    next if restaurant_packages.blank?

    packages = restaurant_packages.map(&:fetch_package)
    cheapest = packages.min do |pkg_x, pkg_y|
      pkg_x.lowest_pricing.price_cents <=> pkg_y.lowest_pricing.price_cents
    end

    pricing = cheapest.lowest_pricing
    next if pricing.blank?

    is_per_pack = pricing.per_pack.present?

    price_cents = if is_per_pack
                    (pricing.price_cents / pricing.per_pack.to_f).ceil
                  else
                    pricing.price_cents
                  end

    price = HhMoney.new(price_cents, pricing.price_currency)

    price_msg = if is_per_pack
                  I18n.t('views.restaurant.from_for_pricing_party_pack', price: price.format_rounded)
                else
                  I18n.t('views.restaurant.from_for_pricing', price: price.format_rounded)
                end
    result.push "#{packages.first.type} #{price_msg}"
  end
  result
end

#parkingObject



331
332
333
# File 'app/decorators/restaurant_decorator.rb', line 331

def parking
  object.parking ? I18n.t('views.d.have_parking') : I18n.t('views.d.dont_have_parking')
end

#price_per_person(as_string: true) ⇒ Object

Returns price in string if as_string parameter is true, otherwise return Int format.

Parameters:

  • as_string (Boolean) (defaults to: true)

    default false

Returns:

  • price in string if as_string parameter is true, otherwise return Int format



183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/decorators/restaurant_decorator.rb', line 183

def price_per_person(as_string: true)
  pricing = cached_packages&.map(&:lowest_pricing)&.compact&.min_by(&:price_cents)
  if pricing.nil?
    return '' if as_string

    return 0
  end

  return HhMoney.new(pricing.price_cents, pricing.price_currency).format_rounded if as_string

  pricing.price.amount
end

#price_rangeObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/decorators/restaurant_decorator.rb', line 157

def price_range
  return '' if cached_packages.blank?

  min = all_package_pricings.min
  max = all_package_pricings.max
  currency = object.default_currency
  if min == max
    price = HhMoney.new(min, currency)
    price.format
  else
    min = HhMoney.new(min, currency)
    max = HhMoney.new(max, currency)
    "#{min.amount.to_i} - #{max.amount.to_i} #{min.currency}"
  end
end

#reviews_score_avgObject



45
46
47
# File 'app/decorators/restaurant_decorator.rb', line 45

def reviews_score_avg
  object&.weighted_score&.round(1) || 0
end


16
17
18
# File 'app/decorators/restaurant_decorator.rb', line 16

def rwg_link(locale: MyLocaleManager.default_locale)
  "#{canonical_link(locale: locale)}&utm_source=rwg"
end

#total_packagesObject



173
174
175
# File 'app/decorators/restaurant_decorator.rb', line 173

def total_packages
  cached_packages.count
end

#total_reviews(format: :html) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'app/decorators/restaurant_decorator.rb', line 34

def total_reviews(format: :html)
  if reviews_count.positive?
    return I18n.t('views.d.total_reviews', count: reviews_count) if format == :html
    return reviews_count if format == :json
  end

  return 0 if format == :json

  nil
end

#total_ticket_groupsObject



177
178
179
# File 'app/decorators/restaurant_decorator.rb', line 177

def total_ticket_groups
  cached_ticket_groups.count
end