Sha256: 5c82e28863e778c5bb2a073e44552543dd158b9bf4ff215817f9c4feda3d5363

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 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.versionable_type.constantize.
                          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

4 entries across 4 versions & 1 rubygems

Version Path
controlled_versioning-0.8.0 lib/controlled_versioning/revision/publisher.rb
controlled_versioning-0.7.2 lib/controlled_versioning/revision/publisher.rb
controlled_versioning-0.6.3 lib/controlled_versioning/revision/publisher.rb
controlled_versioning-0.6.2 lib/controlled_versioning/revision/publisher.rb