Class: Api::V5::CuisinesFilter

Inherits:
Object
  • Object
show all
Includes:
PaginationConcernFilter
Defined in:
app/filters/api/v5/cuisines_filter.rb

Direct Known Subclasses

Aoa::CuisinesFilter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PaginationConcernFilter

#page_number, #per_page

Constructor Details

#initialize(collections = nil) ⇒ CuisinesFilter

Returns a new instance of CuisinesFilter.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/filters/api/v5/cuisines_filter.rb', line 9

def initialize(collections = nil)
  @config = {}

  unless collections.nil?
    self.collections = collections.select("#{RestaurantTag.table_name}.*,
        rtt.total_restaurants AS total_restaurants").
      joins('LEFT JOIN restaurant_tags_totals AS rtt ON rtt.restaurant_tag_id = restaurant_tags.id').
      where_category('cuisine').
      where('rtt.total_restaurants > 0').
      group("#{RestaurantTag.table_name}.id")
  end
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



6
7
8
# File 'app/filters/api/v5/cuisines_filter.rb', line 6

def collections
  @collections
end

Instance Method Details

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

Examples:

options = {}

Parameters:

  • context (Struct)

    Serialization Context

  • version (Integer)

    ApiV5::Constants::DEFAULT_MINOR_VERSION

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



76
77
78
79
80
81
82
83
84
85
86
# File 'app/filters/api/v5/cuisines_filter.rb', line 76

def as_json(context, _version, options = {})
  options = {
    each_serializer: Api::V5::CuisineSerializer,
    city_id: @config[:city_id],
  }
  ActiveModelSerializers::SerializableResource.new(collections, {
    serialization_context: context,
    serializer: ActiveModel::Serializer::CollectionSerializer,
    adapter: :json_api,
  }.merge(options)).as_json
end

#by_city_id(city_id) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/filters/api/v5/cuisines_filter.rb', line 33

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

  @config[:city_id] = city_id

  self.collections = collections.where('rtt.city_id = ?', city_id)

  self
end

#by_ids(ids) ⇒ Object



43
44
45
46
47
48
49
50
# File 'app/filters/api/v5/cuisines_filter.rb', line 43

def by_ids(ids)
  return self if ids.blank?

  cuisine_ids = ids.split(',')

  self.collections = collections.where(id: cuisine_ids)
  self
end

#init_defaultObject



22
23
24
25
26
27
28
29
30
31
# File 'app/filters/api/v5/cuisines_filter.rb', line 22

def init_default
  self.collections = RestaurantTag.select("#{RestaurantTag.table_name}.*,
      rtt.total_restaurants AS total_restaurants").
    joins('LEFT JOIN restaurant_tags_totals AS rtt ON rtt.restaurant_tag_id = restaurant_tags.id').
    where_category('cuisine').
    where('rtt.total_restaurants > 0').
    group("#{RestaurantTag.table_name}.id")

  self
end

#sort_by(key) ⇒ Object

`name_asc`, `name_desc`, `rank_asc` or `rank_desc



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/filters/api/v5/cuisines_filter.rb', line 53

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

  case key.to_s
  when 'name_asc'
    self.collections = collections.order("title_#{MyLocaleManager.normalize_locale} ASC")
  when 'name_desc'
    self.collections = collections.order("title_#{MyLocaleManager.normalize_locale} DESC")
  when 'total_restaurants_asc'
    self.collections = collections.order('rtt.total_restaurants ASC')
  when 'total_restaurants_desc'
    self.collections = collections.order('rtt.total_restaurants DESC')
  end

  self
end