Class: Katalyst::Content::DuplicatesAssociations::DupMany

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/katalyst/content/duplicates_associations.rb

Overview

Duplicates a has_many association from source to target. Public API: can be used directly for models that want to duplicate a specific association without including the concern, e.g. DuplicatesAssociations::DupMany.new(:notes).apply(source, self)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ DupMany

Returns a new instance of DupMany.



62
63
64
# File 'app/models/concerns/katalyst/content/duplicates_associations.rb', line 62

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



60
61
62
# File 'app/models/concerns/katalyst/content/duplicates_associations.rb', line 60

def name
  @name
end

Instance Method Details

#apply(source, target) ⇒ Object



66
67
68
69
70
# File 'app/models/concerns/katalyst/content/duplicates_associations.rb', line 66

def apply(source, target)
  records = source.public_send(name).reject(&:marked_for_destruction?).map(&:dup)

  target.public_send("#{name}=", records) if records.any?
end