1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Added ability for user to specify a field_name to use for the version creation timestamp

This commit is contained in:
Jared Mehle 2012-02-01 13:47:06 -06:00
parent aba0fc30d4
commit f8becc65e8
4 changed files with 14 additions and 5 deletions

View file

@ -32,6 +32,14 @@ module PaperTrail
paper_trail_store[:request_enabled_for_controller] = value
end
def self.timestamp_field=(field_name)
PaperTrail.config.timestamp_field = field_name
end
def self.timestamp_field
PaperTrail.config.timestamp_field
end
# Returns who is reponsible for any changes that occur.
def self.whodunnit
paper_trail_store[:whodunnit]

View file

@ -1,11 +1,12 @@
module PaperTrail
class Config
include Singleton
attr_accessor :enabled
attr_accessor :enabled, :timestamp_field
def initialize
# Indicates whether PaperTrail is on or off.
@enabled = true
@enabled = true
@timestamp_field = :created_at
end
end
end

View file

@ -68,7 +68,7 @@ module PaperTrail
has_many self.versions_association_name,
:class_name => version_class_name,
:as => :item,
:order => "created_at ASC, #{self.version_class_name.constantize.primary_key} ASC"
:order => "#{PaperTrail.timestamp_field} ASC, #{self.version_class_name.constantize.primary_key} ASC"
after_create :record_create, :if => :save_version? if !options[:on] || options[:on].include?(:create)
before_update :record_update, :if => :save_version? if !options[:on] || options[:on].include?(:update)

View file

@ -17,7 +17,7 @@ class Version < ActiveRecord::Base
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.
@ -146,7 +146,7 @@ class Version < ActiveRecord::Base
# 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