Class: Api::Aoa::V1::ReservationsFilter

Inherits:
Object
  • Object
show all
Defined in:
app/filters/api/aoa/v1/reservations_filter.rb

Overview

Responsible to filter reservations data based on user ID

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collections = nil) ⇒ ReservationsFilter

Returns a new instance of ReservationsFilter.

Parameters:

  • collections (Reservation) (defaults to: nil)

    Reservation relations



11
12
13
14
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 11

def initialize(collections = nil)
  self.collections = collections unless collections.nil?
  @config = {}
end

Instance Attribute Details

#collectionsObject

Returns the value of attribute collections.



8
9
10
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 8

def collections
  @collections
end

#error_messageObject

Returns the value of attribute error_message.



8
9
10
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 8

def error_message
  @error_message
end

Instance Method Details

#active_only(val) ⇒ Object



36
37
38
39
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 36

def active_only(val)
  config[:active_only] = val
  self
end

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

if preview_mode == true, then use Api::Aoa::V1::RestaurantPreviewSerializer

Examples:

options = {
  preview_mode: true || false
}

Parameters:

  • context (Struct)

    Serialization Context

  • version (Integer)

    ApiV5::Constants::DEFAULT_MINOR_VERSION

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 100

def as_json(context, _version, options = {})

  options[:each_serializer] = Api::Aoa::V1::ReservationSerializer
  ActiveModelSerializers::SerializableResource.new(collections, {
    serialization_context: context,
    serializer: ActiveModel::Serializer::CollectionSerializer,
    adapter: :json_api,
  }.merge(options)).as_json


  # options[:each_serializer] = Api::Aoa::V1::ReservationSerializer
  # options[:user_id] = config[:user].id if config[:user]
  # ActiveModelSerializers::SerializableResource.new(build_collections,
  #                                                  { serialization_context: context,
  #                                                    serializer: ActiveModel::Serializer::CollectionSerializer,
  #                                                    adapter: :json_api }.merge(options)).as_json
end

#build_collectionsObject

get desired reservations from database



124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 124

def build_collections
  _init_default
  _need_review
  _active_only
  _not_pending
  _pending
  _exclude_no_package
  _filter_by_dining_time
  _order_by
  _paginate
  collections
end

#by_aoa_user_id(aoa_user_id) ⇒ Object



82
83
84
85
86
87
88
89
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 82

def by_aoa_user_id(aoa_user_id)
  return self if aoa_user_id.blank?
  self.collections = collections.joins(:aoa_reservation)
    .where(aoa_reservations: {
      aoa_user_id: aoa_user_id,
    })
  self
end

#dining_time(time_x, operator, time_y = nil) ⇒ Object

Parameters:

  • time_x (Time)
  • operator (String)

    one of

    • gt

    • gte

    • lt

    • lte

  • time_y (Time) (defaults to: nil)


63
64
65
66
67
68
69
70
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 63

def dining_time(time_x, operator, time_y = nil)
  config[:dining_time] = {
    time_x: time_x,
    operator: operator,
    time_y: time_y
  }
  self
end

#exclude_no_packageObject



118
119
120
121
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 118

def exclude_no_package
  config[:exclude_no_package] = true
  self
end

#init_default(user) ⇒ Object

Parameters:

  • user (User)

    User instance



17
18
19
20
21
22
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 17

def init_default(user)
  return self unless user.is_a?(::User)

  config[:user] = user
  self
end

#need_review(val) ⇒ Object



51
52
53
54
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 51

def need_review(val)
  config[:need_review] = val
  self
end

#not_pending(val) ⇒ Object



41
42
43
44
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 41

def not_pending(val)
  config[:not_pending] = val
  self
end

#order_by(order) ⇒ Object

Parameters:

  • order (String)

    one of

    • id desc

    • id asc

    • dining_time asc

    • dining_time desc



29
30
31
32
33
34
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 29

def order_by(order)
  return self if order.blank?

  config[:order_by] = order
  self
end

#page_number(number) ⇒ Object



72
73
74
75
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 72

def page_number(number)
  config[:page_number] = number.to_i if number.to_i.positive?
  self
end

#pending(val) ⇒ Object



46
47
48
49
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 46

def pending(val)
  config[:pending] = val
  self
end

#per_page(number) ⇒ Object



77
78
79
80
# File 'app/filters/api/aoa/v1/reservations_filter.rb', line 77

def per_page(number)
  config[:per_page] = number.to_i if number.to_i.positive?
  self
end