class PaperTrail::RelatedChanges::Serializer

Constants

Diff

Attributes

item_type[R]
model_to_include_name[R]
record[R]
root_type[R]

Public Class Methods

new(record, item_type:, model_to_include_name: {}, root_type: nil) click to toggle source
# File lib/paper_trail/related_changes/serializer.rb, line 11
def initialize(record, item_type:, model_to_include_name: {}, root_type: nil)
  @record                = record
  @item_type             = item_type
  @model_to_include_name = model_to_include_name
  @root_type             = root_type
end

Public Instance Methods

change() click to toggle source
# File lib/paper_trail/related_changes/serializer.rb, line 20
def change
  @change ||= change_template
  build_changes
  @change
end

Private Instance Methods

build_changes() click to toggle source
# File lib/paper_trail/related_changes/serializer.rb, line 65
def build_changes
  @build_changes ||= record.changeset.each do |attr, diff|
    BuildDiffs.new(attr, diff, record, item_type, @change).call
  end
end
change_template() click to toggle source
# File lib/paper_trail/related_changes/serializer.rb, line 28
def change_template
  Change.new(
    version_id:     record.id,
    user:           user,
    event:          record.event,
    resource:       record.item_type,
    description:    {
      name:  resource_title,
      value: record.name
    },
    resource_id:    record.item_id,
    timestamp:      record.created_at,
    requested_root: !included?
  )
end
included?() click to toggle source
# File lib/paper_trail/related_changes/serializer.rb, line 49
def included?
  return record.item_type != root_type unless root_type.nil?
  record.item_type != item_type.classify
end
resource_title() click to toggle source
# File lib/paper_trail/related_changes/serializer.rb, line 54
def resource_title
  model_to_include_name.fetch(
    record.item_type,
    record.item_type.underscore.split('/').last
  ).to_s.singularize.titleize
end
user() click to toggle source
# File lib/paper_trail/related_changes/serializer.rb, line 61
def user
  PaperTrail::RelatedChanges.user_class.find_by(id: record.whodunnit)&.name || "system"
end