Sha256: 9832f8a9692dd2facec6127aa674ca4a6931ee905e97bc07fb59b48c73b1004b

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

class Revision::Publisher < Revision

  attr_reader :version, :versionable
  def initialize(version)
    @version = version
    @versionable = version.versionable
  end

  def accept_revision
    create_or_update_versionable
    update_version_children
  end

  private
  def create_or_update_versionable
    versionable.present? ? update_versionable : create_versionable
  end

  def new_attributes
    version_attributes.each_with_object({}) {|v, h| h[v.name] = v.new_value }
  end

  def create_versionable
    version.versionable = version.parent.versionable.public_send(
                          version.association_name).create(new_attributes)
  end

  def update_versionable
    versionable.update_attributes(new_attributes)
  end

  def update_version_children
    version_children.each do |child|
      update_or_destroy_child(child)
    end
  end

  def update_or_destroy_child(child)
    if child.marked_for_removal?
      destroy_child(child)
    else
      Revision::Publisher.new(child).accept_revision
    end
  end

  def destroy_child(child)
    child.versionable.destroy
  end

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
rubber_stamp-0.13.0 lib/rubber_stamp/revision/publisher.rb
controlled_versioning-0.12.0 lib/controlled_versioning/revision/publisher.rb
controlled_versioning-0.11.0 lib/controlled_versioning/revision/publisher.rb
controlled_versioning-0.10.0 lib/controlled_versioning/revision/publisher.rb
controlled_versioning-0.9.2 lib/controlled_versioning/revision/publisher.rb
controlled_versioning-0.9.1 lib/controlled_versioning/revision/publisher.rb
controlled_versioning-0.9.0 lib/controlled_versioning/revision/publisher.rb