Class: Api::V5::LocationsFilter

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

Direct Known Subclasses

Aoa::V1::LocationsFilter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PaginationConcernFilter

#page_number, #per_page

Constructor Details

#initialize(collections = nil) ⇒ LocationsFilter

Returns a new instance of LocationsFilter.



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

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

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

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



5
6
7
# File 'app/filters/api/v5/locations_filter.rb', line 5

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



64
65
66
67
68
69
70
71
72
73
74
# File 'app/filters/api/v5/locations_filter.rb', line 64

def as_json(context, _version, options = {})
  options = {
    each_serializer: Api::V5::LocationSerializer,
    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



32
33
34
35
36
37
38
# File 'app/filters/api/v5/locations_filter.rb', line 32

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

#init_defaultObject



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

def init_default
  self.collections = RestaurantTag.select("#{RestaurantTag.table_name}.*,
      COALESCE(rtt.total_restaurants, 0) AS total_restaurants").
    joins('LEFT JOIN restaurant_tags_totals AS rtt ON rtt.restaurant_tag_id = restaurant_tags.id').
    where_category('location').
    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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/filters/api/v5/locations_filter.rb', line 41

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('total_restaurants ASC')
  when 'total_restaurants_desc'
    self.collections = collections.order('total_restaurants DESC')
  end

  self
end