Class: SpreeCmCommissioner::Seats::BlocksHolder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/seats/blocks_holder.rb

Constant Summary collapse

HOLD_DURATION =
ENV.fetch('HOLD_DURATION_IN_MINUTES', '8').to_i.minutes

Instance Method Summary collapse

Constructor Details

#initialize(line_item_ids:, hold_by: nil) ⇒ BlocksHolder

Returns a new instance of BlocksHolder.



6
7
8
9
# File 'app/models/spree_cm_commissioner/seats/blocks_holder.rb', line 6

def initialize(line_item_ids:, hold_by: nil)
  @line_item_ids = line_item_ids
  @hold_by = hold_by
end

Instance Method Details

#hold_blocks!Object

Hold blocks for the given line items, preventing duplicate block selection.

It ensures:

  • Raises an error if already reserved.

  • Raises an error if currently on_hold by someone else.

  • Allows re-locking if already on_hold by the same guest.

  • Otherwise, proceeds to lock the given blocks.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/spree_cm_commissioner/seats/blocks_holder.rb', line 18

def hold_blocks!
  line_items = Spree::LineItem.where(id: @line_item_ids).includes(:inventory_items, guests_with_blocks: :block)
  guests_with_blocks = line_items.flat_map(&:guests_with_blocks).compact
  inventory_items = line_items.flat_map(&:inventory_items).compact

  reserved_blocks = []

  ActiveRecord::Base.transaction do
    guests_with_blocks.each do |guest|
      next unless guest.can_hold_block?

      inventory_items.each do |inventory_item|
        reserved_blocks << hold_specific_block!(inventory_item, guest)
      end
    end
  end

  reserved_blocks
end