Sha256: c2e6074a14e4626b47bf0480213f1380bba3a4034e0f7a7af70c5d5d1f8927c6

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

class RevisionAuditor < Revision

  attr_reader :versionable
  def initialize(versionable)
    @versionable = versionable
  end

  def changes_original?
    return true if versionable.marked_for_destruction?
    return true if changes_at_least_one_attribute?
    return true if changes_at_least_one_nested_association?
    return false
  end

  private
  def changes_at_least_one_attribute?
    versionable_attributes.each do |attr|
      attr = AttributeEncapsulator.new(attr)
      return true if changed_attributes.include?(attr.key)
    end
    return false
  end

  def changes_at_least_one_nested_association?
    nested_associations.each do |association|
      return true if association_changed?(association)
    end
    return false
  end

  def association_changed?(association)
    versionable.public_send(association).each do |child|
      return true if child_changed?(child)
    end
    return false
  end

  def child_changed?(child)
    child.new_record? || child.marked_for_destruction? ||
      RevisionAuditor.new(child).changes_original?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
controlled_versioning-0.6.1 lib/controlled_versioning/revision_auditor.rb
controlled_versioning-0.6 lib/controlled_versioning/revision_auditor.rb