Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent class attributes name clashes. #157

Merged
merged 3 commits into from
Nov 6, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 16 additions & 19 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,15 @@ def has_paper_trail(options = {})
class_attribute :version_class_name
self.version_class_name = options[:class_name] || 'Version'

class_attribute :ignore
self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
class_attribute :paper_trail_options
self.paper_trail_options = options.dup

class_attribute :if_condition
self.if_condition = options[:if]

class_attribute :unless_condition
self.unless_condition = options[:unless]

class_attribute :skip
self.skip = ([options[:skip]].flatten.compact || []).map &:to_s

class_attribute :only
self.only = ([options[:only]].flatten.compact || []).map &:to_s
[:ignore, :skip, :only].each do |k|
paper_trail_options[k] =
([paper_trail_options[k]].flatten.compact || []).map &:to_s
end

class_attribute :meta
self.meta = options[:meta] || {}
paper_trail_options[:meta] ||= {}

class_attribute :paper_trail_enabled_for_model
self.paper_trail_enabled_for_model = true
Expand Down Expand Up @@ -183,7 +175,7 @@ def record_destroy

def merge_metadata(data)
# First we merge the model-level metadata in `meta`.
meta.each do |k,v|
paper_trail_options[:meta].each do |k,v|
data[k] =
if v.respond_to?(:call)
v.call(self)
Expand All @@ -210,26 +202,31 @@ def item_before_change
end

def object_to_string(object)
object.attributes.except(*self.class.skip).to_yaml
object.attributes.except(*self.class.paper_trail_options[:skip]).to_yaml
end

def changed_notably?
notably_changed.any?
end

def notably_changed
self.class.only.empty? ? changed_and_not_ignored : (changed_and_not_ignored & self.class.only)
only = self.class.paper_trail_options[:only]
only.empty? ? changed_and_not_ignored : (changed_and_not_ignored & only)
end

def changed_and_not_ignored
changed - self.class.ignore - self.class.skip
ignore = self.class.paper_trail_options[:ignore]
skip = self.class.paper_trail_options[:skip]
changed - ignore - skip
end

def switched_on?
PaperTrail.enabled? && PaperTrail.enabled_for_controller? && self.class.paper_trail_enabled_for_model
end

def save_version?
if_condition = self.class.paper_trail_options[:if]
unless_condition = self.class.paper_trail_options[:unless]
(if_condition.blank? || if_condition.call(self)) && !unless_condition.try(:call, self)
end
end
Expand Down