Class: SpreeCmCommissioner::Seats::BlocksReserver
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Seats::BlocksReserver
- Defined in:
- app/models/spree_cm_commissioner/seats/blocks_reserver.rb
Instance Method Summary collapse
-
#initialize(line_item_ids:, reserve_by: nil) ⇒ BlocksReserver
constructor
A new instance of BlocksReserver.
-
#reserve_blocks! ⇒ Object
Mark blocks as reserved for the given line items.
- #reserve_specific_block!(inventory_item, guest) ⇒ Object
Constructor Details
#initialize(line_item_ids:, reserve_by: nil) ⇒ BlocksReserver
Returns a new instance of BlocksReserver.
4 5 6 7 |
# File 'app/models/spree_cm_commissioner/seats/blocks_reserver.rb', line 4 def initialize(line_item_ids:, reserve_by: nil) @line_item_ids = line_item_ids @reserve_by = reserve_by end |
Instance Method Details
#reserve_blocks! ⇒ Object
Mark blocks as reserved for the given line items.
It ensures:
-
Raises an error if already reserved by same guest or other guest.
-
Raises an error if currently on_hold by someone else.
-
Otherwise, proceeds to reserve the given blocks.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/spree_cm_commissioner/seats/blocks_reserver.rb', line 15 def reserve_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 ActiveRecord::Base.transaction do guests_with_blocks.each do |guest| inventory_items.each do |inventory_item| reserve_specific_block!(inventory_item, guest) end end end end |
#reserve_specific_block!(inventory_item, guest) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/spree_cm_commissioner/seats/blocks_reserver.rb', line 29 def reserve_specific_block!(inventory_item, guest) reserved_block = SpreeCmCommissioner::ReservedBlock.find_or_initialize_by(inventory_item_id: inventory_item.id, block_id: guest.block_id) raise Errors::BlocksAreReservedByOtherGuest if reserved_block.reserved? && reserved_block.guest_id != guest.id raise Errors::BlocksAreReservedBySameGuest if reserved_block.reserved? && reserved_block.guest_id == guest.id raise Errors::BlocksAreOnHoldByOtherGuest if reserved_block.active_on_hold? && reserved_block.guest_id != guest.id # mark the block as reserved if not on_hold or lock by anyone but already expired reserved_block.assign_attributes( guest: guest, status: :reserved, expired_at: nil, inventory_date: inventory_item.inventory_date, updated_by: @reserve_by ) raise Errors::UnableToSaveReservedBlockRecord unless reserved_block.save end |