Skip to content

Commit

Permalink
added note about possible destructive behavior of if_changed? [Michae…
Browse files Browse the repository at this point in the history
…l Schuerig]

git-svn-id: http://svn.techno-weenie.net/projects/plugins/acts_as_versioned@390 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Nov 27, 2005
1 parent adc2d4b commit 1d4e34a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
*0.2.4*

* (27 Nov 2005) added note about possible destructive behavior of if_changed? [Michael Schuerig]

*0.2.3*

* (12 Nov 2005) fixed bug with old behavior of #blank? [Michael Schuerig]
Expand Down
16 changes: 13 additions & 3 deletions lib/acts_as_versioned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ module ClassMethods
# end
#
# * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes
# either a symbol or array of symbols.
# either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have.
# Add this instead:
#
# def name=(new_name)
# write_changed_attribute :name, new_name
# end
#
# == Database Schema
#
Expand All @@ -104,6 +109,7 @@ module ClassMethods
# Post.drop_versioned_table
# end
# end
#
def acts_as_versioned(options = {})
# don't allow multiple calls
return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)
Expand Down Expand Up @@ -140,8 +146,7 @@ def acts_as_versioned(options = {})
options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array)
options[:if_changed].each do |attr_name|
define_method("#{attr_name}=") do |value|
(self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) or self.send(attr_name) == value
write_attribute(attr_name.to_s, value)
write_changed_attribute attr_name, value
end
end
end
Expand Down Expand Up @@ -296,6 +301,11 @@ def clear_changed_attributes
self.changed_attributes = []
end

def write_changed_attribute(attr_name, attr_value)
(self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) or self.send(attr_name) == attr_value
write_attribute(attr_name.to_s, attr_value)
end

private
unless defined?(ACTS_AS_VERSIONED_CALLBACKS)
ACTS_AS_VERSIONED_CALLBACKS = [:set_new_version, :save_version_on_create, :save_version, :clear_changed_attributes]
Expand Down

0 comments on commit 1d4e34a

Please sign in to comment.