Skip to content

Commit

Permalink
Added ability for user to specify a field_name to use for the version…
Browse files Browse the repository at this point in the history
… creation timestamp
  • Loading branch information
jrmehle committed Feb 1, 2012
1 parent aba0fc3 commit f8becc6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def self.enabled_for_controller=(value)
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]
Expand Down
5 changes: 3 additions & 2 deletions lib/paper_trail/config.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def has_paper_trail(options = {})
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)
Expand Down
4 changes: 2 additions & 2 deletions lib/paper_trail/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.with_item_keys(item_type, item_id)

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.
Expand Down Expand Up @@ -146,7 +146,7 @@ def reify_has_ones(model, lookback)
# 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
Expand Down

0 comments on commit f8becc6

Please sign in to comment.