Class: Api::Vendor::V1::RestaurantsFilter

Inherits:
Object
  • Object
show all
Defined in:
app/filters/api/vendor/v1/restaurants_filter.rb

Constant Summary collapse

TAG_CONFIG_FILTER =
%i[by_cuisine_ids by_location_ids].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collections = nil) ⇒ RestaurantsFilter

Returns a new instance of RestaurantsFilter.



12
13
14
15
16
17
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 12

def initialize(collections = nil)
  self.collections = collections unless collections.nil?
  @valid = true
  @config = {}
  self.show_available_restaurants_only = false
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



8
9
10
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 8

def collections
  @collections
end

#error_messageObject

Returns the value of attribute error_message.



8
9
10
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 8

def error_message
  @error_message
end

#show_available_restaurants_onlyObject

Returns the value of attribute show_available_restaurants_only.



8
9
10
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 8

def show_available_restaurants_only
  @show_available_restaurants_only
end

#total_restaurantsObject

Returns the value of attribute total_restaurants.



8
9
10
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 8

def total_restaurants
  @total_restaurants
end

Instance Method Details

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 98

def as_json(_context, _version, options = {})
  coll = if options[:each_serializer].blank?
           options[:each_serializer] = Api::Vendor::V1::RestaurantSerializer
           build_collections.includes(:restaurant_external)
         end

  serializer_options = {
    serialization_context: options[:serialization_context],
    minor_version: options[:minor_version],
    params: { package_types: config[:by_package_type] },
  }

  include_options = options[:include]
  if include_options.present? && include_options.include?('pictures')
    serializer_options[:include] = ['pictures']
  end

  serializer = options[:each_serializer].new(coll, serializer_options)
  serializer.as_json
end

#build_collectionsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 119

def build_collections
  if @restaurant_ids.present?
    self.collections = ::Restaurant.active.not_expired.
      includes(:translations, :restaurant_tags).
      where(id: @restaurant_ids)
  else
    self.collections = default_collections if collections.blank?
    build_by_city_id
    build_by_package_type
    build_by_filter_price
    build_by_name_like
    build_by_restaurant_tags
    generate_total_restaurants
    build_sort_by
    paginate
    collections
  end
end

#by_city_id(city_id) ⇒ Object



43
44
45
46
47
48
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 43

def by_city_id(city_id)
  return self if city_id.blank?

  config[:by_city_id] = city_id
  self
end

#by_name_like(name) ⇒ Object



74
75
76
77
78
79
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 74

def by_name_like(name)
  return self if name.blank?

  config[:by_name_like] = name
  self
end

#by_package_type(package_type) ⇒ Object



65
66
67
68
69
70
71
72
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 65

def by_package_type(package_type)
  return self if package_type.blank?

  package_type_arr = package_type.split(',')

  config[:by_package_type] = package_type_arr
  self
end

#by_price_filter(price_filter) ⇒ Object



91
92
93
94
95
96
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 91

def by_price_filter(price_filter)
  return self if price_filter.blank?

  config[:price_filter] = price_filter
  self
end

#by_service_type(service_type) ⇒ Object



58
59
60
61
62
63
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 58

def by_service_type(service_type)
  return self if service_type.blank?

  config[:by_service_type] = service_type
  self
end

#page_number(number) ⇒ Object



33
34
35
36
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 33

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

#per_page(number) ⇒ Object



38
39
40
41
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 38

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

#restaurant_ids=(restaurant_ids) ⇒ Object



24
25
26
27
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 24

def restaurant_ids=(restaurant_ids)
  @restaurant_ids = restaurant_ids
  self
end

#sort_by(key) ⇒ Object

`new` or `top` / `popular`



51
52
53
54
55
56
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 51

def sort_by(key)
  return self if key.blank?

  config[:sort_by] = key
  self
end

#use_default_collectionsObject



19
20
21
22
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 19

def use_default_collections
  self.collections = default_collections.preload(pictures: :taggings)
  self
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/filters/api/vendor/v1/restaurants_filter.rb', line 29

def valid?
  @valid
end