Module: Katalyst::Content::DuplicatesAssociations

Extended by:
ActiveSupport::Concern
Included in:
Item
Defined in:
app/models/concerns/katalyst/content/duplicates_associations.rb

Overview

Copies declared associations when a record is duplicated, e.g. for copy-on-write. Intended for owned detail records that extend an STI item with attributes beyond the shared table:

has_one :detail, as: :detailable, autosave: true, dependent: :destroy
accepts_nested_attributes_for :detail, update_only: true
duplicates_association :detail

Unsaved changes on the source's association are carried over to the duplicate. Records marked for destruction are dropped.

Defined Under Namespace

Classes: DupMany, DupOne

Constant Summary collapse

DUPLICATORS =
{
  has_one:  DupOne,
  has_many: DupMany,
}.freeze

Instance Method Summary collapse

Instance Method Details

#initialize_dup(source) ⇒ Object



28
29
30
31
32
33
34
35
# File 'app/models/concerns/katalyst/content/duplicates_associations.rb', line 28

def initialize_dup(source)
  super

  duplicated_associations.each do |name|
    macro = self.class.reflect_on_association(name).macro
    DUPLICATORS.fetch(macro).new(name).apply(source, self)
  end
end