Class: CompactRestaurantsFilter

Inherits:
Object
  • Object
show all
Includes:
Api::V5::PaginationConcernFilter
Defined in:
app/filters/compact_restaurants_filter.rb

Overview

typed: ignore

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api::V5::PaginationConcernFilter

#page_number, #per_page

Constructor Details

#initialize(collections = nil) ⇒ CompactRestaurantsFilter

Returns a new instance of CompactRestaurantsFilter.



8
9
10
# File 'app/filters/compact_restaurants_filter.rb', line 8

def initialize(collections = nil)
  init_default if collections.nil?
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



4
5
6
# File 'app/filters/compact_restaurants_filter.rb', line 4

def collections
  @collections
end

#group_landing_page_idObject

Returns the value of attribute group_landing_page_id.



4
5
6
# File 'app/filters/compact_restaurants_filter.rb', line 4

def group_landing_page_id
  @group_landing_page_id
end

#section_numberObject

Returns the value of attribute section_number.



4
5
6
# File 'app/filters/compact_restaurants_filter.rb', line 4

def section_number
  @section_number
end

Instance Method Details

#as_json(context, _version, options = {}) ⇒ Object

Examples:

options = {}

Parameters:

  • context (Struct)

    Serialization Context

  • options (Hash) (defaults to: {})
    • include options to be passed to Serializer instance



144
145
146
147
148
149
150
151
152
153
154
# File 'app/filters/compact_restaurants_filter.rb', line 144

def as_json(context, _version, options = {})
  options[:each_serializer] = Api::V5::FeaturedRestaurantSerializer
  data = collections.includes(:translations, :city, :picture,
                              restaurant: [:restaurant_info, :google_review],
                              branch: :restaurants)
  ActiveModelSerializers::SerializableResource.new(data, {
    serialization_context: context,
    serializer: ActiveModel::Serializer::CollectionSerializer,
    adapter: :json_api,
  }.merge(options)).as_json
end


17
18
19
20
# File 'app/filters/compact_restaurants_filter.rb', line 17

def featured_only
  self.collections = collections.where.not(rank: nil).group("#{table_name}.id")
  self
end

#filter_by_city_id(city_id) ⇒ Object



91
92
93
# File 'app/filters/compact_restaurants_filter.rb', line 91

def filter_by_city_id(city_id)
  self.collections = collections.where(city_id: city_id)
end

#filter_by_days_report(collections, reports_scope, start_date = Date.current_date - 3.days) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/filters/compact_restaurants_filter.rb', line 116

def filter_by_days_report(collections, reports_scope, start_date = Date.current_date - 3.days)
  end_date = Date.current_date
  query = reports_scope.between_date(start_date, end_date).
    select('sum(data) as total_data, restaurant_id').
    group('restaurant_id')

  # First query for restaurant_id
  query1 = collections.select("#{CompactRestaurant.table_name}.*, SUM(foo.total_data) as total_data").
    joins('LEFT JOIN restaurants ON restaurants.id = compact_restaurants.restaurant_id').
    joins("LEFT JOIN (#{query.to_sql}) foo ON foo.restaurant_id = restaurants.id")

  # Second query for branch_id
  query2 = collections.select("#{CompactRestaurant.table_name}.*, SUM(foo.total_data) as total_data").
    joins('LEFT JOIN restaurants ON restaurants.branch_id = compact_restaurants.branch_id').
    joins("LEFT JOIN (#{query.to_sql}) foo ON foo.restaurant_id = restaurants.id")

  # Combine the queries using UNION or UNION ALL
  collections_query = query1.union(query2)

  self.collections = order_by_section_sort(query: collections_query).order('total_data desc').
    group("#{CompactRestaurant.table_name}.id")
end

#filter_by_ids(ids) ⇒ Object



86
87
88
89
# File 'app/filters/compact_restaurants_filter.rb', line 86

def filter_by_ids(ids)
  self.collections = CompactRestaurant.where(id: ids) if ids&.present?
  self
end

#filter_restaurant_and_branch_ids(restaurant_ids:, branch_ids:) ⇒ Object



77
78
79
80
81
82
83
84
# File 'app/filters/compact_restaurants_filter.rb', line 77

