2009-05-27 11:21:20 -04:00
|
|
|
class Version < ActiveRecord::Base
|
|
|
|
belongs_to :item, :polymorphic => true
|
|
|
|
validates_presence_of :event
|
|
|
|
|
|
|
|
def reify
|
|
|
|
unless object.nil?
|
2009-06-18 04:45:46 -04:00
|
|
|
# Attributes
|
|
|
|
|
|
|
|
attrs = YAML::load object
|
2009-06-17 07:36:23 -04:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
#
|
2009-06-18 13:21:20 -04:00
|
|
|
# In this situation we constantize the +item_type+ to get hold of
|
2009-06-17 07:36:23 -04:00
|
|
|
# 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.
|
2009-06-18 13:21:20 -04:00
|
|
|
# If +type+ is present but empty, the class is the base class.
|
2009-06-17 07:36:23 -04:00
|
|
|
|
2009-06-18 13:21:20 -04:00
|
|
|
if item
|
|
|
|
model = item
|
|
|
|
else
|
|
|
|
class_name = attrs['type'].blank? ? item_type : attrs['type']
|
|
|
|
klass = class_name.constantize
|
|
|
|
model = klass.new
|
|
|
|
end
|
2009-06-17 07:36:23 -04:00
|
|
|
|
|
|
|
attrs.each do |k, v|
|
2009-05-27 11:21:20 -04:00
|
|
|
begin
|
|
|
|
model.send "#{k}=", v
|
|
|
|
rescue NoMethodError
|
2009-11-19 22:15:30 -05:00
|
|
|
logger.warn "Attribute #{k} does not exist on #{item_type} (Version id: #{id})."
|
2009-05-27 11:21:20 -04:00
|
|
|
end
|
|
|
|
end
|
2009-06-18 04:45:46 -04:00
|
|
|
|
2009-05-27 11:21:20 -04:00
|
|
|
model
|
|
|
|
end
|
|
|
|
end
|
2009-06-19 05:15:49 -04:00
|
|
|
|
2009-06-19 11:46:33 -04:00
|
|
|
def next
|
|
|
|
Version.first :conditions => ["id > ? AND item_type = ? AND item_id = ?", id, item_type, item_id],
|
|
|
|
:order => 'id ASC'
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous
|
|
|
|
Version.first :conditions => ["id < ? AND item_type = ? AND item_id = ?", id, item_type, item_id],
|
|
|
|
:order => 'id DESC'
|
|
|
|
end
|
|
|
|
|
2009-06-22 11:37:00 -04:00
|
|
|
def index
|
|
|
|
Version.all(:conditions => ["item_type = ? AND item_id = ?", item_type, item_id],
|
|
|
|
:order => 'id ASC').index(self)
|
|
|
|
end
|
|
|
|
|
2009-05-27 11:21:20 -04:00
|
|
|
end
|