Class: Api::Vendor::V1::LocationsFilter
- Inherits:
-
Object
- Object
- Api::Vendor::V1::LocationsFilter
- Includes:
- Api::V5::PaginationConcernFilter
- Defined in:
- app/filters/api/vendor/v1/locations_filter.rb
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
Methods included from Api::V5::PaginationConcernFilter
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
#collections ⇒ Object
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, = {}) [:each_serializer] = Api::Vendor::V1::LocationSerializer ActiveModelSerializers::SerializableResource.new(collections, { serialization_context: context, serializer: ActiveModel::Serializer::CollectionSerializer, adapter: :json_api, }.merge()).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_default ⇒ Object
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 |