Module: PaperTrail::RelatedChanges::Serializer::Polymorphic

Defined in:
lib/paper_trail/related_changes/serializer/polymorphic.rb

Class Method Summary collapse

Class Method Details

.match(attribute) ⇒ Object



3
4
5
# File 'lib/paper_trail/related_changes/serializer/polymorphic.rb', line 3

def self.match(attribute)
  attribute.version.item_type.constantize.reflections.detect { |_, r| r.polymorphic? && r.belongs_to? }
end

.serialize(attribute, change) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/paper_trail/related_changes/serializer/polymorphic.rb', line 7

def self.serialize(attribute, change)
  return if [/_id/, /_type/].any? { |r| attribute.to_s =~ r } # Catch all non-associative attributes
  change.merge_into_root = true unless attribute.version.model_class.relationally_independent?
  change.add_diff(
    attribute: attribute.version.item_type.titleize,
    old:       attribute.diff[0],
    new:       attribute.diff[1],
    rank:      2,
    source:    self.name
  )
end