Class: CompositeContent::Model::Builder::Slot

Inherits:
Base
  • Object
show all
Defined in:
lib/composite_content/model/builder/slot.rb

Overview

Class builder for Slot models

Every composite content slot is defined by its own class, subclass of CompositeContent::Slot. This allows to use of a dedicated block class where we can set validations on block types and to rely on ActiveRecord default behaviors for Single Table Inheritance models (as adding a constraint on type to queries).

Constant Summary

Constants inherited from Base

Base::BLOCK_SUFFIX, Base::SLOT_SUFFIX

Instance Method Summary collapse

Methods inherited from Base

build, #build

Instance Method Details

#build_class(parent, association, _types = []) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/composite_content/model/builder/slot.rb', line 15

def build_class(parent, association, _types = [])
  Class.new(CompositeContent::Slot).tap do |klass|
    klass.has_many :blocks,
                   -> { order(position: :asc) },
                   class_name: classname_for_block(parent, association),
                   foreign_key: :slot_id,
                   inverse_of: :slot,
                   dependent: :destroy

    klass.accepts_nested_attributes_for :blocks, allow_destroy: true, reject_if: :all_blank
    klass.validates_associated :blocks
  end
end