Class: Api::V5::LocationsFilter
- Inherits:
-
Object
- Object
- Api::V5::LocationsFilter
- Includes:
- PaginationConcernFilter
- Defined in:
- app/filters/api/v5/locations_filter.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#collections ⇒ Object
Returns the value of attribute collections.
Instance Method Summary collapse
- #as_json(context, _version, options = {}) ⇒ Object
- #by_city_id(city_id) ⇒ Object
- #init_default ⇒ Object
-
#initialize(collections = nil) ⇒ LocationsFilter
constructor
A new instance of LocationsFilter.
-
#sort_by(key) ⇒ Object
`name_asc`, `name_desc`, `rank_asc` or `rank_desc.
Methods included from PaginationConcernFilter
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
#collections ⇒ Object
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
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, = {}) = { 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()).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_default ⇒ Object
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 |