Class: Version

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/paper_trail/version.rb

Instance Method Summary collapse

Instance Method Details

#differencesObject



44
45
46
# File 'lib/paper_trail/version.rb', line 44

def differences
  diff ? YAML.load(diff) : {}
end

#indexObject



58
59
60
61
# File 'lib/paper_trail/version.rb', line 58

def index
  Version.all(:conditions => ["item_type = ? AND item_id = ?", item_type, item_id],
              :order => 'id ASC').index(self)
end

#nextObject



48
49
50
51
# File 'lib/paper_trail/version.rb', line 48

def next
  Version.first :conditions => ["id > ? AND item_type = ? AND item_id = ?", id, item_type, item_id],
                :order => 'id ASC'
end

#previousObject



53
54
55
56
# File 'lib/paper_trail/version.rb', line 53

def previous
  Version.first :conditions => ["id < ? AND item_type = ? AND item_id = ?", id, item_type, item_id],
                :order => 'id DESC'
end

#reifyObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/paper_trail/version.rb', line 5

def reify
  unless object.nil?
    # Attributes

    attrs = YAML::load object

    # Normally a polymorphic belongs_to relationship allows us
    # to get the object we belong to by calling, in this case,
    # +item+.  However this returns nil if +item+ has been
    # destroyed, and we need to be able to retrieve destroyed
    # objects.
    #
    # In this situation we constantize the +item_type+ to get hold of
    # the class...except when the stored object's attributes
    # include a +type+ key.  If this is the case, the object
    # we belong to is using single table inheritance and the
    # +item_type+ will be the base class, not the actual subclass.
    # If +type+ is present but empty, the class is the base class.

    if item
      model = item
    else
      class_name = attrs['type'].blank? ? item_type : attrs['type']
      klass = class_name.constantize
      model = klass.new
    end

    attrs.each do |k, v|
      begin
        model.send "#{k}=", v
      rescue NoMethodError
        logger.warn "Attribute #{k} does not exist on #{item_type} (Version id: #{id})."
      end
    end

    model
  end
end