class PaperTrail::RelatedChanges::Serializer::BelongsTo

Attributes

attribute[R]
change[R]
item_type[R]

Public Class Methods

match(attribute) click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 4
def self.match(attribute)
  attribute.version
    .model_class
    .reflections
    .values
    .select(&:belongs_to?)
    .reject(&:polymorphic?)
    .any? { |r| r.join_foreign_key.to_sym == attribute.to_sym }
end
new(attribute, change) click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 22
def initialize(attribute, change)
  @attribute = attribute
  @item_type = attribute.version.item_type
  @change    = change
end
serialize(attribute, change) click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 14
def self.serialize(attribute, change)
  new(attribute, change).serialize
end

Public Instance Methods

association() click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 44
def association
  @association ||= item_type
                     .constantize
                     .reflections
                     .detect { |_, a| a.foreign_key.to_sym == attribute.to_sym }&.fetch(1)
end
attribute_name() click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 40
def attribute_name
  association.name
end
find_associated_version(id) click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 64
def find_associated_version(id)
  PaperTrail::Version.where(
    PaperTrail::Version.arel_table[:created_at].lt(attribute.version.created_at)
  ).where(
    item_id:   id,
    item_type: model_class.name
  ).order(created_at: :desc).first
end
find_current_record(id) click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 60
def find_current_record(id)
  model_class.find_by(id: id)
end
find_record(id) click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 51
def find_record(id)
  return unless id
  find_associated_version(id).try(:name) || find_current_record(id).try(:name) || "Record no longer exists"
end
model_class() click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 56
def model_class
  @model_class ||= association.klass
end
serialize() click to toggle source
# File lib/paper_trail/related_changes/serializer/belongs_to.rb, line 28
def serialize
  return if association.name.to_s.underscore.singularize == attribute.request_type.to_s.underscore.singularize
  change.merge_into_root = true unless model_class.relationally_independent?
  change.add_diff(
    attribute: attribute_name,
    old:       find_record(attribute.diff[0]),
    new:       find_record(attribute.diff[1]),
    rank:      3,
    source:    self.class.name
  )
end