strip_attributes 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  StripAttributes is an ActiveModel extension that automatically strips all
4
4
  attributes of leading and trailing whitespace before validation. If the
5
- attribute is blank, it strips the value to `nil`.
5
+ attribute is blank, it strips the value to `nil` by default.
6
6
 
7
7
  It works by adding a before_validation hook to the record. By default, all
8
8
  attributes are stripped of whitespace, but `:only` and `:except`
@@ -46,6 +46,15 @@ class ConservativePokerPlayer < ActiveRecord::Base
46
46
  end
47
47
  ```
48
48
 
49
+ ### Using `allow_empty`
50
+
51
+ ```ruby
52
+ # Empty attributes will not be converted to nil
53
+ class BrokePokerPlayer < ActiveRecord::Base
54
+ strip_attributes :allow_empty => true
55
+ end
56
+ ```
57
+
49
58
  ## Usage Patterns
50
59
 
51
60
  ### Other ORMs implementing `ActiveModel`
@@ -97,7 +106,6 @@ To initialize **Shoulda (with test-unit)**, add this to your `test_helper.rb`:
97
106
  ```ruby
98
107
  require "strip_attributes/matchers"
99
108
  class Test::Unit::TestCase
100
- include StripAttributes::Matchers
101
109
  extend StripAttributes::Matchers
102
110
  end
103
111
  ```
@@ -1,3 +1,3 @@
1
1
  module StripAttributes
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -4,10 +4,12 @@ module ActiveModel::Validations::HelperMethods
4
4
  # Strips whitespace from model fields and converts blank values to nil.
5
5
  def strip_attributes(options = nil)
6
6
  before_validation do |record|
7
+ allow_empty = options.delete(:allow_empty) if options
8
+
7
9
  attributes = StripAttributes.narrow(record.attributes, options)
8
10
  attributes.each do |attr, value|
9
11
  if value.respond_to?(:strip)
10
- record[attr] = (value.blank?) ? nil : value.strip
12
+ record[attr] = (value.blank? && !allow_empty) ? nil : value.strip
11
13
  end
12
14
  end
13
15
  end
@@ -25,7 +27,7 @@ module StripAttributes
25
27
  # Necessary because Rails has removed the narrowing of attributes using :only
26
28
  # and :except on Base#attributes
27
29
  def self.narrow(attributes, options)
28
- if options.nil?
30
+ if options.nil? || options.empty?
29
31
  attributes
30
32
  else
31
33
  if except = options[:except]
@@ -31,6 +31,12 @@ class StripExceptThreeMockRecord < Tableless
31
31
  strip_attributes :except => [:foo, :bar, :biz]
32
32
  end
33
33
 
34
+ class StripAllowEmpty < Tableless
35
+ include MockAttributes
36
+ strip_attributes :allow_empty => true
37
+ end
38
+
39
+
34
40
  class StripAttributesTest < MiniTest::Unit::TestCase
35
41
  def setup
36
42
  @init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " " }
@@ -89,4 +95,14 @@ class StripAttributesTest < MiniTest::Unit::TestCase
89
95
  assert_nil record.baz
90
96
  assert_nil record.bang
91
97
  end
98
+
99
+ def test_should_strip_and_allow_empty
100
+ record = StripAllowEmpty.new(@init_params)
101
+ record.valid?
102
+ assert_equal "foo", record.foo
103
+ assert_equal "bar", record.bar
104
+ assert_equal "biz", record.biz
105
+ assert_equal "", record.baz
106
+ assert_equal "", record.bang
107
+ end
92
108
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: strip_attributes
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.2.0
5
+ version: 1.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan McGeary
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-25 00:00:00.000000000 Z
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
106
  - !ruby/object:Gem::Version
107
107
  segments:
108
108
  - 0
109
- hash: 4383093810651620729
109
+ hash: -2456424087513438655
110
110
  version: '0'
111
111
  required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  none: false
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  - !ruby/object:Gem::Version
116
116
  segments:
117
117
  - 0
118
- hash: 4383093810651620729
118
+ hash: -2456424087513438655
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project: