diff options
Diffstat (limited to 'vendor/plugins/strip_attributes/lib')
-rw-r--r-- | vendor/plugins/strip_attributes/lib/strip_attributes.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/vendor/plugins/strip_attributes/lib/strip_attributes.rb b/vendor/plugins/strip_attributes/lib/strip_attributes.rb new file mode 100644 index 000000000..493db56bf --- /dev/null +++ b/vendor/plugins/strip_attributes/lib/strip_attributes.rb @@ -0,0 +1,31 @@ +module StripAttributes + # Strips whitespace from model fields and converts blank values to nil. + def strip_attributes!(options = nil) + before_validation do |record| + attributes = StripAttributes.narrow(record.attributes, options) + attributes.each do |attr, value| + if value.respond_to?(:strip) + record[attr] = (value.blank?) ? nil : value.strip + end + end + end + end + + # Necessary because Rails has removed the narrowing of attributes using :only + # and :except on Base#attributes + def self.narrow(attributes, options) + if options.nil? + attributes + else + if except = options[:except] + except = Array(except).collect { |attribute| attribute.to_s } + attributes.except(*except) + elsif only = options[:only] + only = Array(only).collect { |attribute| attribute.to_s } + attributes.slice(*only) + else + raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})" + end + end + end +end |