Class: Recorder::Revision

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/recorder/revision.rb

Instance Method Summary collapse

Instance Method Details

#association_changeset(name) ⇒ Recorder::Changeset

Get changeset for an association

Parameters:

  • name (String)

    name of association to return changeset

Returns:



64
65
66
67
68
69
# File 'lib/recorder/revision.rb', line 64

def association_changeset(name)
  association = item.send(name)
  # association = association.source if association.decorated?

  changeset_class(association).new(association, data['associations'].fetch(name.to_s).try(:fetch, 'changes'))
end

#changed_associationsArray

Get names of item associations that has been changed

Returns:

  • (Array)


57
58
59
# File 'lib/recorder/revision.rb', line 57

def changed_associations
  data['associations'].try(:keys) || []
end

#itemObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/recorder/revision.rb', line 30

def item
  return @item if defined?(@item)
  return if item_id.nil?

  @item = item_type.classify.constantize.new(data['attributes'])

  if data['associations'].present?
    data['associations'].each do |name, association|
      @item.send("build_#{name}", association['attributes'])
    end
  end

  @item
end

#item_changesetRecorder::Changeset

Get changeset for an item

Returns:



47
48
49
50
51
52
53
# File 'lib/recorder/revision.rb', line 47

def item_changeset
  return @item_changeset if defined?(@item_changeset)
  return nil if item.nil?
  return nil if data['changes'].nil?

  @item_changeset ||= changeset_class(item).new(item, data['changes'])
end