From 8f7af9a45d9129ce7cc534d6b24b9ebae69d059b Mon Sep 17 00:00:00 2001 From: Ben Zittlau Date: Thu, 24 Mar 2011 13:19:32 -0600 Subject: [PATCH] Made the Version class customizable --- lib/paper_trail/has_paper_trail.rb | 22 +++++++++++++--------- lib/paper_trail/version.rb | 6 +++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index fbc1c5cf..794e566a 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -11,12 +11,13 @@ module PaperTrail # the model is available in the `versions` association. # # Options: - # :ignore an array of attributes for which a new `Version` will not be created if only they change. - # :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied - # :meta a hash of extra data to store. You must add a column to the `versions` table for each key. - # Values are objects or procs (which are called with `self`, i.e. the model with the paper - # trail). See `PaperTrail::Controller.info_for_paper_trail` for how to store data from - # the controller. + # :class_name the name of a custom Version class. This class should inherit from Version. + # :ignore an array of attributes for which a new `Version` will not be created if only they change. + # :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied + # :meta a hash of extra data to store. You must add a column to the `versions` table for each key. + # Values are objects or procs (which are called with `self`, i.e. the model with the paper + # trail). See `PaperTrail::Controller.info_for_paper_trail` for how to store data from + # the controller. def has_paper_trail(options = {}) # Lazily include the instance methods so we don't clutter up # any more ActiveRecord models than we have to. @@ -24,6 +25,9 @@ module PaperTrail # The version this instance was reified from. attr_accessor :version + + cattr_accessor :version_class_name + self.version_class_name = options[:class_name] || "Version" cattr_accessor :ignore self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s @@ -39,7 +43,7 @@ module PaperTrail cattr_accessor :paper_trail_active self.paper_trail_active = true - has_many :versions, :as => :item, :order => 'created_at ASC, id ASC' + has_many :versions, :class_name => version_class_name, :as => :item, :order => 'created_at ASC, id ASC' after_create :record_create before_update :record_update @@ -68,7 +72,7 @@ module PaperTrail # Returns who put the object into its current state. def originator - Version.with_item_keys(self.class.name, id).last.try :whodunnit + version_class_name.constantize.with_item_keys(self.class.name, id).last.try :whodunnit end # Returns the object (not a Version) as it was at the given timestamp. @@ -111,7 +115,7 @@ module PaperTrail def record_destroy if switched_on? and not new_record? - Version.create merge_metadata(:item_id => self.id, + version_class_name.constantize.create merge_metadata(:item_id => self.id, :item_type => self.class.name, :event => 'destroy', :object => object_to_string(item_before_change), diff --git a/lib/paper_trail/version.rb b/lib/paper_trail/version.rb index c5498706..ba75847c 100644 --- a/lib/paper_trail/version.rb +++ b/lib/paper_trail/version.rb @@ -7,11 +7,11 @@ class Version < ActiveRecord::Base } scope :subsequent, lambda { |version| - where(["id > ?", version.is_a?(Version) ? version.id : version]).order("id ASC") + where(["id > ?", version.is_a?(self) ? version.id : version]).order("id ASC") } scope :preceding, lambda { |version| - where(["id < ?", version.is_a?(Version) ? version.id : version]).order("id DESC") + where(["id < ?", version.is_a?(self) ? version.id : version]).order("id DESC") } scope :after, lambda { |timestamp| @@ -87,7 +87,7 @@ class Version < ActiveRecord::Base end def sibling_versions - Version.with_item_keys(item_type, item_id) + self.class.with_item_keys(item_type, item_id) end def next