diff options
author | Henare Degan <henare.degan@gmail.com> | 2013-02-07 08:07:11 +1100 |
---|---|---|
committer | Henare Degan <henare.degan@gmail.com> | 2013-02-07 08:07:11 +1100 |
commit | 702399a111382e16e7e7b29ec7a90d98d5cd27aa (patch) | |
tree | c04e2d7e48a3072b4e7b1e8fcdac7631ff96248b | |
parent | 95aed91006f4e39cbf854efaa879adfda07d3838 (diff) |
Fix bug to allow updating attributes when using strip_attributes and globalize3
We do this by making strip_attributes manipulate the unsaved object so
that it doesn't interact with globalize3.
Calling 'record.attributes' would invoke the globalize3 overridden
method, which returns the stale translated attribute values.
We now use the object's values instead which are the ones we have just
assigned and want to update to.
-rw-r--r-- | vendor/plugins/strip_attributes/lib/strip_attributes.rb | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/vendor/plugins/strip_attributes/lib/strip_attributes.rb b/vendor/plugins/strip_attributes/lib/strip_attributes.rb index 70f414654..bb93aa9a7 100644 --- a/vendor/plugins/strip_attributes/lib/strip_attributes.rb +++ b/vendor/plugins/strip_attributes/lib/strip_attributes.rb @@ -3,10 +3,12 @@ module StripAttributes # XXX this differs from official StripAttributes, as it doesn't make blank cells null. def strip_attributes!(options = nil) before_validation do |record| - attributes = StripAttributes.narrow(record.attributes, options) - attributes.each do |attr, value| + attribute_names = StripAttributes.narrow(record.attribute_names, options) + + attribute_names.each do |attribute_name| + value = record[attribute_name] if value.respond_to?(:strip) - record[attr] = (value.nil?) ? nil : value.strip + record[attribute_name] = (value.nil?) ? nil : value.strip end end end @@ -14,16 +16,16 @@ module StripAttributes # Necessary because Rails has removed the narrowing of attributes using :only # and :except on Base#attributes - def self.narrow(attributes, options) + def self.narrow(attribute_names, options) if options.nil? - attributes + attribute_names else if except = options[:except] except = Array(except).collect { |attribute| attribute.to_s } - attributes.except(*except) + attribute_names - except elsif only = options[:only] only = Array(only).collect { |attribute| attribute.to_s } - attributes.slice(*only) + attribute_names & only else raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})" end |