Sha256: 4a9b976e598e25660387c5e4cd78cdbb9be3cdfb685b4e2d2d4c286577eef5ca

Contents?: true

Size: 1.33 KB

Versions: 144

Compression:

Stored size: 1.33 KB

Contents

module Scrivito

class Comparison < Struct.new(:revision, :inverse, :diff_mode)

  alias :inverse? :inverse

  def active?
    !!revision
  end

  def modification(content)
    return nil unless revision

    convert_modification(content.modification(revision))
  end

  def modification_for_attribute(content, field_name)
    return nil unless revision

    convert_modification(
      content.modification_for_attribute(field_name, revision)
    )
  end

  def diff_for(content, field_name)
    return nil unless revision

    current_value = content[field_name]
    compare_value = value_of(content, field_name)

    return nil if current_value == compare_value

    field_type = content.type_of_attribute(field_name)

    if inverse?
      old, new = current_value, compare_value
    else
      old, new = compare_value, current_value
    end

    Scrivito::Diff.for(diff_mode, field_type, old, new)
  end

  private

  def value_of(content, field_name)
    compare_content = content.in_revision(revision)
    compare_content && compare_content[field_name]
  end

  def convert_modification(modification)
    if inverse?
      case modification
      when Modification::NEW
        Modification::DELETED
      when Modification::DELETED
        Modification::NEW
      else
        modification
      end
    else
      modification
    end
  end
end

end

Version data entries

144 entries across 144 versions & 1 rubygems

Version Path
scrivito_sdk-1.18.0 app/cms/scrivito/comparison.rb
scrivito_sdk-1.18.0.rc1 app/cms/scrivito/comparison.rb
scrivito_sdk-1.17.0 app/cms/scrivito/comparison.rb
scrivito_sdk-1.17.0.rc3 app/cms/scrivito/comparison.rb
scrivito_sdk-1.17.0.rc2 app/cms/scrivito/comparison.rb
scrivito_sdk-1.17.0.rc1 app/cms/scrivito/comparison.rb
scrivito_sdk-1.16.0 app/cms/scrivito/comparison.rb
scrivito_sdk-1.16.0.rc2 app/cms/scrivito/comparison.rb
scrivito_sdk-1.16.0.rc1 app/cms/scrivito/comparison.rb
scrivito_sdk-1.15.0 app/cms/scrivito/comparison.rb
scrivito_sdk-1.15.0.rc2 app/cms/scrivito/comparison.rb
scrivito_sdk-1.15.0.rc1 app/cms/scrivito/comparison.rb
scrivito_sdk-1.14.0 app/cms/scrivito/comparison.rb
scrivito_sdk-1.14.0.rc3 app/cms/scrivito/comparison.rb
scrivito_sdk-1.14.0.rc2 app/cms/scrivito/comparison.rb
scrivito_sdk-1.14.0.rc1 app/cms/scrivito/comparison.rb
scrivito_sdk-1.13.0 app/cms/scrivito/comparison.rb
scrivito_sdk-1.13.0.rc2 app/cms/scrivito/comparison.rb
scrivito_sdk-1.13.0.rc1 app/cms/scrivito/comparison.rb
scrivito_sdk-1.12.0 app/cms/scrivito/comparison.rb