Class: SpreeCmCommissioner::SeatLayouts::Update

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/seat_layouts/update.rb

Overview

Shared by both dashboards that edit a seat layout (organizer's event-scoped controller, shared_console's trip-scoped one) — the nested-attribute remapping, section/block regrouping and total_seats bookkeeping is identical either way; only the URL-building and (for events) the background-attachment handling differ, and those stay in each controller.

Instance Method Summary collapse

Instance Method Details

#call(seat_layout:, attributes:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/spree_cm_commissioner/seat_layouts/update.rb', line 10

def call(seat_layout:, attributes:)
  attributes = attributes.deep_dup
  # `background_svg_url` is derived (SeatLayout#background_svg_url), not a writable column —
  # the event-scoped caller strips/handles it separately (background removal); other callers'
  # editor payload includes it unconditionally, so drop it here rather than crashing on assign.
  attributes.delete(:background_svg_url)

  ActiveRecord::Base.transaction do
    create_sections_for_existing_blocks!(seat_layout, attributes)
    ungroup_blocks_from_sections!(seat_layout, attributes)

    attributes[:top_level_blocks_attributes] = attributes.delete(:top_level_blocks) if attributes.key?(:top_level_blocks)

    if attributes.key?(:seat_sections)
      attributes[:seat_sections_attributes] = attributes.delete(:seat_sections)

      attributes[:seat_sections_attributes].each do |section|
        section[:blocks_attributes] = section.delete(:blocks) if section.key?(:blocks)
      end
    end

    seat_layout.assign_attributes(attributes)
    seat_layout.seat_sections.each { |section| section.total_seats = section.blocks.select(&:seatable?).count }
    seat_layout.total_seats = seat_layout.seat_sections.sum(&:total_seats) + seat_layout.top_level_blocks.select(&:seatable?).count
    seat_layout.save!
  end

  success(seat_layout)
rescue StandardError => e
  failure(seat_layout, e.message)
end