lib/paper_trail/version.rb in paper_trail-2.6.0 vs lib/paper_trail/version.rb in paper_trail-2.6.1
- old
+ new
@@ -6,20 +6,20 @@
def self.with_item_keys(item_type, item_id)
scoped(:conditions => { :item_type => item_type, :item_id => item_id })
end
scope :subsequent, lambda { |version|
- where(["#{self.primary_key} > ?", version.is_a?(self) ? version.id : version]).order("#{self.primary_key} ASC")
+ where(["#{self.primary_key} > ?", version.is_a?(self) ? version.send(self.primary_key) : version]).order("#{self.primary_key} ASC")
}
scope :preceding, lambda { |version|
- where(["#{self.primary_key} < ?", version.is_a?(self) ? version.id : version]).order("#{self.primary_key} DESC")
+ where(["#{self.primary_key} < ?", version.is_a?(self) ? version.send(self.primary_key) : version]).order("#{self.primary_key} DESC")
}
scope :following, lambda { |timestamp|
# TODO: is this :order necessary, considering its presence on the has_many :versions association?
- where(['created_at > ?', timestamp]).order("created_at ASC, #{self.primary_key} ASC")
+ where(["#{PaperTrail.timestamp_field} > ?", timestamp]).order("#{PaperTrail.timestamp_field} ASC, #{self.primary_key} ASC")
}
# Restore the item from this version.
#
# This will automatically restore all :has_one associations as they were "at the time",
@@ -114,11 +114,12 @@
def previous
sibling_versions.preceding(self).first
end
def index
- sibling_versions.select(:id).order("id ASC").map(&:id).index(self.id)
+ id_column = self.class.primary_key.to_sym
+ sibling_versions.select(id_column).order("#{id_column} ASC").map(&id_column).index(self.send(id_column))
end
private
# In Rails 3.1+, calling reify on a previous version confuses the
@@ -143,10 +144,10 @@
# N.B. we use version of the child as it was `lookback` seconds before the parent was updated.
# Ideally we want the version of the child as it was just before the parent was updated...
# but until PaperTrail knows which updates are "together" (e.g. parent and child being
# updated on the same form), it's impossible to tell when the overall update started;
# and therefore impossible to know when "just before" was.
- if (child_as_it_was = child.version_at(created_at - lookback.seconds))
+ if (child_as_it_was = child.version_at(send(PaperTrail.timestamp_field) - lookback.seconds))
child_as_it_was.attributes.each do |k,v|
model.send(assoc.name).send :write_attribute, k.to_sym, v rescue nil
end
else
model.send "#{assoc.name}=", nil