Class: Api::Vendor::V1::LocationsFilter

Inherits:
Object
  • Object
show all
Includes:
Api::V5::PaginationConcernFilter
Defined in:
app/filters/api/vendor/v1/locations_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api::V5::PaginationConcernFilter

#page_number, #per_page

Constructor Details

#initialize(collections = nil) ⇒ LocationsFilter

Returns a new instance of LocationsFilter.



11
12
13
14
15
16
17
18
19
# File 'app/filters/api/vendor/v1/locations_filter.rb', line 11

def initialize(collections = nil)
  unless collections.nil?
    self.collections = collections.select("#{RestaurantTag.table_name}.*").
      select('COUNT(*) AS total_restaurants').
      joins(:restaurants).where('title_en LIKE ?', 'Location:%').where(restaurants: { active: true }).
      where('date(restaurants.expiry_date) >= ?', Time.thai_time.to_date).
      group('restaurant_tags.id')
  end
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



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

def collections
  @collections
end

Instance Method Details

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



55
56
57
58
59
60
61
62
# File 'app/filters/api/vendor/v1/locations_filter.rb', line 55

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



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

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

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

#init_defaultObject



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

def init_default
  self.collections = RestaurantTag.select("#{RestaurantTag.table_name}.*").
    select('COUNT(*) AS total_restaurants').
    joins(:restaurants).where('title_en LIKE ?', 'Location:%').
    where(restaurants: { active: true }).
    where('date(restaurants.expiry_date) >= ?', Time.thai_time.to_date).
    group('restaurant_tags.id')
  self
end

#sort_by(key) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/filters/api/vendor/v1/locations_filter.rb', line 38

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