Sha256: 5e065a50c0967b35a48d88639fe1484f439c17631312e4a1a3a7a7268ff1e8e0

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

class ActiveShepherd::Methods::ApplyChanges < ActiveShepherd::ApplyMethod
  def apply_changes
    aggregate.model.mark_for_destruction if destroy?
    traverse!
  end

  def handle_attribute(attribute_name, before_and_after)
    before, after = before_and_after
    current_value = aggregate.model.send(attribute_name)

    before = aggregate.deserialize_value(attribute_name, before)
    after  = aggregate.deserialize_value(attribute_name, after)

    unless current_value == before
      raise ::ActiveShepherd::BadChangeError, "Expecting "\
        "`#{attribute_name} to be `#{before.inspect}', not "\
        "`#{current_value.inspect}'"
    end

    aggregate.model.send "#{attribute_name}=", after
  end

  def handle_has_many_association(reflection, collection_changes)
    apply_changes_to_has_many_association reflection, collection_changes
  end

  def handle_has_one_association(reflection, changes)
    associated_model = aggregate.model.public_send reflection.name
    self.class.apply_changes recurse(associated_model, reflection.foreign_key),
      changes
  end

private

  def apply_changes_to_has_many_association(reflection, collection_changes)
    association = aggregate.model.send reflection.name

    collection_changes.each do |index, changes|
      # FIXME
      association.build until association.size >= (index + 1)
      # /FIXME

      associated_model = association[index]
      if associated_model.nil?
        raise ::ActiveShepherd::BadChangeError,
          "Can't find record ##{index}"
      end
      self.class.apply_changes recurse(associated_model, reflection.foreign_key),
        changes
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activeshepherd-0.8.4 lib/active_shepherd/methods/apply_changes.rb
activeshepherd-0.8.3 lib/active_shepherd/methods/apply_changes.rb
activeshepherd-0.8.2 lib/active_shepherd/methods/apply_changes.rb