Class: Api::V5::ReservationsFilter

Inherits:
Object
  • Object
show all
Defined in:
app/filters/api/v5/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/v5/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/v5/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/v5/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/v5/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::V5::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



105
106
107
108
109
110
111
112
# File 'app/filters/api/v5/reservations_filter.rb', line 105

def as_json(context, _version, options = {})
  options[:each_serializer] = Api::V5::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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/filters/api/v5/reservations_filter.rb', line 120

def build_collections
  _init_default
  _need_review
  _active_only
  _not_pending
  _pending
  _pending_confirmation
  _group_booking
  _exclude_no_package
  _filter_by_dining_time
  _order_by
  _paginate
  collections
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)


77
78
79
80
81
82
83
84
# File 'app/filters/api/v5/reservations_filter.rb', line 77

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_package(val) ⇒ Object



114
115
116
117
# File 'app/filters/api/v5/reservations_filter.rb', line 114

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

#group_booking(val) ⇒ Api::V5::ReservationsFilter

Returns self for method chaining

Parameters:

  • val (Boolean)

    Filter reservations by group booking status

Returns:



65
66
67
68
# File 'app/filters/api/v5/reservations_filter.rb', line 65

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

#init_default(user) ⇒ Object

Parameters:

  • user (User)

    User instance



17
18
19
20
21
22
# File 'app/filters/api/v5/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/v5/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/v5/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/v5/reservations_filter.rb', line 29

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

  config[:order_by] = order
  self
end

#page_number(number) ⇒ Object



86
87
88
89
# File 'app/filters/api/v5/reservations_filter.rb', line 86

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/v5/reservations_filter.rb', line 46

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

#pending_confirmation(val) ⇒ Api::V5::ReservationsFilter

Returns self for method chaining

Parameters:

  • val (Boolean)

    Filter reservations by pending confirmation status

Returns:



58
59
60
61
# File 'app/filters/api/v5/reservations_filter.rb', line 58

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

#per_page(number) ⇒ Object



91
92
93
94
# File 'app/filters/api/v5/reservations_filter.rb', line 91

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