attribute-filters 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +66 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +21 -21
- data/README.md +51 -30
- data/Rakefile +1 -1
- data/attribute-filters.gemspec +5 -5
- data/docs/HISTORY +11 -0
- data/docs/USAGE.md +37 -17
- data/lib/attribute-filters/attribute_set_attrquery.rb +7 -0
- data/lib/attribute-filters/attribute_set_query.rb +6 -0
- data/lib/attribute-filters/common_filters.rb +105 -45
- data/lib/attribute-filters/dsl_filters.rb +16 -7
- data/lib/attribute-filters/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +30 -30
- metadata.gz.sig +0 -0
data/ChangeLog
CHANGED
@@ -1,3 +1,69 @@
|
|
1
|
+
commit c1220554066705edceb097b7c26c665d26785271
|
2
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
3
|
+
Date: Fri Aug 3 00:51:17 2012 +0200
|
4
|
+
|
5
|
+
Release 1.2.2
|
6
|
+
|
7
|
+
commit 163cc3bc64a36b60ef3a1169b9921fe2224bb2b3
|
8
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
9
|
+
Date: Wed Jul 18 01:15:40 2012 +0200
|
10
|
+
|
11
|
+
Added contributor - thanx Robert!
|
12
|
+
|
13
|
+
commit 4e2b2dd088267cbb7ef7a29737807b4144e668ae
|
14
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
15
|
+
Date: Wed Jul 18 01:05:51 2012 +0200
|
16
|
+
|
17
|
+
Common filters splitted into submodules.
|
18
|
+
|
19
|
+
commit a363955e72d5c81509f8a4cf17b83869533220ff
|
20
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
21
|
+
Date: Wed Jul 18 01:05:36 2012 +0200
|
22
|
+
|
23
|
+
Filtering methods now accept AttributeSet objects as arguments.
|
24
|
+
|
25
|
+
commit 0759dbb26b766a327406103dbb57c9a363f316a5
|
26
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
27
|
+
Date: Wed Jul 18 01:04:52 2012 +0200
|
28
|
+
|
29
|
+
Added is_a? overrides in proxy classes.
|
30
|
+
|
31
|
+
commit fcb6276a9b06a981ae0c3a7e03c333dce843d66f
|
32
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
33
|
+
Date: Mon Jul 16 10:18:29 2012 +0200
|
34
|
+
|
35
|
+
Typo fixed in README
|
36
|
+
|
37
|
+
commit 6dd35c429ffbeb21348df996a854e467483678d1
|
38
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
39
|
+
Date: Tue Jul 10 12:38:22 2012 +0200
|
40
|
+
|
41
|
+
Documentation updated
|
42
|
+
|
43
|
+
commit ad796840fc35470fd2794b38f297dd23eb592d12
|
44
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
45
|
+
Date: Tue Jul 10 10:17:56 2012 +0200
|
46
|
+
|
47
|
+
Fixed sentence order in USAGE
|
48
|
+
|
49
|
+
commit b453533b1a2a0dd68a9ea2cad76d66f341b05c37
|
50
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
51
|
+
Date: Tue Jul 10 09:50:44 2012 +0200
|
52
|
+
|
53
|
+
Documentation updated
|
54
|
+
|
55
|
+
commit 8122f404f2a037b699ccd0e303f22b371e8256f6
|
56
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
57
|
+
Date: Tue Jul 10 09:45:40 2012 +0200
|
58
|
+
|
59
|
+
More fixes in README
|
60
|
+
|
61
|
+
commit ed2a5f89072e0c9c3bef85c73af90774090d155c
|
62
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
63
|
+
Date: Tue Jul 10 09:41:09 2012 +0200
|
64
|
+
|
65
|
+
Fixed some typos in documentation
|
66
|
+
|
1
67
|
commit 653b456c00b6a9825c87549f013c2d10e36c76ba
|
2
68
|
Author: Paweł Wilk <siefca@gnu.org>
|
3
69
|
Date: Tue Jul 10 09:30:09 2012 +0200
|
data/Gemfile
CHANGED
@@ -9,7 +9,7 @@ gem "activemodel", "~>3.0"
|
|
9
9
|
|
10
10
|
gem "hoe-yard", ">=0.1.2", :group => [:development, :test]
|
11
11
|
gem "rspec", ">=2.6.0", :group => [:development, :test]
|
12
|
-
gem "yard", ">=0.
|
12
|
+
gem "yard", ">=0.8.2", :group => [:development, :test]
|
13
13
|
gem "rdoc", ">=3.8.0", :group => [:development, :test]
|
14
14
|
gem "redcarpet", ">=2.1.0", :group => [:development, :test]
|
15
15
|
gem "supermodel", ">=0.1.6", :group => [:development, :test]
|
data/Gemfile.lock
CHANGED
@@ -2,9 +2,9 @@ GEM
|
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
4
|
abstract (1.0.0)
|
5
|
-
actionpack (3.0.
|
6
|
-
activemodel (= 3.0.
|
7
|
-
activesupport (= 3.0.
|
5
|
+
actionpack (3.0.16)
|
6
|
+
activemodel (= 3.0.16)
|
7
|
+
activesupport (= 3.0.16)
|
8
8
|
builder (~> 2.1.2)
|
9
9
|
erubis (~> 2.6.6)
|
10
10
|
i18n (~> 0.5.0)
|
@@ -12,16 +12,16 @@ GEM
|
|
12
12
|
rack-mount (~> 0.6.14)
|
13
13
|
rack-test (~> 0.5.7)
|
14
14
|
tzinfo (~> 0.3.23)
|
15
|
-
activemodel (3.0.
|
16
|
-
activesupport (= 3.0.
|
15
|
+
activemodel (3.0.16)
|
16
|
+
activesupport (= 3.0.16)
|
17
17
|
builder (~> 2.1.2)
|
18
18
|
i18n (~> 0.5.0)
|
19
|
-
activerecord (3.0.
|
20
|
-
activemodel (= 3.0.
|
21
|
-
activesupport (= 3.0.
|
19
|
+
activerecord (3.0.16)
|
20
|
+
activemodel (= 3.0.16)
|
21
|
+
activesupport (= 3.0.16)
|
22
22
|
arel (~> 2.0.10)
|
23
23
|
tzinfo (~> 0.3.23)
|
24
|
-
activesupport (3.0.
|
24
|
+
activesupport (3.0.16)
|
25
25
|
arel (2.0.10)
|
26
26
|
builder (2.1.2)
|
27
27
|
diff-lcs (1.1.3)
|
@@ -36,15 +36,15 @@ GEM
|
|
36
36
|
hoe-yard (0.1.2)
|
37
37
|
yard (>= 0.2.3.1)
|
38
38
|
i18n (0.5.0)
|
39
|
-
json (1.7.
|
39
|
+
json (1.7.4)
|
40
40
|
rack (1.2.5)
|
41
41
|
rack-mount (0.6.14)
|
42
42
|
rack (>= 1.0.0)
|
43
43
|
rack-test (0.5.7)
|
44
44
|
rack (>= 1.0)
|
45
|
-
railties (3.0.
|
46
|
-
actionpack (= 3.0.
|
47
|
-
activesupport (= 3.0.
|
45
|
+
railties (3.0.16)
|
46
|
+
actionpack (= 3.0.16)
|
47
|
+
activesupport (= 3.0.16)
|
48
48
|
rake (>= 0.8.7)
|
49
49
|
rdoc (~> 3.4)
|
50
50
|
thor (~> 0.14.4)
|
@@ -52,14 +52,14 @@ GEM
|
|
52
52
|
rdoc (3.12)
|
53
53
|
json (~> 1.4)
|
54
54
|
redcarpet (2.1.1)
|
55
|
-
rspec (2.
|
56
|
-
rspec-core (~> 2.
|
57
|
-
rspec-expectations (~> 2.
|
58
|
-
rspec-mocks (~> 2.
|
59
|
-
rspec-core (2.
|
60
|
-
rspec-expectations (2.
|
55
|
+
rspec (2.11.0)
|
56
|
+
rspec-core (~> 2.11.0)
|
57
|
+
rspec-expectations (~> 2.11.0)
|
58
|
+
rspec-mocks (~> 2.11.0)
|
59
|
+
rspec-core (2.11.1)
|
60
|
+
rspec-expectations (2.11.2)
|
61
61
|
diff-lcs (~> 1.1.3)
|
62
|
-
rspec-mocks (2.
|
62
|
+
rspec-mocks (2.11.1)
|
63
63
|
supermodel (0.1.6)
|
64
64
|
activemodel (~> 3.0.0)
|
65
65
|
thor (0.14.6)
|
@@ -82,4 +82,4 @@ DEPENDENCIES
|
|
82
82
|
redcarpet (>= 2.1.0)
|
83
83
|
rspec (>= 2.6.0)
|
84
84
|
supermodel (>= 0.1.6)
|
85
|
-
yard (>= 0.
|
85
|
+
yard (>= 0.8.2)
|
data/README.md
CHANGED
@@ -5,15 +5,16 @@ Attribute Filters for Rails
|
|
5
5
|
|
6
6
|
* https://rubygems.org/gems/attribute-filters
|
7
7
|
* https://github.com/siefca/attribute-filters/tree
|
8
|
-
*
|
8
|
+
* pw@gnu.org
|
9
9
|
|
10
10
|
|
11
11
|
Summary
|
12
12
|
-------
|
13
13
|
|
14
|
-
Attribute Filters adds couple of DSL keywords
|
15
|
-
to Rails thereby allowing you
|
16
|
-
|
14
|
+
Attribute Filters extension adds couple of DSL keywords
|
15
|
+
and some syntactic sugar to Rails, thereby allowing you
|
16
|
+
to express filtering and grouping model attributes
|
17
|
+
in a concise and clean way.
|
17
18
|
|
18
19
|
When?
|
19
20
|
-----
|
@@ -21,15 +22,15 @@ When?
|
|
21
22
|
You may want to try it when your Rails application often modifies
|
22
23
|
attribute values that changed recently and uses callbacks to do that.
|
23
24
|
|
24
|
-
When the number of attributes that are altered in such a way increases
|
25
|
-
|
25
|
+
When the number of attributes that are altered in such a way increases,
|
26
|
+
you can observe the same thing happening with your filtering
|
26
27
|
methods. That's because each one is tied to some attribute.
|
27
28
|
|
28
29
|
To refine that process you may write more generic methods
|
29
30
|
for altering attributes. They should be designed to handle
|
30
|
-
common operations and not tied to certain attributes.
|
31
|
+
common operations and not be tied to certain attributes.
|
31
32
|
|
32
|
-
|
33
|
+
Let's see that in action.
|
33
34
|
|
34
35
|
### Before ###
|
35
36
|
|
@@ -100,27 +101,20 @@ or even shorter:
|
|
100
101
|
|
101
102
|
```ruby
|
102
103
|
class User < ActiveRecord::Base
|
103
|
-
include ActiveModel::AttributeFilters::Common
|
104
|
+
include ActiveModel::AttributeFilters::Common::Strip
|
105
|
+
include ActiveModel::AttributeFilters::Common::Downcase
|
106
|
+
include ActiveModel::AttributeFilters::Common::Titleize
|
104
107
|
|
105
|
-
attributes_that should_be_stripped:
|
106
|
-
attributes_that should_be_downcased:
|
107
|
-
attributes_that
|
108
|
+
attributes_that should_be_stripped: [ :username, :email, :real_name ]
|
109
|
+
attributes_that should_be_downcased: [ :username, :email ]
|
110
|
+
attributes_that should_be_titleized: [ :real_name ]
|
108
111
|
|
109
112
|
before_validation :strip_attributes
|
110
113
|
before_validation :downcase_attributes
|
111
|
-
before_validation :
|
114
|
+
before_validation :titleize_attributes
|
112
115
|
end
|
113
116
|
```
|
114
117
|
|
115
|
-
Attributes that have to be altered may be simply added to the attribute sets
|
116
|
-
that you define and then filtered with generic methods. You can use
|
117
|
-
these methods in all your models if you wish.
|
118
|
-
|
119
|
-
The last action can be performed by putting the filtering methods into
|
120
|
-
some base class that models inherit form or (better) into your own
|
121
|
-
handy module that is included in your models. Alternatively you can
|
122
|
-
use predefined filters from `ActiveModel::AttributeFilters::Common` module.
|
123
|
-
|
124
118
|
If you would rather like to group filters by attribute names then
|
125
119
|
the alternative syntax may be helpful:
|
126
120
|
|
@@ -132,8 +126,18 @@ class User < ActiveRecord::Base
|
|
132
126
|
end
|
133
127
|
```
|
134
128
|
|
135
|
-
|
136
|
-
|
129
|
+
Attributes that should be altered may be simply added
|
130
|
+
to the attribute sets that you define and then filtered
|
131
|
+
with generic methods. You can use these methods in all
|
132
|
+
your models if you wish.
|
133
|
+
|
134
|
+
The last action can be performed by putting the filtering methods into
|
135
|
+
some base class that all your models inherit form or (better) into your own
|
136
|
+
handy module that is included in all your models. Alternatively you can
|
137
|
+
use predefined filters from `ActiveModel::AttributeFilters::Common` module.
|
138
|
+
|
139
|
+
More examples and usage
|
140
|
+
-----------------------
|
137
141
|
|
138
142
|
You can use it to filter attributes (as presented above) but you can also
|
139
143
|
use it to express some logic
|
@@ -142,22 +146,38 @@ use it to express some logic
|
|
142
146
|
* See [USAGE](http://rubydoc.info/gems/attribute-filters/file/docs/USAGE.md) for examples and detailed information about the usage.
|
143
147
|
* See [whole documentation](http://rubydoc.info/gems/attribute-filters/) to browse all documents.
|
144
148
|
|
149
|
+
### Sneak peeks ###
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
@user.attributes_that(:should_be_stripped).all.present?
|
153
|
+
# => false
|
154
|
+
|
155
|
+
@user.attributes_that(:should_be_stripped).list.present?
|
156
|
+
# => #<ActiveModel::AttributeSet: {"username", "email"}>
|
157
|
+
|
158
|
+
@user.the_attribute(:username).should_be_stripped?
|
159
|
+
# => true
|
160
|
+
|
161
|
+
@user.the_attribute(:username).list.sets
|
162
|
+
# => #<ActiveModel::AttributeSet: {:should_be_downcased, :should_be_stripped}>
|
163
|
+
```
|
164
|
+
|
145
165
|
How it works?
|
146
166
|
-------------
|
147
167
|
|
148
168
|
It creates a new Active Model submodule called `AttributeFilters`. That module
|
149
169
|
contains the needed DSL that goes into your models. It also creates `ActiveModel::AttributeSet`
|
150
|
-
class which is just a new kind of set, a structure for storing
|
170
|
+
class which is just a new kind of set, a structure for storing attribute names.
|
151
171
|
|
152
|
-
Then it forces Rails to include the AttributeFilters in any model that
|
153
|
-
at any time
|
154
|
-
quite often; e.g. Active Record and other popular ORM-s use it. (I'm calling
|
172
|
+
Then it forces Rails to include the `ActiveModel::AttributeFilters` in any model that
|
173
|
+
at any time includes `ActiveModel::AttributeMethods`. The last one is included
|
174
|
+
quite often; e.g. Active Record and other popular ORM-s use it. (I'm calling that thechnique
|
155
175
|
"the accompanying module".)
|
156
176
|
|
157
177
|
That's why you can make use of attribute filters without explicitly including
|
158
|
-
the module, as long as your application
|
178
|
+
the module, as long as your application uses some popular ORM.
|
159
179
|
|
160
|
-
However, if something
|
180
|
+
However, if something goes wrong or your application is somehow unusual, you can always
|
161
181
|
include the `AttributeFilters` module manually in any of your models:
|
162
182
|
|
163
183
|
```ruby
|
@@ -217,6 +237,7 @@ Credits
|
|
217
237
|
|
218
238
|
* [iConsulting](http://www.iconsulting.pl/) supports Free Software and has contributed to this library by paying for me to eat when I've been coding.
|
219
239
|
* [MrZYX (Jonne Haß)](https://github.com/MrZYX) contributed by giving me some hints and answering basic questions on IRC – THX!
|
240
|
+
* [Robert Pankowecki](https://github.com/paneq/) contributed by suggesting selective inclusion of filtering helpers
|
220
241
|
|
221
242
|
License
|
222
243
|
-------
|
data/Rakefile
CHANGED
@@ -45,7 +45,7 @@ Hoe.spec 'attribute-filters' do
|
|
45
45
|
extra_deps << ['railties', '~> 3.0'] <<
|
46
46
|
['activemodel', '~> 3.0']
|
47
47
|
extra_dev_deps << ['rspec', '>= 2.6.0'] <<
|
48
|
-
['yard', '>= 0.
|
48
|
+
['yard', '>= 0.8.2'] <<
|
49
49
|
['rdoc', '>= 3.8.0'] <<
|
50
50
|
['redcarpet', '>= 2.1.0'] <<
|
51
51
|
['supermodel', '>= 0.1.6'] <<
|
data/attribute-filters.gemspec
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "attribute-filters"
|
5
|
-
s.version = "1.2.
|
5
|
+
s.version = "1.2.2.20120803004107"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Pawe\u{142} Wilk"]
|
9
9
|
s.cert_chain = ["/Users/siefca/.gem/gem-public_cert.pem"]
|
10
|
-
s.date = "2012-
|
10
|
+
s.date = "2012-08-02"
|
11
11
|
s.description = "Concise way of filtering model attributes in Rails."
|
12
12
|
s.email = ["pw@gnu.org"]
|
13
13
|
s.extra_rdoc_files = ["Manifest.txt"]
|
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
s.add_runtime_dependency(%q<activemodel>, ["~> 3.0"])
|
29
29
|
s.add_development_dependency(%q<hoe-yard>, [">= 0.1.2"])
|
30
30
|
s.add_development_dependency(%q<rspec>, [">= 2.6.0"])
|
31
|
-
s.add_development_dependency(%q<yard>, [">= 0.
|
31
|
+
s.add_development_dependency(%q<yard>, [">= 0.8.2"])
|
32
32
|
s.add_development_dependency(%q<rdoc>, [">= 3.8.0"])
|
33
33
|
s.add_development_dependency(%q<redcarpet>, [">= 2.1.0"])
|
34
34
|
s.add_development_dependency(%q<supermodel>, [">= 0.1.6"])
|
@@ -42,7 +42,7 @@ Gem::Specification.new do |s|
|
|
42
42
|
s.add_dependency(%q<activemodel>, ["~> 3.0"])
|
43
43
|
s.add_dependency(%q<hoe-yard>, [">= 0.1.2"])
|
44
44
|
s.add_dependency(%q<rspec>, [">= 2.6.0"])
|
45
|
-
s.add_dependency(%q<yard>, [">= 0.
|
45
|
+
s.add_dependency(%q<yard>, [">= 0.8.2"])
|
46
46
|
s.add_dependency(%q<rdoc>, [">= 3.8.0"])
|
47
47
|
s.add_dependency(%q<redcarpet>, [">= 2.1.0"])
|
48
48
|
s.add_dependency(%q<supermodel>, [">= 0.1.6"])
|
@@ -57,7 +57,7 @@ Gem::Specification.new do |s|
|
|
57
57
|
s.add_dependency(%q<activemodel>, ["~> 3.0"])
|
58
58
|
s.add_dependency(%q<hoe-yard>, [">= 0.1.2"])
|
59
59
|
s.add_dependency(%q<rspec>, [">= 2.6.0"])
|
60
|
-
s.add_dependency(%q<yard>, [">= 0.
|
60
|
+
s.add_dependency(%q<yard>, [">= 0.8.2"])
|
61
61
|
s.add_dependency(%q<rdoc>, [">= 3.8.0"])
|
62
62
|
s.add_dependency(%q<redcarpet>, [">= 2.1.0"])
|
63
63
|
s.add_dependency(%q<supermodel>, [">= 0.1.6"])
|
data/docs/HISTORY
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
=== 1.2.2 / 2012-08-03
|
2
|
+
|
3
|
+
* major enhancements
|
4
|
+
|
5
|
+
* Common filtering methods modularized
|
6
|
+
|
7
|
+
* minor enhancements
|
8
|
+
|
9
|
+
* Added is_a?() overrides in proxy classes
|
10
|
+
* Method attributes_to_filter now also takes attribute set as an argument
|
11
|
+
|
1
12
|
=== 1.2.1 / 2012-07-09
|
2
13
|
|
3
14
|
* minor bugfixes
|
data/docs/USAGE.md
CHANGED
@@ -8,8 +8,8 @@ Attribute set is a set of attribute names. It's like an array
|
|
8
8
|
or, to be exact, like a set (a hash that can only have true values
|
9
9
|
assigned to elements in order to just know whether the key exists or not).
|
10
10
|
|
11
|
-
Attribute sets have simple function; **they group attribute names**. What
|
12
|
-
|
11
|
+
Attribute sets have simple function; **they group attribute names**. What can
|
12
|
+
you do with that? For example you can use it to perform some tasks
|
13
13
|
on all attributes that are listed in a set.
|
14
14
|
|
15
15
|
### Data structures ###
|
@@ -28,7 +28,7 @@ a method that returns a set of attributes or even
|
|
28
28
|
a set of set names, the returned value will probably
|
29
29
|
be an instance of the class `AttributeSet`.
|
30
30
|
|
31
|
-
Note that when sets are returned the
|
31
|
+
Note that when sets are returned the convention is that:
|
32
32
|
|
33
33
|
* **attribute names are strings**
|
34
34
|
* **set names are symbols**
|
@@ -314,7 +314,7 @@ practice there are two groups of methods with two sets of DSL features.
|
|
314
314
|
First group (querying attribute sets):
|
315
315
|
|
316
316
|
* [`attribute_set`](#attribute_set_set_name_0) and aliases
|
317
|
-
* [`attributes_to_filter`](attributes_to_filter_set_name_____)
|
317
|
+
* [`attributes_to_filter`](#attributes_to_filter_set_name_____)
|
318
318
|
|
319
319
|
Second group (querying attributes for sets they belong to):
|
320
320
|
|
@@ -495,7 +495,7 @@ Example:
|
|
495
495
|
* **`set?`**
|
496
496
|
* **`is_one_that?`**
|
497
497
|
* **`one_that?`**
|
498
|
-
* **`that
|
498
|
+
* **`that?`**
|
499
499
|
|
500
500
|
These methods allow to test if the attribute belongs to the given
|
501
501
|
attribute set or sets. You may consider the methods above aliases
|
@@ -678,7 +678,7 @@ Instead of `filter_attrs_from_set` you may also use one of the aliases:
|
|
678
678
|
* `attribute_call_for_set`, `call_attrs_from_set`,
|
679
679
|
`for_attributes_which`, `for_attributes_that`, `for_attributes_that_are`, `for_attributes_which_are`
|
680
680
|
|
681
|
-
#### `attributes_to_filter(
|
681
|
+
#### `attributes_to_filter(set,...)` ####
|
682
682
|
|
683
683
|
The [`attributes_to_filter`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters:attributes_to_filter)
|
684
684
|
method is used for **getting the attributes that should be filtered**.
|
@@ -687,9 +687,9 @@ to be processed but you can use it on your own. It returns an `AttributeSet` ins
|
|
687
687
|
attribute names that match the given criteria (by default: that are in a set of the given name,
|
688
688
|
that are present and that have changed lately).
|
689
689
|
|
690
|
-
The method takes one manatory argument (`
|
691
|
-
that can be `true` or `false` (default). The first optional argument (`process_all`),
|
692
|
-
|
690
|
+
The method takes one manatory argument (`set`) and two optional arguments (`process_all` and `no_presence_check`)
|
691
|
+
that can be `true` or `false` (default). The `set` may be an object which is kind of String, Symbol (in that case it should contain the name of a set) or it can be an object which is a kind of `AttributeSet` (in that case it should contain a proper object). The first optional argument (`process_all`), when set to `true`, forces method to return also the attributes that haven't changed lately.
|
692
|
+
|
693
693
|
By default the result will be narrowed to the attributes that have changed and haven't been saved yet.
|
694
694
|
The second optional argument (`no_presence_check`) will tell the method to omit the presence check
|
695
695
|
for each attribute. By default only the attributes that are real attributes (are present
|
@@ -762,29 +762,49 @@ or pass their names to callback hooks.
|
|
762
762
|
|
763
763
|
To use predefined filters you have to manually
|
764
764
|
include the [`ActiveModel::AttributeFilters::Common`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common)
|
765
|
-
module.
|
765
|
+
module. If you don't want to include portions of code that you'll never use, you can also include some filters selectively. To do that just include just a submodule containing certain filtering method.
|
766
766
|
|
767
767
|
Here is a list of the predefined filtering methods:
|
768
768
|
|
769
|
-
*
|
770
|
-
*
|
771
|
-
*
|
772
|
-
*
|
773
|
-
*
|
769
|
+
* `capitalize_attributes` (submodule: `Capitalize`)
|
770
|
+
* `fully_capitalize_attributes` (submodule: `Capitalize`)
|
771
|
+
* `titleize_attributes` (submodule: `Titleize`)
|
772
|
+
* `downcase_attributes` (submodule: `Downcase` or `Case`)
|
773
|
+
* `upcase_attributes` (submodule: `Upcase` or `Case`)
|
774
|
+
* `strip_attributes` (submodule: `Strip`)
|
775
|
+
* `squeeze_attributes` (submodule: `Squeeze`)
|
774
776
|
|
775
777
|
Example:
|
776
778
|
|
777
779
|
```ruby
|
778
780
|
class User < ActiveRecord::Base
|
779
781
|
include ActiveModel::AttributeFilters::Common
|
782
|
+
|
783
|
+
the_attribute user: [:should_be_stripped, :should_be_downcased ]
|
784
|
+
the_attribute email: [:should_be_stripped, :should_be_downcased ]
|
785
|
+
the_attribute name: [:should_be_stripped, :should_be_downcased, :should_be_titleized ]
|
786
|
+
|
787
|
+
before_validation :strip_attributes
|
788
|
+
before_validation :downcase_attributes
|
789
|
+
before_validation :titleize_attributes
|
790
|
+
end
|
791
|
+
```
|
792
|
+
|
793
|
+
or (better):
|
780
794
|
|
795
|
+
```ruby
|
796
|
+
class User < ActiveRecord::Base
|
797
|
+
include ActiveModel::AttributeFilters::Common::Stip
|
798
|
+
include ActiveModel::AttributeFilters::Common::Downcase
|
799
|
+
include ActiveModel::AttributeFilters::Common::Titleize
|
800
|
+
|
781
801
|
the_attribute user: [:should_be_stripped, :should_be_downcased ]
|
782
802
|
the_attribute email: [:should_be_stripped, :should_be_downcased ]
|
783
|
-
the_attribute name: [:should_be_stripped, :should_be_downcased, :
|
803
|
+
the_attribute name: [:should_be_stripped, :should_be_downcased, :should_be_titleized ]
|
784
804
|
|
785
805
|
before_validation :strip_attributes
|
786
806
|
before_validation :downcase_attributes
|
787
|
-
before_validation :
|
807
|
+
before_validation :titleize_attributes
|
788
808
|
end
|
789
809
|
```
|
790
810
|
|
@@ -69,6 +69,13 @@ module ActiveModel
|
|
69
69
|
@set_object.respond_to?(name) || name.to_s.slice(-1,1) == '?'
|
70
70
|
end
|
71
71
|
end
|
72
|
+
|
73
|
+
# @private
|
74
|
+
def is_a?(klass)
|
75
|
+
super || @set_object.is_a?(klass)
|
76
|
+
end
|
77
|
+
alias_method :kind_of?, :is_a?
|
78
|
+
|
72
79
|
end # class AttrQuery
|
73
80
|
end # class AttributeSet
|
74
81
|
end # module ActiveModel
|
@@ -12,68 +12,128 @@ module ActiveModel
|
|
12
12
|
module AttributeFilters
|
13
13
|
# This module contains common, ready-to-use filtering methods.
|
14
14
|
module Common
|
15
|
+
|
15
16
|
# Strips attributes from leading and trailing spaces.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
module Strip
|
18
|
+
# Strips attributes from leading and trailing spaces.
|
19
|
+
#
|
20
|
+
# The attrubutes to be stripped are taken from the attribute set called
|
21
|
+
# +should_be_stripped+. It operates directly on attribute's contents.
|
22
|
+
#
|
23
|
+
# @return [void]
|
24
|
+
def strip_attributes
|
25
|
+
filter_attrs_from_set(:should_be_stripped) { |atr| atr.strip }
|
26
|
+
end
|
23
27
|
end
|
24
28
|
|
25
29
|
# Downcases attributes.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
module Downcase
|
31
|
+
# Downcases attributes.
|
32
|
+
#
|
33
|
+
# The attrubutes to be downcased are taken from the attribute set
|
34
|
+
# called +should_be_downcased+. This method is safe to be
|
35
|
+
# used with multibyte strings (containing diacritics).
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
def downcase_attributes
|
39
|
+
filter_attrs_from_set(:should_be_downcased) do |atr|
|
40
|
+
atr.mb_chars.downcase.to_s
|
41
|
+
end
|
35
42
|
end
|
36
43
|
end
|
37
44
|
|
38
45
|
# Upcases attributes.
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
module Upcase
|
47
|
+
# Upcases attributes.
|
48
|
+
#
|
49
|
+
# The attrubutes to be upcased are taken from the attribute set
|
50
|
+
# called +should_be_upcased+. This method is safe to be
|
51
|
+
# used with multibyte strings (containing diacritics).
|
52
|
+
#
|
53
|
+
# @return [void]
|
54
|
+
def upcase_attributes
|
55
|
+
filter_attrs_from_set(:should_be_upcased) do |atr|
|
56
|
+
atr.mb_chars.upcase.to_s
|
57
|
+
end
|
48
58
|
end
|
49
59
|
end
|
50
60
|
|
51
|
-
#
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
#
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
+
# Operates on attributes' case.
|
62
|
+
module Case
|
63
|
+
include Upcase
|
64
|
+
include Downcase
|
65
|
+
end
|
66
|
+
|
67
|
+
# Capitalizes attributes.
|
68
|
+
module Capitalize
|
69
|
+
# Capitalizes attributes.
|
70
|
+
#
|
71
|
+
# The attrubutes to be capitalized are taken from the attribute set
|
72
|
+
# called +should_be_capitalized+. This method is safe to be
|
73
|
+
# used with multibyte strings (containing diacritics).
|
74
|
+
#
|
75
|
+
# @return [void]
|
76
|
+
def capitalize_attributes
|
77
|
+
filter_attrs_from_set(:should_be_capitalized) do |atr|
|
78
|
+
atr.mb_chars.capitalize.to_s
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Fully capitalizes attributes (capitalizes each word and squeezes spaces).
|
83
|
+
#
|
84
|
+
# The attrubutes to be fully capitalized are taken from the attribute set
|
85
|
+
# called +should_be_fully_capitalized+ and from set +should_be_titleized+.
|
86
|
+
# This method is safe to be used with multibyte strings (containing diacritics).
|
87
|
+
#
|
88
|
+
# @return [void]
|
89
|
+
def titleize_with_squeezed_spaces
|
90
|
+
s = attribute_set(:should_be_fully_capitalized) + attribute_set(:should_be_titleized)
|
91
|
+
filter_attrs_from_set(s) do |atr|
|
92
|
+
atr.mb_chars.split(' ').map { |n| n.capitalize }.join(' ')
|
93
|
+
end
|
61
94
|
end
|
95
|
+
alias_method :fully_capitalize_attributes, :titleize_with_squeezed_spaces
|
96
|
+
|
62
97
|
end
|
63
98
|
|
64
|
-
#
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
99
|
+
# Squeezes white characters in attributes.
|
100
|
+
module Squeeze
|
101
|
+
# Squeezes white characters in attributes.
|
102
|
+
#
|
103
|
+
# The attrubutes to be squeezed are taken from the attribute set
|
104
|
+
# called +should_be_squeezed+. This method is safe to be
|
105
|
+
# used with multibyte strings (containing diacritics).
|
106
|
+
#
|
107
|
+
# @return [void]
|
108
|
+
def squeeze_attributes
|
109
|
+
filter_attrs_from_set(:should_be_squeezed) do |atr|
|
110
|
+
atr.mb_chars.squeeze.to_s
|
111
|
+
end
|
74
112
|
end
|
75
113
|
end
|
76
114
|
|
115
|
+
# Titleizes attributes.
|
116
|
+
module Titleize
|
117
|
+
# Titleizes attributes.
|
118
|
+
#
|
119
|
+
# The attrubutes to be titleized are taken from the attribute set
|
120
|
+
# called +should_be_titleized+. This method is safe to be
|
121
|
+
# used with multibyte strings (containing diacritics).
|
122
|
+
#
|
123
|
+
# @return [void]
|
124
|
+
def titleize_attributes
|
125
|
+
filter_attrs_from_set(:should_be_titleized) do |atr|
|
126
|
+
atr.mb_chars.titleize.to_s
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
include Case;
|
132
|
+
include Strip;
|
133
|
+
include Capitalize;
|
134
|
+
include Titleize;
|
135
|
+
include Squeeze;
|
136
|
+
|
77
137
|
end # module Common
|
78
138
|
end # module AttributeFilters
|
79
139
|
end # module ActiveModel
|
@@ -20,14 +20,23 @@ module ActiveModel
|
|
20
20
|
# selecting attributes that are meeting certain criteria and belong
|
21
21
|
# to the given attribute set.
|
22
22
|
#
|
23
|
-
# @
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
23
|
+
# @overload attributes_to_filter(set_name, process_all, no_presence_check)
|
24
|
+
# @param set_name [String,Symbol] name of a set of attributes used to get attributes
|
25
|
+
# @param process_all [Boolean] if set then all the attributes from the attribute set are selected,
|
26
|
+
# not just attributes that has changed (defaults to +false+)
|
27
|
+
# @param no_presence_check [Boolean] if set then the checking whether attribute exists will be
|
28
|
+
# disabled (matters only when +process_all+ is also set) (defaults to +false+)
|
29
|
+
# @return [AttributeSet] set of attributes (attribute name => previous_value)
|
30
|
+
#
|
31
|
+
# @overload attributes_to_filter(attribute_set, process_all, no_presence_check)
|
32
|
+
# @param attribute_set [AttributeSet] set of attributes used to get attributes
|
33
|
+
# @param process_all [Boolean] if set then all the attributes from the attribute set are selected,
|
34
|
+
# not just attributes that has changed (defaults to +false+)
|
35
|
+
# @param no_presence_check [Boolean] if set then the checking whether attribute exists will be
|
36
|
+
# disabled (matters only when +process_all+ is also set) (defaults to +false+)
|
37
|
+
# @return [AttributeSet] set of attributes (attribute name => previous_value)
|
29
38
|
def attributes_to_filter(set_name, process_all = false, no_presence_check = false)
|
30
|
-
atf = attribute_set(set_name)
|
39
|
+
atf = set_name.is_a?(::ActiveModel::AttributeSet) ? set_name : attribute_set(set_name)
|
31
40
|
if process_all
|
32
41
|
no_presence_check ? atf : atf & (__vatrf(no_presence_check) + attributes.keys)
|
33
42
|
else
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute-filters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -48,11 +48,11 @@ cert_chain:
|
|
48
48
|
-----END CERTIFICATE-----
|
49
49
|
|
50
50
|
'
|
51
|
-
date: 2012-
|
51
|
+
date: 2012-08-02 00:00:00.000000000 Z
|
52
52
|
dependencies:
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: railties
|
55
|
-
requirement: &
|
55
|
+
requirement: &2155091400 !ruby/object:Gem::Requirement
|
56
56
|
none: false
|
57
57
|
requirements:
|
58
58
|
- - ~>
|
@@ -60,10 +60,10 @@ dependencies:
|
|
60
60
|
version: '3.0'
|
61
61
|
type: :runtime
|
62
62
|
prerelease: false
|
63
|
-
version_requirements: *
|
63
|
+
version_requirements: *2155091400
|
64
64
|
- !ruby/object:Gem::Dependency
|
65
65
|
name: activemodel
|
66
|
-
requirement: &
|
66
|
+
requirement: &2155090940 !ruby/object:Gem::Requirement
|
67
67
|
none: false
|
68
68
|
requirements:
|
69
69
|
- - ~>
|
@@ -71,10 +71,10 @@ dependencies:
|
|
71
71
|
version: '3.0'
|
72
72
|
type: :runtime
|
73
73
|
prerelease: false
|
74
|
-
version_requirements: *
|
74
|
+
version_requirements: *2155090940
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
76
|
name: hoe-yard
|
77
|
-
requirement: &
|
77
|
+
requirement: &2155090500 !ruby/object:Gem::Requirement
|
78
78
|
none: false
|
79
79
|
requirements:
|
80
80
|
- - ! '>='
|
@@ -82,10 +82,10 @@ dependencies:
|
|
82
82
|
version: 0.1.2
|
83
83
|
type: :development
|
84
84
|
prerelease: false
|
85
|
-
version_requirements: *
|
85
|
+
version_requirements: *2155090500
|
86
86
|
- !ruby/object:Gem::Dependency
|
87
87
|
name: rspec
|
88
|
-
requirement: &
|
88
|
+
requirement: &2155090020 !ruby/object:Gem::Requirement
|
89
89
|
none: false
|
90
90
|
requirements:
|
91
91
|
- - ! '>='
|
@@ -93,21 +93,21 @@ dependencies:
|
|
93
93
|
version: 2.6.0
|
94
94
|
type: :development
|
95
95
|
prerelease: false
|
96
|
-
version_requirements: *
|
96
|
+
version_requirements: *2155090020
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: yard
|
99
|
-
requirement: &
|
99
|
+
requirement: &2155089540 !ruby/object:Gem::Requirement
|
100
100
|
none: false
|
101
101
|
requirements:
|
102
102
|
- - ! '>='
|
103
103
|
- !ruby/object:Gem::Version
|
104
|
-
version: 0.
|
104
|
+
version: 0.8.2
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
|
-
version_requirements: *
|
107
|
+
version_requirements: *2155089540
|
108
108
|
- !ruby/object:Gem::Dependency
|
109
109
|
name: rdoc
|
110
|
-
requirement: &
|
110
|
+
requirement: &2155089100 !ruby/object:Gem::Requirement
|
111
111
|
none: false
|
112
112
|
requirements:
|
113
113
|
- - ! '>='
|
@@ -115,10 +115,10 @@ dependencies:
|
|
115
115
|
version: 3.8.0
|
116
116
|
type: :development
|
117
117
|
prerelease: false
|
118
|
-
version_requirements: *
|
118
|
+
version_requirements: *2155089100
|
119
119
|
- !ruby/object:Gem::Dependency
|
120
120
|
name: redcarpet
|
121
|
-
requirement: &
|
121
|
+
requirement: &2155088660 !ruby/object:Gem::Requirement
|
122
122
|
none: false
|
123
123
|
requirements:
|
124
124
|
- - ! '>='
|
@@ -126,10 +126,10 @@ dependencies:
|
|
126
126
|
version: 2.1.0
|
127
127
|
type: :development
|
128
128
|
prerelease: false
|
129
|
-
version_requirements: *
|
129
|
+
version_requirements: *2155088660
|
130
130
|
- !ruby/object:Gem::Dependency
|
131
131
|
name: supermodel
|
132
|
-
requirement: &
|
132
|
+
requirement: &2155088220 !ruby/object:Gem::Requirement
|
133
133
|
none: false
|
134
134
|
requirements:
|
135
135
|
- - ! '>='
|
@@ -137,10 +137,10 @@ dependencies:
|
|
137
137
|
version: 0.1.6
|
138
138
|
type: :development
|
139
139
|
prerelease: false
|
140
|
-
version_requirements: *
|
140
|
+
version_requirements: *2155088220
|
141
141
|
- !ruby/object:Gem::Dependency
|
142
142
|
name: activerecord
|
143
|
-
requirement: &
|
143
|
+
requirement: &2155087780 !ruby/object:Gem::Requirement
|
144
144
|
none: false
|
145
145
|
requirements:
|
146
146
|
- - ! '>='
|
@@ -148,10 +148,10 @@ dependencies:
|
|
148
148
|
version: '3.0'
|
149
149
|
type: :development
|
150
150
|
prerelease: false
|
151
|
-
version_requirements: *
|
151
|
+
version_requirements: *2155087780
|
152
152
|
- !ruby/object:Gem::Dependency
|
153
153
|
name: bundler
|
154
|
-
requirement: &
|
154
|
+
requirement: &2155087340 !ruby/object:Gem::Requirement
|
155
155
|
none: false
|
156
156
|
requirements:
|
157
157
|
- - ! '>='
|
@@ -159,10 +159,10 @@ dependencies:
|
|
159
159
|
version: 1.0.10
|
160
160
|
type: :development
|
161
161
|
prerelease: false
|
162
|
-
version_requirements: *
|
162
|
+
version_requirements: *2155087340
|
163
163
|
- !ruby/object:Gem::Dependency
|
164
164
|
name: hoe-bundler
|
165
|
-
requirement: &
|
165
|
+
requirement: &2155086900 !ruby/object:Gem::Requirement
|
166
166
|
none: false
|
167
167
|
requirements:
|
168
168
|
- - ! '>='
|
@@ -170,10 +170,10 @@ dependencies:
|
|
170
170
|
version: 1.1.0
|
171
171
|
type: :development
|
172
172
|
prerelease: false
|
173
|
-
version_requirements: *
|
173
|
+
version_requirements: *2155086900
|
174
174
|
- !ruby/object:Gem::Dependency
|
175
175
|
name: hoe-gemspec
|
176
|
-
requirement: &
|
176
|
+
requirement: &2155102820 !ruby/object:Gem::Requirement
|
177
177
|
none: false
|
178
178
|
requirements:
|
179
179
|
- - ! '>='
|
@@ -181,10 +181,10 @@ dependencies:
|
|
181
181
|
version: 1.0.0
|
182
182
|
type: :development
|
183
183
|
prerelease: false
|
184
|
-
version_requirements: *
|
184
|
+
version_requirements: *2155102820
|
185
185
|
- !ruby/object:Gem::Dependency
|
186
186
|
name: hoe
|
187
|
-
requirement: &
|
187
|
+
requirement: &2155102380 !ruby/object:Gem::Requirement
|
188
188
|
none: false
|
189
189
|
requirements:
|
190
190
|
- - ~>
|
@@ -192,7 +192,7 @@ dependencies:
|
|
192
192
|
version: '2.16'
|
193
193
|
type: :development
|
194
194
|
prerelease: false
|
195
|
-
version_requirements: *
|
195
|
+
version_requirements: *2155102380
|
196
196
|
description: Concise way of filtering model attributes in Rails.
|
197
197
|
email:
|
198
198
|
- pw@gnu.org
|
@@ -252,7 +252,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
252
252
|
version: '0'
|
253
253
|
segments:
|
254
254
|
- 0
|
255
|
-
hash:
|
255
|
+
hash: -2940101135055655246
|
256
256
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
257
257
|
none: false
|
258
258
|
requirements:
|
metadata.gz.sig
CHANGED
Binary file
|