Class: Recorder::Changeset

Inherits:
Object
  • Object
show all
Defined in:
lib/recorder/changeset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, changes) ⇒ Changeset

Returns a new instance of Changeset.



5
6
7
# File 'lib/recorder/changeset.rb', line 5

def initialize(item, changes)
  @item = item; @changes = changes
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



3
4
5
# File 'lib/recorder/changeset.rb', line 3

def changes
  @changes
end

#itemObject (readonly)

Returns the value of attribute item.



3
4
5
# File 'lib/recorder/changeset.rb', line 3

def item
  @item
end

Instance Method Details

#human_attribute_name(attribute) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/recorder/changeset.rb', line 13

def human_attribute_name(attribute)
  if defined?(Draper) && self.item.decorated?
    self.item.source.class.human_attribute_name(attribute.to_s)
  else
    self.item.class.human_attribute_name(attribute.to_s)
  end
end

#keysObject



9
10
11
# File 'lib/recorder/changeset.rb', line 9

def keys
  self.changes.try(:keys) || []
end

#next(attribute) ⇒ Object



37
38
39
# File 'lib/recorder/changeset.rb', line 37

def next(attribute)
  self.try("next_#{attribute}") || self.next_version.send(attribute)
end

#next_versionObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/recorder/changeset.rb', line 41

def next_version
  return @next_version if defined?(@next_version)

  @next_version = self.item.dup

  self.changes.each do |key, change|
    @next_version.send("#{key}=", change[1])
  end

  @next_version
end

#previous(attribute) ⇒ Object



21
22
23
# File 'lib/recorder/changeset.rb', line 21

def previous(attribute)
  self.try("previous_#{attribute}") || self.previous_version.try(attribute)
end

#previous_versionObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/recorder/changeset.rb', line 25

def previous_version
  return @previous_version if defined?(@previous_version)

  @previous_version = self.item.dup

  self.changes.each do |key, change|
    @previous_version.send("#{key}=", change[0])
  end

  @previous_version
end