Class: CompactRestaurant

Inherits:
ApplicationRecord show all
Defined in:
app/models/compact_restaurant.rb

Overview

This model is a combination of several restaurants, and the data is generated by the GenerateCompactRestaurantsWorker Diagram: drive.google.com/file/d/1gSEiBn2sIKWeO9okqjpOFiCuI5T4WXM4/view?usp=sharing If the restaurant belongs to a branch, then the compact restaurant only has data for that branch

attributes:

custom_text:
  is a field to add short text that will be displayed on home page
  CompactRestaurant will merge all Restaurant's custom_text data into this field

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Instance Attribute Details

#top_orderInteger

Returns:

  • (Integer)

    for the ranking of compact restaurant data based on revenue data



# File 'app/models/compact_restaurant.rb', line 102

Instance Method Details

#group_landing_pageObject



111
112
113
114
115
116
117
# File 'app/models/compact_restaurant.rb', line 111

def group_landing_page
  if branch.present?
    GroupLandingPage.find_by(group: branch)
  elsif restaurant.present?
    GroupLandingPage.find_by(group: restaurant)
  end
end

#highest_promotion_badgeObject

get the highest promotion badge from the compact restaurant



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/compact_restaurant.rb', line 120

def highest_promotion_badge
  restaurant_ids = if branch_id.present?
                     branch.restaurants.active.not_expired.pluck(:id) || []
                   elsif restaurant_id.present?
                     [restaurant_id].compact
                   end

  return { promotion_type: nil, promotion_text: nil } if restaurant_ids.blank?

  highest_promo_restaurants = Restaurant.where(id: restaurant_ids).map do |r|
    r.promotion_badge(with_percentage: true)
  end.compact
  highest_promo = highest_promo_restaurants.max_by { |promo| promo[:percentage_discount] }

  if highest_promo.nil? || highest_promo.dig(:percentage_discount)&.zero?
    return { promotion_type: nil,
             promotion_text: nil }
  end

  { promotion_type: highest_promo[:promotion_type], promotion_text: highest_promo[:promotion_text] }
end