Class: SpreeCmCommissioner::TripQuery
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::TripQuery
- Defined in:
- app/queries/spree_cm_commissioner/trip_query.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#destination_id ⇒ Object
readonly
Returns the value of attribute destination_id.
-
#number_of_guests ⇒ Object
readonly
Returns the value of attribute number_of_guests.
-
#origin_id ⇒ Object
readonly
Returns the value of attribute origin_id.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#route_type ⇒ Object
readonly
Returns the value of attribute route_type.
-
#vendor_id ⇒ Object
readonly
Returns the value of attribute vendor_id.
Instance Method Summary collapse
- #call ⇒ Object
- #direct_trips ⇒ Object
-
#initialize(origin_id:, destination_id:, date:, route_type: nil, vendor_id: nil, number_of_guests: nil, params: {}) ⇒ TripQuery
constructor
rubocop:disable Metrics/ParameterLists.
- #product_inventory_totals ⇒ Object
Constructor Details
#initialize(origin_id:, destination_id:, date:, route_type: nil, vendor_id: nil, number_of_guests: nil, params: {}) ⇒ TripQuery
rubocop:disable Metrics/ParameterLists
5 6 7 8 9 10 11 12 13 14 15 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 5 def initialize(origin_id:, destination_id:, date:, route_type: nil, vendor_id: nil, number_of_guests: nil, params: {}) # rubocop:disable Metrics/ParameterLists @origin_id = origin_id @destination_id = destination_id @date = date.to_date == Time.zone.now.to_date ? Time.zone.now : Time.zone.parse(date.to_s) @vendor_id = vendor_id @number_of_guests = number_of_guests || 1 @route_type = route_type @params = params @page = (params[:page] || 1).to_i @per_page = params[:per_page] end |
Instance Attribute Details
#date ⇒ Object (readonly)
Returns the value of attribute date.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 3 def date @date end |
#destination_id ⇒ Object (readonly)
Returns the value of attribute destination_id.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 3 def destination_id @destination_id end |
#number_of_guests ⇒ Object (readonly)
Returns the value of attribute number_of_guests.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 3 def number_of_guests @number_of_guests end |
#origin_id ⇒ Object (readonly)
Returns the value of attribute origin_id.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 3 def origin_id @origin_id end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 3 def params @params end |
#route_type ⇒ Object (readonly)
Returns the value of attribute route_type.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 3 def route_type @route_type end |
#vendor_id ⇒ Object (readonly)
Returns the value of attribute vendor_id.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 3 def vendor_id @vendor_id end |
Instance Method Details
#call ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 17 def call return Kaminari.paginate_array([]) if date.to_date < Date.current paginated_relation = direct_trips return Kaminari.paginate_array([]) if paginated_relation.empty? unique_trips = paginated_relation.uniq(&:id) results_array = unique_trips.map do |trip| result = build_trip_result(trip) SpreeCmCommissioner::TripQueryResult.new([result]) end Kaminari.paginate_array( results_array, total_count: unique_trips.size, limit: unique_trips.size, offset: paginated_relation.offset_value ) end |
#direct_trips ⇒ Object
37 38 39 40 41 42 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 37 def direct_trips result = trip_scope result = result.where({ vendor_id: vendor_id }.compact) result = result.where(route_type: route_type) if route_type.present? paginate(result) end |
#product_inventory_totals ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/queries/spree_cm_commissioner/trip_query.rb', line 44 def product_inventory_totals Spree::Product .select( 'spree_products.id AS product_id, SUM(cm_inventory_items.max_capacity) AS max_capacity, SUM(cm_inventory_items.quantity_available) AS quantity_available' ) .joins(variants: :inventory_items) .where('cm_inventory_items.inventory_date = ? AND cm_inventory_items.quantity_available >= ?', @date.to_date, @number_of_guests) .group('spree_products.id') end |