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