def filter_restaurant_and_branch_ids(restaurant_ids:, branch_ids:)
  compact_restaurants_direct = CompactRestaurant.where(restaurant_id: restaurant_ids)
  compact_restaurants_via_branch = CompactRestaurant.where(branch_id: branch_ids)

  # we use union to combine the two queries and remove duplicate id
  self.collections = compact_restaurants_direct.union(compact_restaurants_via_branch)
  self
end


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/filters/compact_restaurants_filter.rb', line 95

def filter_trending(collections)
  date = Date.current_date - 1.day
  query = Report.trending_restaurant.date_eq_scope(date)

  # First query for restaurant_id
  query1 = collections.select("#{CompactRestaurant.table_name}.*, foo.data as total_data").
    joins('JOIN restaurants ON restaurants.id = compact_restaurants.restaurant_id').
    joins("JOIN (#{query.to_sql}) foo ON foo.restaurant_id = restaurants.id")

  # Second query for branch_id
  query2 = collections.select("#{CompactRestaurant.table_name}.*, foo.data as total_data").
    joins('JOIN restaurants ON restaurants.branch_id = compact_restaurants.branch_id').
    joins("JOIN (#{query.to_sql}) foo ON foo.restaurant_id = restaurants.id")

  # Combine the queries using UNION
  collections_query = query1.union(query2)

  self.collections = order_by_section_sort(query: collections_query).
    order('total_data desc').group("#{CompactRestaurant.table_name}.id")
end

#init_defaultObject



12
13
14
15
# File 'app/filters/compact_restaurants_filter.rb', line 12

def init_default
  self.collections = CompactRestaurant.where(active: true).group("#{table_name}.id")
  self
end

#set_group_landing_page(id) ⇒ Object



22
23
24
# File 'app/filters/compact_restaurants_filter.rb', line 22

def set_group_landing_page(id)
  self.group_landing_page_id = id
end

#sort_by(type) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/filters/compact_restaurants_filter.rb', line 26

def sort_by(type)
  # backward compatibility for old version, we use hh-search for the phase 2
  if group_landing_page.present? &&
      group_landing_page.group_landing_page_groups.where(rank_sort_type: 'manual').exists?
    type = :group_landing_page
  end

  self.collections = case type.to_s.to_sym
                     when :rank
                       order_by_section_sort(query: collections,
                                             with_report_data: false).order("`#{CompactRestaurant.table_name}`.`rank` ASC")
                     when :new
                       order_by_section_sort(query: collections,
                                             with_report_data: false).order("#{CompactRestaurant.table_name}.start_date DESC")
                     when :top
                       order_by_section_sort(query: collections,
                                             with_report_data: false).order("#{CompactRestaurant.table_name}.top_order ASC")
                     when :trending
                       filter_trending(collections)
                     when :best_selling
                       filter_by_days_report(collections, Report.trending_restaurant, Date.current_date - 30.days)
                     when :staycation
                       filter_by_days_report(collections, Report.top_staycation, Date.current_date - 2.days)
                     when :perfect_for_dates
                       filter_by_days_report(collections, Report.perfect_for_dates, Date.current_date - 6.days)
                     when :birthday_and_celebration
                       filter_by_days_report(collections, Report.birthday_and_celebration, Date.current_date - 6.days)
                     when :top_delivery
                       filter_by_days_report(collections, Report.top_delivery, Date.current_date - 2.days)
                     when :under_1000b
                       filter_by_days_report(collections, Report.under_1000b, Date.current_date - 3.days)
                     when :premium_delivery_offers_hashtag
                       filter_by_days_report(collections, Report.where(name: 'premium_delivery_offers_hashtag'),
                                             Date.current_date - 1.week)
                     when :seafood
                       filter_by_days_report(collections, Report.top_category_seafood, Date.current_date - 3.days)
                     when :japanese
                       filter_by_days_report(collections, Report.top_category_japanese, Date.current_date - 3.days)
                     when :top_dine_in
                       filter_by_days_report(collections, Report.top_dine_in, Date.current_date - 3.days)
                     when :w1_recommendation
                       filter_by_days_report(collections, Report.w1_recommendation, Date.current_date - 7.days)
                     when :group_landing_page
                       sort_by_by_group_landing_page_rank(collections)
                     else
                       # type is based on any type that defined in RestaurantSection
                       filter_by_days_report(collections, Report.where(name: type), Date.current_date - 1.week)
                     end
  self
end