class Recorder::Tape::Data

Attributes

item[R]

Public Class Methods

new(item) click to toggle source
# File lib/recorder/tape/data.rb, line 8
def initialize(item)
  @item = item
end

Public Instance Methods

associations_for(event, options) click to toggle source
# File lib/recorder/tape/data.rb, line 34
def associations_for(event, options)
  associations = parse_associations_attributes(event, options)

  associations.present? ? {associations: associations} : {}
end
attributes_for(_event, options) click to toggle source
# File lib/recorder/tape/data.rb, line 20
def attributes_for(_event, options)
  {attributes: sanitize_attributes(item.attributes, options)}
end
changes_for(event, options) click to toggle source
# File lib/recorder/tape/data.rb, line 24
def changes_for(event, options)
  changes =
    case event.to_sym
    when :update
      sanitize_attributes(item.saved_changes, options)
    end

  changes.present? ? {changes: changes} : {}
end
data_for(event, options = {}) click to toggle source
# File lib/recorder/tape/data.rb, line 12
def data_for(event, options = {})
  {
    **attributes_for(event, options),
    **changes_for(event, options),
    **associations_for(event, options)
  }
end

Private Instance Methods

parse_association(event, association, options) click to toggle source
# File lib/recorder/tape/data.rb, line 68
def parse_association(event, association, options)
  reflection = item.class.reflect_on_association(association)

  if reflection.present?
    if reflection.collection?

    elsif (object = item.send(association))
      data = Recorder::Tape::Data.new(object).data_for(event, options || {})

      [reflection.name, data]
    end
  end
end
parse_associations_attributes(event, options) click to toggle source
# File lib/recorder/tape/data.rb, line 58
def parse_associations_attributes(event, options)
  return unless options[:associations]

  options[:associations].each_with_object({}) do |(association, options), hash|
    name, data = parse_association(event, association, options)

    hash[name] = data if data.any?
  end
end
sanitize_attributes(attributes, options) click to toggle source
# File lib/recorder/tape/data.rb, line 42
def sanitize_attributes(attributes, options)
  if options[:only].present?
    only = wrap_options(options[:only])
    attributes.symbolize_keys.slice(*only)
  elsif options[:ignore].present?
    ignore = wrap_options(options[:ignore])
    attributes.symbolize_keys.except(*ignore)
  else
    attributes.symbolize_keys.except(*Recorder.config.ignore)
  end
end
wrap_options(values) click to toggle source
# File lib/recorder/tape/data.rb, line 54
def wrap_options(values)
  Array.wrap(values).map(&:to_sym)
end