strip_attributes 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +64 -19
- data/lib/strip_attributes/matchers.rb +44 -0
- data/lib/strip_attributes/shoulda/macros.rb +6 -0
- data/lib/strip_attributes/shoulda.rb +2 -0
- data/lib/strip_attributes/version.rb +1 -1
- data/test/matchers_test.rb +41 -0
- data/test/strip_attributes_test.rb +1 -1
- data/test/test_helper.rb +2 -1
- metadata +31 -12
data/README.md
CHANGED
@@ -15,7 +15,7 @@ a single attribute (`:only => :field`) or arrays of attributes (`:except =>
|
|
15
15
|
Include the gem in your Gemfile:
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
gem "strip_attributes", "~> 1.
|
18
|
+
gem "strip_attributes", "~> 1.2"
|
19
19
|
```
|
20
20
|
|
21
21
|
## Examples
|
@@ -59,44 +59,89 @@ class User
|
|
59
59
|
end
|
60
60
|
```
|
61
61
|
|
62
|
-
###
|
63
|
-
|
64
|
-
If you want to use this outside of Rails, just require
|
65
|
-
`strip_attributes/active_model` and you models will get the `strip_attributes`
|
66
|
-
class method.
|
62
|
+
### Using it with [`ActiveAttr`](https://github.com/cgriego/active_attr)
|
67
63
|
|
68
64
|
```ruby
|
69
|
-
|
70
|
-
|
65
|
+
class Person
|
66
|
+
include ActiveAttr::Model
|
67
|
+
include ActiveModel::Validations::Callbacks
|
68
|
+
|
69
|
+
attribute :name
|
70
|
+
attribute :email
|
71
|
+
|
71
72
|
strip_attributes
|
72
73
|
end
|
74
|
+
|
73
75
|
```
|
74
76
|
|
75
77
|
## Testing
|
76
78
|
|
77
|
-
StripAttributes provides
|
78
|
-
attribute assignment.
|
79
|
+
StripAttributes provides an RSpec/Shoulda-compatible matcher for easier
|
80
|
+
testing of attribute assignment. You can use this with
|
81
|
+
[RSpec](http://rspec.info/), [Shoulda](https://github.com/thoughtbot/shoulda),
|
82
|
+
or [Minitest-Matchers](https://github.com/zenspider/minitest-matchers).
|
79
83
|
|
80
|
-
### Setup `test_helper.rb`
|
84
|
+
### Setup `spec_helper.rb` or `test_helper.rb`
|
81
85
|
|
82
|
-
To initialize
|
83
|
-
`test_helper.rb`:
|
86
|
+
To initialize **RSpec**, add this to your `spec_helper.rb`:
|
84
87
|
|
85
88
|
```ruby
|
86
|
-
require "strip_attributes/
|
89
|
+
require "strip_attributes/matchers"
|
90
|
+
RSpec.configure do |config|
|
91
|
+
config.include StripAttributes::Matchers
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
To initialize **Shoulda (with test-unit)**, add this to your `test_helper.rb`:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
require "strip_attributes/matchers"
|
87
99
|
class Test::Unit::TestCase
|
88
|
-
|
100
|
+
include StripAttributes::Matchers
|
101
|
+
extend StripAttributes::Matchers
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
To initialize **Minitest-Matchers**, add this to your `test_helper.rb`:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
require "strip_attributes/matchers"
|
109
|
+
class MiniTest::Spec
|
110
|
+
include StripAttributes::Matchers
|
89
111
|
end
|
90
112
|
```
|
91
113
|
|
92
114
|
### Writing Tests
|
93
115
|
|
94
|
-
|
116
|
+
**Rspec**:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
describe User do
|
120
|
+
it { should strip_attribute :name }
|
121
|
+
it { should strip_attribute :email }
|
122
|
+
it { should_not strip_attribute :password }
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
**Shoulda (with test-unit)**:
|
95
127
|
|
96
128
|
```ruby
|
97
129
|
class UserTest < ActiveSupport::TestCase
|
98
|
-
|
99
|
-
|
130
|
+
should strip_attribute :name
|
131
|
+
should strip_attribute :email
|
132
|
+
should_not strip_attribute :password
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
**Minitest-Matchers**:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
describe User do
|
140
|
+
subject { User.new }
|
141
|
+
|
142
|
+
must { strip_attribute :name }
|
143
|
+
must { strip_attribute :email }
|
144
|
+
wont { strip_attribute :password }
|
100
145
|
end
|
101
146
|
```
|
102
147
|
|
@@ -116,5 +161,5 @@ support.
|
|
116
161
|
|
117
162
|
## License
|
118
163
|
|
119
|
-
Copyright (c) 2007-
|
164
|
+
Copyright (c) 2007-2013 Ryan McGeary released under the [MIT
|
120
165
|
license](http://en.wikipedia.org/wiki/MIT_License)
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module StripAttributes
|
2
|
+
module Matchers
|
3
|
+
|
4
|
+
# Whitespace is stripped from the beginning and end of the attribute
|
5
|
+
#
|
6
|
+
# RSpec Examples:
|
7
|
+
#
|
8
|
+
# it { should strip_attribute(:first_name) }
|
9
|
+
# it { should_not strip_attribute(:password) }
|
10
|
+
#
|
11
|
+
# Minitest Matchers Examples:
|
12
|
+
#
|
13
|
+
# must { strip_attribute :first_name }
|
14
|
+
# wont { strip_attribute :password }
|
15
|
+
def strip_attribute(attribute)
|
16
|
+
StripAttributeMatcher.new(attribute)
|
17
|
+
end
|
18
|
+
|
19
|
+
class StripAttributeMatcher
|
20
|
+
def initialize(attribute)
|
21
|
+
@attribute = attribute
|
22
|
+
end
|
23
|
+
|
24
|
+
def matches?(subject)
|
25
|
+
subject.send("#{@attribute}=", " string ")
|
26
|
+
subject.valid?
|
27
|
+
subject.send(@attribute) == "string"
|
28
|
+
end
|
29
|
+
|
30
|
+
def failure_message
|
31
|
+
"Expected whitespace to be stripped from `#{@attribute}`, but it was not"
|
32
|
+
end
|
33
|
+
|
34
|
+
def negative_failure_message
|
35
|
+
"Expected whitespace to remain on `#{@attribute}`, but it was stripped"
|
36
|
+
end
|
37
|
+
|
38
|
+
def description
|
39
|
+
"strip whitespace from ##{@attribute}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -3,7 +3,10 @@ require "shoulda/context"
|
|
3
3
|
module StripAttributes
|
4
4
|
module Shoulda
|
5
5
|
module Macros
|
6
|
+
# Deprecated. Use `should strip_attribute :attribute` instead.
|
6
7
|
def should_strip_attributes(*attributes)
|
8
|
+
warn "[DEPRECATION] should_strip_attributes is deprecated. " <<
|
9
|
+
"Use `should strip_attribute :attribute` instead."
|
7
10
|
attributes.each do |attribute|
|
8
11
|
attribute = attribute.to_sym
|
9
12
|
should "strip whitespace from #{attribute}" do
|
@@ -14,7 +17,10 @@ module StripAttributes
|
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
20
|
+
# Deprecated. Use `should_not strip_attribute :attribute` instead.
|
17
21
|
def should_not_strip_attributes(*attributes)
|
22
|
+
warn "[DEPRECATION] should_not_strip_attributes is deprecated. " <<
|
23
|
+
"Use `should_not strip_attribute :attribute` instead."
|
18
24
|
attributes.each do |attribute|
|
19
25
|
attribute = attribute.to_sym
|
20
26
|
should "not strip whitespace from #{attribute}" do
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "minitest/matchers"
|
2
|
+
require "test_helper"
|
3
|
+
require "strip_attributes/matchers"
|
4
|
+
|
5
|
+
class SampleMockRecord < Tableless
|
6
|
+
attributes :stripped1, :stripped2, :stripped3
|
7
|
+
attributes :unstripped1, :unstripped2, :unstripped3
|
8
|
+
|
9
|
+
strip_attributes :only => [:stripped1, :stripped2, :stripped3]
|
10
|
+
end
|
11
|
+
|
12
|
+
describe SampleMockRecord do
|
13
|
+
include StripAttributes::Matchers
|
14
|
+
|
15
|
+
subject { SampleMockRecord.new }
|
16
|
+
|
17
|
+
must { strip_attribute :stripped1 }
|
18
|
+
must { strip_attribute :stripped2 }
|
19
|
+
must { strip_attribute :stripped3 }
|
20
|
+
wont { strip_attribute :unstripped1 }
|
21
|
+
wont { strip_attribute :unstripped2 }
|
22
|
+
wont { strip_attribute :unstripped3 }
|
23
|
+
|
24
|
+
it "should fail when testing for strip on an unstripped attribute" do
|
25
|
+
begin
|
26
|
+
assert_must strip_attribute(:unstripped1)
|
27
|
+
assert false
|
28
|
+
rescue
|
29
|
+
assert true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should fail when testing for no strip on a stripped attribute" do
|
34
|
+
begin
|
35
|
+
assert_wont strip_attribute(:stripped1)
|
36
|
+
assert false
|
37
|
+
rescue
|
38
|
+
assert true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -31,7 +31,7 @@ class StripExceptThreeMockRecord < Tableless
|
|
31
31
|
strip_attributes :except => [:foo, :bar, :biz]
|
32
32
|
end
|
33
33
|
|
34
|
-
class StripAttributesTest <
|
34
|
+
class StripAttributesTest < MiniTest::Unit::TestCase
|
35
35
|
def setup
|
36
36
|
@init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " " }
|
37
37
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strip_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.1
|
5
4
|
prerelease:
|
5
|
+
version: 1.2.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:
|
12
|
+
date: 2013-01-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -20,13 +20,29 @@ dependencies:
|
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
|
-
prerelease: false
|
24
23
|
version_requirements: !ruby/object:Gem::Requirement
|
25
24
|
none: false
|
26
25
|
requirements:
|
27
26
|
- - ~>
|
28
27
|
- !ruby/object:Gem::Version
|
29
28
|
version: '3.0'
|
29
|
+
prerelease: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest-matchers
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.2'
|
38
|
+
type: :development
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '1.2'
|
45
|
+
prerelease: false
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: activerecord
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -36,13 +52,13 @@ dependencies:
|
|
36
52
|
- !ruby/object:Gem::Version
|
37
53
|
version: '3.0'
|
38
54
|
type: :development
|
39
|
-
prerelease: false
|
40
55
|
version_requirements: !ruby/object:Gem::Requirement
|
41
56
|
none: false
|
42
57
|
requirements:
|
43
58
|
- - ~>
|
44
59
|
- !ruby/object:Gem::Version
|
45
60
|
version: '3.0'
|
61
|
+
prerelease: false
|
46
62
|
- !ruby/object:Gem::Dependency
|
47
63
|
name: rake
|
48
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -50,15 +66,15 @@ dependencies:
|
|
50
66
|
requirements:
|
51
67
|
- - ~>
|
52
68
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0
|
69
|
+
version: '10.0'
|
54
70
|
type: :development
|
55
|
-
prerelease: false
|
56
71
|
version_requirements: !ruby/object:Gem::Requirement
|
57
72
|
none: false
|
58
73
|
requirements:
|
59
74
|
- - ~>
|
60
75
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0
|
76
|
+
version: '10.0'
|
77
|
+
prerelease: false
|
62
78
|
description: StripAttributes automatically strips all ActiveRecord model attributes
|
63
79
|
of leading and trailing whitespace before validation. If the attribute is blank,
|
64
80
|
it strips the value to nil.
|
@@ -68,10 +84,12 @@ executables: []
|
|
68
84
|
extensions: []
|
69
85
|
extra_rdoc_files: []
|
70
86
|
files:
|
87
|
+
- lib/strip_attributes/matchers.rb
|
71
88
|
- lib/strip_attributes/shoulda/macros.rb
|
72
89
|
- lib/strip_attributes/shoulda.rb
|
73
90
|
- lib/strip_attributes/version.rb
|
74
91
|
- lib/strip_attributes.rb
|
92
|
+
- test/matchers_test.rb
|
75
93
|
- test/strip_attributes_test.rb
|
76
94
|
- test/test_helper.rb
|
77
95
|
- README.md
|
@@ -86,25 +104,26 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
104
|
requirements:
|
87
105
|
- - ! '>='
|
88
106
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
107
|
segments:
|
91
108
|
- 0
|
92
|
-
hash:
|
109
|
+
hash: 4383093810651620729
|
110
|
+
version: '0'
|
93
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
112
|
none: false
|
95
113
|
requirements:
|
96
114
|
- - ! '>='
|
97
115
|
- !ruby/object:Gem::Version
|
98
|
-
version: '0'
|
99
116
|
segments:
|
100
117
|
- 0
|
101
|
-
hash:
|
118
|
+
hash: 4383093810651620729
|
119
|
+
version: '0'
|
102
120
|
requirements: []
|
103
|
-
rubyforge_project:
|
121
|
+
rubyforge_project:
|
104
122
|
rubygems_version: 1.8.23
|
105
123
|
signing_key:
|
106
124
|
specification_version: 3
|
107
125
|
summary: Whitespace cleanup for ActiveModel attributes
|
108
126
|
test_files:
|
127
|
+
- test/matchers_test.rb
|
109
128
|
- test/strip_attributes_test.rb
|
110
129
|
- test/test_helper.rb
|