Class: Api::V5::CuisinesFilter
- Inherits:
-
Object
- Object
- Api::V5::CuisinesFilter
- Includes:
- PaginationConcernFilter
- Defined in:
- app/filters/api/v5/cuisines_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
- #by_ids(ids) ⇒ Object
- #init_default ⇒ Object
-
#initialize(collections = nil) ⇒ CuisinesFilter
constructor
A new instance of CuisinesFilter.
-
#sort_by(key) ⇒ Object
`name_asc`, `name_desc`, `rank_asc` or `rank_desc.
Methods included from PaginationConcernFilter
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
#collections ⇒ Object
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
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, = {}) = { 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()).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_default ⇒ Object
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 |