strip_attributes 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +120 -0
- data/lib/strip_attributes/shoulda/macros.rb +6 -10
- data/lib/strip_attributes/version.rb +1 -1
- metadata +37 -10
data/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# StripAttributes [](http://travis-ci.org/rmm5t/strip_attributes)
|
|
2
|
+
|
|
3
|
+
StripAttributes is an ActiveModel extension that automatically strips all
|
|
4
|
+
attributes of leading and trailing whitespace before validation. If the
|
|
5
|
+
attribute is blank, it strips the value to `nil`.
|
|
6
|
+
|
|
7
|
+
It works by adding a before_validation hook to the record. By default, all
|
|
8
|
+
attributes are stripped of whitespace, but `:only` and `:except`
|
|
9
|
+
options can be used to limit which attributes are stripped. Both options accept
|
|
10
|
+
a single attribute (`:only => :field`) or arrays of attributes (`:except =>
|
|
11
|
+
[:field1, :field2, :field3]`).
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Include the gem in your Gemfile:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
gem "strip_attributes", "~> 1.0"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Examples
|
|
22
|
+
|
|
23
|
+
### Default Behavior
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
class DrunkPokerPlayer < ActiveRecord::Base
|
|
27
|
+
strip_attributes
|
|
28
|
+
end
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Using `except`
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
# all attributes will be stripped except :boxers
|
|
35
|
+
class SoberPokerPlayer < ActiveRecord::Base
|
|
36
|
+
strip_attributes :except => :boxers
|
|
37
|
+
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Using `only`
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
# only :shoe, :sock, and :glove attributes will be stripped
|
|
44
|
+
class ConservativePokerPlayer < ActiveRecord::Base
|
|
45
|
+
strip_attributes :only => [:shoe, :sock, :glove]
|
|
46
|
+
end
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage Patterns
|
|
50
|
+
|
|
51
|
+
### Other ORMs implementing `ActiveModel`
|
|
52
|
+
|
|
53
|
+
It also works on other ActiveModel classes, such as [Mongoid](http://mongoid.org/) documents:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
class User
|
|
57
|
+
include Mongoid::Document
|
|
58
|
+
strip_attributes :only => :email
|
|
59
|
+
end
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Outside of Rails
|
|
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.
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
require "strip_attributes/active_model"
|
|
70
|
+
class SomeModel < ActiveRecord::Base
|
|
71
|
+
strip_attributes
|
|
72
|
+
end
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Testing
|
|
76
|
+
|
|
77
|
+
StripAttributes provides shoulda-compatible macros for easier testing of
|
|
78
|
+
attribute assignment.
|
|
79
|
+
|
|
80
|
+
### Setup `test_helper.rb`
|
|
81
|
+
|
|
82
|
+
To initialize, extend `StripAttributes::Shoulda::Macros` in your
|
|
83
|
+
`test_helper.rb`:
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
require "strip_attributes/shoulda"
|
|
87
|
+
class Test::Unit::TestCase
|
|
88
|
+
extend StripAttributes::Shoulda::Macros
|
|
89
|
+
end
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Writing Tests
|
|
93
|
+
|
|
94
|
+
And in your unit tests:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
class UserTest < ActiveSupport::TestCase
|
|
98
|
+
should_strip_attributes :name, :email
|
|
99
|
+
should_not_strip_attributes :password
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Support
|
|
104
|
+
|
|
105
|
+
Submit suggestions or feature requests as a GitHub Issue or Pull
|
|
106
|
+
Request (preferred). If you send a pull request, remember to update the
|
|
107
|
+
corresponding unit tests. In fact, I prefer new features to be submitted in the
|
|
108
|
+
form of new unit tests.
|
|
109
|
+
|
|
110
|
+
## Credits
|
|
111
|
+
|
|
112
|
+
The idea was originally triggered by the information at the (now defunct) [Rails
|
|
113
|
+
Wiki](http://oldwiki.rubyonrails.org/rails/pages/HowToStripWhitespaceFromModelFields)
|
|
114
|
+
but was modified from the original to include more idiomatic ruby and rails
|
|
115
|
+
support.
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
Copyright (c) 2007-2011 Ryan McGeary released under the [MIT
|
|
120
|
+
license](http://en.wikipedia.org/wiki/MIT_License)
|
|
@@ -4,27 +4,23 @@ module StripAttributes
|
|
|
4
4
|
module Shoulda
|
|
5
5
|
module Macros
|
|
6
6
|
def should_strip_attributes(*attributes)
|
|
7
|
-
klass = described_type
|
|
8
7
|
attributes.each do |attribute|
|
|
9
8
|
attribute = attribute.to_sym
|
|
10
9
|
should "strip whitespace from #{attribute}" do
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
assert_equal "string", object.send(attribute)
|
|
10
|
+
subject.send("#{attribute}=", " string ")
|
|
11
|
+
subject.valid?
|
|
12
|
+
assert_equal "string", subject.send(attribute)
|
|
15
13
|
end
|
|
16
14
|
end
|
|
17
15
|
end
|
|
18
16
|
|
|
19
17
|
def should_not_strip_attributes(*attributes)
|
|
20
|
-
klass = described_type
|
|
21
18
|
attributes.each do |attribute|
|
|
22
19
|
attribute = attribute.to_sym
|
|
23
20
|
should "not strip whitespace from #{attribute}" do
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
assert_equal " string ", object.send(attribute)
|
|
21
|
+
subject.send("#{attribute}=", " string ")
|
|
22
|
+
subject.valid?
|
|
23
|
+
assert_equal " string ", subject.send(attribute)
|
|
28
24
|
end
|
|
29
25
|
end
|
|
30
26
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: strip_attributes
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.
|
|
4
|
+
version: 1.1.1
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,11 +9,11 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-
|
|
12
|
+
date: 2012-08-29 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activemodel
|
|
16
|
-
requirement:
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ~>
|
|
@@ -21,10 +21,15 @@ dependencies:
|
|
|
21
21
|
version: '3.0'
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements:
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '3.0'
|
|
25
30
|
- !ruby/object:Gem::Dependency
|
|
26
31
|
name: activerecord
|
|
27
|
-
requirement:
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
28
33
|
none: false
|
|
29
34
|
requirements:
|
|
30
35
|
- - ~>
|
|
@@ -32,7 +37,28 @@ dependencies:
|
|
|
32
37
|
version: '3.0'
|
|
33
38
|
type: :development
|
|
34
39
|
prerelease: false
|
|
35
|
-
version_requirements:
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ~>
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '3.0'
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: rake
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ~>
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0.9'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ~>
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0.9'
|
|
36
62
|
description: StripAttributes automatically strips all ActiveRecord model attributes
|
|
37
63
|
of leading and trailing whitespace before validation. If the attribute is blank,
|
|
38
64
|
it strips the value to nil.
|
|
@@ -48,6 +74,7 @@ files:
|
|
|
48
74
|
- lib/strip_attributes.rb
|
|
49
75
|
- test/strip_attributes_test.rb
|
|
50
76
|
- test/test_helper.rb
|
|
77
|
+
- README.md
|
|
51
78
|
homepage: https://github.com/rmm5t/strip_attributes
|
|
52
79
|
licenses: []
|
|
53
80
|
post_install_message:
|
|
@@ -62,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
62
89
|
version: '0'
|
|
63
90
|
segments:
|
|
64
91
|
- 0
|
|
65
|
-
hash:
|
|
92
|
+
hash: -1824406782290119143
|
|
66
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
94
|
none: false
|
|
68
95
|
requirements:
|
|
@@ -71,13 +98,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
71
98
|
version: '0'
|
|
72
99
|
segments:
|
|
73
100
|
- 0
|
|
74
|
-
hash:
|
|
101
|
+
hash: -1824406782290119143
|
|
75
102
|
requirements: []
|
|
76
103
|
rubyforge_project: strip_attributes
|
|
77
|
-
rubygems_version: 1.8.
|
|
104
|
+
rubygems_version: 1.8.23
|
|
78
105
|
signing_key:
|
|
79
106
|
specification_version: 3
|
|
80
|
-
summary: Whitespace cleanup for
|
|
107
|
+
summary: Whitespace cleanup for ActiveModel attributes
|
|
81
108
|
test_files:
|
|
82
109
|
- test/strip_attributes_test.rb
|
|
83
110
|
- test/test_helper.rb
|