Class: Api::V5::RestaurantPackagesFilter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PaginationConcernFilter

#page_number, #per_page

Constructor Details

#initialize(collections = nil) ⇒ RestaurantPackagesFilter

Returns a new instance of RestaurantPackagesFilter.



9
10
11
# File 'app/filters/api/v5/restaurant_packages_filter.rb', line 9

def initialize(collections = nil)
  self.collections = collections unless collections.nil?
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



7
8
9
# File 'app/filters/api/v5/restaurant_packages_filter.rb', line 7

def collections
  @collections
end

Instance Method Details

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

Examples:

options = {
  include: ['restaurant']
}

Parameters:

  • context (Struct)

    Serialization Context

  • version (Integer)

    ApiV5::Constants::DEFAULT_MINOR_VERSION

  • options (Hash) (defaults to: {})
    • include options to be passed to Serializer instance



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

def as_json(context, version, options = {})
  options[:each_serializer] = Api::V5::RestaurantPackageSerializer
  col = collections.compact
  if col.present?
    ActiveModelSerializers::SerializableResource.new(col,
                                                     { serialization_context: context,
                                                       serializer: ActiveModel::Serializer::CollectionSerializer,
                                                       adapter: :json_api,
                                                       minor_version: version }.merge(options)
                                                    ).as_json
  else
    { data: [] }
  end
end

#by_city_id(city_id) ⇒ Object



35
36
37
38
39
# File 'app/filters/api/v5/restaurant_packages_filter.rb', line 35

def by_city_id(city_id)
  return self if city_id.blank?
  self.collections = collections.where("#{Restaurant.table_name}.city_id = ?", city_id)
  self
end

#by_updated_at(timestamp) ⇒ Object



29
30
31
32
33
# File 'app/filters/api/v5/restaurant_packages_filter.rb', line 29

def by_updated_at(timestamp)
  return self if timestamp.blank?
  self.collections = collections.where("#{HhPackage::RestaurantPackage.table_name}.updated_at >= ?", timestamp)
  self
end

#exclude_nil_rank(yes) ⇒ Object



49
50
51
52
# File 'app/filters/api/v5/restaurant_packages_filter.rb', line 49

def exclude_nil_rank(yes)
  self.collections = collections.where("#{HhPackage::RestaurantPackage.table_name}.rank IS NOT NULL") if yes
  self
end

#init_defaultObject



13
14
15
16
17
18
19
20
21
22
# File 'app/filters/api/v5/restaurant_packages_filter.rb', line 13

def init_default
  today = Time.zone.now.to_date

  self.collections = HhPackage::RestaurantPackage.joins(:restaurant)
                                                 .where(deleted_at: nil, active: true).where('end_date >= ?', today)
                                                 .where('restaurants.active = ?', true)
                                                 .includes(package: %i[translations pricing],
                                                           restaurant: %i[translations restaurant_tags])
  self
end

#remove_hah_packagesObject



24
25
26
27
# File 'app/filters/api/v5/restaurant_packages_filter.rb', line 24

def remove_hah_packages
  self.collections = collections.where.not(package_type: HhPackage::Package::HungryAtHome.to_s)
  self
end

#sort_by(key) ⇒ Object

currently only support sort by `rank`



42
43
44
45
46
47
# File 'app/filters/api/v5/restaurant_packages_filter.rb', line 42

def sort_by(key)
  return self if key.blank?
  self.collections = collections.order("#{HhPackage::RestaurantPackage.table_name}.rank ASC") if key.to_s == 'rank'

  self
end