Class: SpreeCmCommissioner::Accommodations::FindVariant

Inherits:
Object
  • Object
show all
Defined in:
app/finders/spree_cm_commissioner/accommodations/find_variant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vendor_id:, from_date:, to_date:, number_of_adults:, number_of_kids:) ⇒ FindVariant

Returns a new instance of FindVariant.



6
7
8
9
10
11
# File 'app/finders/spree_cm_commissioner/accommodations/find_variant.rb', line 6

def initialize(vendor_id:, from_date:, to_date:, number_of_adults:, number_of_kids:)
  @vendor_id = vendor_id
  @from_date = from_date
  @to_date = to_date
  @number_of_guests = number_of_adults.to_i + number_of_kids.to_i
end

Instance Attribute Details

#from_dateObject (readonly)

Returns the value of attribute from_date.



4
5
6
# File 'app/finders/spree_cm_commissioner/accommodations/find_variant.rb', line 4

def from_date
  @from_date
end

#number_of_guestsObject (readonly)

Returns the value of attribute number_of_guests.



4
5
6
# File 'app/finders/spree_cm_commissioner/accommodations/find_variant.rb', line 4

def number_of_guests
  @number_of_guests
end

#to_dateObject (readonly)

Returns the value of attribute to_date.



4
5
6
# File 'app/finders/spree_cm_commissioner/accommodations/find_variant.rb', line 4

def to_date
  @to_date
end

#vendor_idObject (readonly)

Returns the value of attribute vendor_id.



4
5
6
# File 'app/finders/spree_cm_commissioner/accommodations/find_variant.rb', line 4

def vendor_id
  @vendor_id
end

Instance Method Details

#executeObject



13
14
15
16
17
18
19
20
21
22
23
# File 'app/finders/spree_cm_commissioner/accommodations/find_variant.rb', line 13

def execute
  Spree::Variant
    .joins(:inventory_items)
    .where(vendor_id: vendor_id)
    .where(inventory_items: { inventory_date: stay_dates })
    .where('CAST(public_metadata->\'cm_options\'->>\'number-of-adults\' AS INTEGER) +
            CAST(public_metadata->\'cm_options\'->>\'number-of-kids\' AS INTEGER) >= ?', number_of_guests
    )
    .where('inventory_items.quantity_available > 0')
    .distinct
end