attribute-filters 1.4.0 → 2.0.0
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.tar.gz.sig +0 -0
- data/.yardopts +1 -0
- data/ChangeLog +501 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +7 -7
- data/Manifest.txt +7 -0
- data/README.md +59 -30
- data/Rakefile +1 -0
- data/attribute-filters.gemspec +4 -4
- data/docs/COMMON-FILTERS.md +1067 -0
- data/docs/HISTORY +42 -0
- data/docs/TODO +29 -12
- data/docs/USAGE.md +718 -818
- data/lib/attribute-filters.rb +4 -2
- data/lib/attribute-filters/attribute_set.rb +144 -73
- data/lib/attribute-filters/attribute_set_annotations.rb +110 -77
- data/lib/attribute-filters/attribute_set_attrquery.rb +51 -8
- data/lib/attribute-filters/attribute_set_enum.rb +44 -38
- data/lib/attribute-filters/attribute_set_query.rb +62 -12
- data/lib/attribute-filters/backports.rb +36 -4
- data/lib/attribute-filters/common_filters.rb +83 -37
- data/lib/attribute-filters/common_filters/bare.rb +29 -0
- data/lib/attribute-filters/common_filters/case.rb +34 -19
- data/lib/attribute-filters/common_filters/convert.rb +259 -0
- data/lib/attribute-filters/common_filters/join.rb +37 -30
- data/lib/attribute-filters/common_filters/order.rb +105 -0
- data/lib/attribute-filters/common_filters/pick.rb +90 -0
- data/lib/attribute-filters/common_filters/presence.rb +70 -0
- data/lib/attribute-filters/common_filters/split.rb +37 -21
- data/lib/attribute-filters/common_filters/squeeze.rb +28 -9
- data/lib/attribute-filters/common_filters/strip.rb +7 -3
- data/lib/attribute-filters/dsl_attr_virtual.rb +2 -1
- data/lib/attribute-filters/dsl_filters.rb +14 -61
- data/lib/attribute-filters/dsl_sets.rb +238 -88
- data/lib/attribute-filters/helpers.rb +7 -1
- data/lib/attribute-filters/meta_set.rb +38 -0
- data/lib/attribute-filters/version.rb +1 -1
- data/spec/attribute-filters_spec.rb +178 -16
- data/spec/spec_helper.rb +9 -4
- metadata +129 -69
- metadata.gz.sig +0 -0
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
GEM
|
|
2
|
-
remote:
|
|
2
|
+
remote: https://rubygems.org/
|
|
3
3
|
specs:
|
|
4
4
|
abstract (1.0.0)
|
|
5
5
|
actionpack (3.0.17)
|
|
@@ -29,7 +29,7 @@ GEM
|
|
|
29
29
|
abstract (>= 1.0.0)
|
|
30
30
|
hoe (2.16.1)
|
|
31
31
|
rake (~> 0.8)
|
|
32
|
-
hoe-bundler (1.
|
|
32
|
+
hoe-bundler (1.2.0)
|
|
33
33
|
hoe (>= 2.2.0)
|
|
34
34
|
hoe-gemspec (1.0.0)
|
|
35
35
|
hoe (>= 2.2.0)
|
|
@@ -51,20 +51,20 @@ GEM
|
|
|
51
51
|
rake (0.9.2.2)
|
|
52
52
|
rdoc (3.12)
|
|
53
53
|
json (~> 1.4)
|
|
54
|
-
redcarpet (2.
|
|
54
|
+
redcarpet (2.2.2)
|
|
55
55
|
rspec (2.11.0)
|
|
56
56
|
rspec-core (~> 2.11.0)
|
|
57
57
|
rspec-expectations (~> 2.11.0)
|
|
58
58
|
rspec-mocks (~> 2.11.0)
|
|
59
59
|
rspec-core (2.11.1)
|
|
60
|
-
rspec-expectations (2.11.
|
|
60
|
+
rspec-expectations (2.11.3)
|
|
61
61
|
diff-lcs (~> 1.1.3)
|
|
62
|
-
rspec-mocks (2.11.
|
|
62
|
+
rspec-mocks (2.11.3)
|
|
63
63
|
supermodel (0.1.6)
|
|
64
64
|
activemodel (~> 3.0.0)
|
|
65
65
|
thor (0.14.6)
|
|
66
|
-
tzinfo (0.3.
|
|
67
|
-
yard (0.8.
|
|
66
|
+
tzinfo (0.3.34)
|
|
67
|
+
yard (0.8.3)
|
|
68
68
|
|
|
69
69
|
PLATFORMS
|
|
70
70
|
ruby
|
data/Manifest.txt
CHANGED
|
@@ -8,6 +8,7 @@ Manifest.txt
|
|
|
8
8
|
README.md
|
|
9
9
|
Rakefile
|
|
10
10
|
attribute-filters.gemspec
|
|
11
|
+
docs/COMMON-FILTERS.md
|
|
11
12
|
docs/COPYING
|
|
12
13
|
docs/HISTORY
|
|
13
14
|
docs/LEGAL
|
|
@@ -26,8 +27,13 @@ lib/attribute-filters/attribute_set_enum.rb
|
|
|
26
27
|
lib/attribute-filters/attribute_set_query.rb
|
|
27
28
|
lib/attribute-filters/backports.rb
|
|
28
29
|
lib/attribute-filters/common_filters.rb
|
|
30
|
+
lib/attribute-filters/common_filters/bare.rb
|
|
29
31
|
lib/attribute-filters/common_filters/case.rb
|
|
32
|
+
lib/attribute-filters/common_filters/convert.rb
|
|
30
33
|
lib/attribute-filters/common_filters/join.rb
|
|
34
|
+
lib/attribute-filters/common_filters/order.rb
|
|
35
|
+
lib/attribute-filters/common_filters/pick.rb
|
|
36
|
+
lib/attribute-filters/common_filters/presence.rb
|
|
31
37
|
lib/attribute-filters/common_filters/split.rb
|
|
32
38
|
lib/attribute-filters/common_filters/squeeze.rb
|
|
33
39
|
lib/attribute-filters/common_filters/strip.rb
|
|
@@ -35,6 +41,7 @@ lib/attribute-filters/dsl_attr_virtual.rb
|
|
|
35
41
|
lib/attribute-filters/dsl_filters.rb
|
|
36
42
|
lib/attribute-filters/dsl_sets.rb
|
|
37
43
|
lib/attribute-filters/helpers.rb
|
|
44
|
+
lib/attribute-filters/meta_set.rb
|
|
38
45
|
lib/attribute-filters/railtie.rb
|
|
39
46
|
lib/attribute-filters/version.rb
|
|
40
47
|
spec/attribute-filters_spec.rb
|
data/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Attribute Filters for Rails
|
|
2
2
|
===========================
|
|
3
3
|
|
|
4
|
-
**attribute-filters version `
|
|
4
|
+
**attribute-filters version `2.0`** (`Beard`)
|
|
5
5
|
|
|
6
6
|
* https://rubygems.org/gems/attribute-filters
|
|
7
7
|
* https://github.com/siefca/attribute-filters/tree
|
|
@@ -73,15 +73,15 @@ The filtering code is not reusable since it operates on specific attributes.
|
|
|
73
73
|
class User < ActiveRecord::Base
|
|
74
74
|
include ActiveModel::AttributeFilters::Common
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
strips_attributes :username, :email, :real_name
|
|
77
|
+
downcases_attributes :username, :email
|
|
78
|
+
titleizes_attribute :real_name
|
|
79
79
|
|
|
80
80
|
before_validation :filter_attributes
|
|
81
81
|
end
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
-
or if you want
|
|
84
|
+
or if you want more control:
|
|
85
85
|
|
|
86
86
|
```ruby
|
|
87
87
|
class User < ActiveRecord::Base
|
|
@@ -89,9 +89,9 @@ class User < ActiveRecord::Base
|
|
|
89
89
|
include ActiveModel::AttributeFilters::Common::Downcase
|
|
90
90
|
include ActiveModel::AttributeFilters::Common::Titleize
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
has_attributes_that should_be_stripped: [ :username, :email, :real_name ]
|
|
93
|
+
has_attributes_that should_be_downcased: [ :username, :email ]
|
|
94
|
+
has_attributes_that should_be_titleized: [ :real_name ]
|
|
95
95
|
|
|
96
96
|
before_validation :strip_attributes
|
|
97
97
|
before_validation :downcase_attributes
|
|
@@ -103,13 +103,15 @@ or if you would like to create filtering methods on your own:
|
|
|
103
103
|
|
|
104
104
|
```ruby
|
|
105
105
|
class User < ActiveRecord::Base
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
has_attributes_that should_be_stripped: [ :username, :email, :real_name ]
|
|
107
|
+
has_attributes_that should_be_downcased: [ :username, :email ]
|
|
108
|
+
has_attributes_that should_be_capitalized: [ :real_name ]
|
|
109
109
|
|
|
110
|
-
before_validation :
|
|
111
|
-
|
|
112
|
-
|
|
110
|
+
before_validation :filter_attributes
|
|
111
|
+
|
|
112
|
+
has_filtering_method :downcase_names, :should_be_downcased
|
|
113
|
+
has_filtering_method :capitalize, :should_be_capitalized
|
|
114
|
+
has_filtering_method :strip_names, :should_be_stripped
|
|
113
115
|
|
|
114
116
|
def downcase_names
|
|
115
117
|
filter_attributes_that :should_be_downcased do |atr|
|
|
@@ -144,9 +146,9 @@ registering them then the alternative syntax may be helpful:
|
|
|
144
146
|
|
|
145
147
|
```ruby
|
|
146
148
|
class User < ActiveRecord::Base
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
149
|
+
its_attribute email: [ :should_be_stripped, :should_be_downcased ]
|
|
150
|
+
its_attribute username: [ :should_be_stripped, :should_be_downcased ]
|
|
151
|
+
its_attribute real_name: [ :should_be_stripped, :should_be_capitalized ]
|
|
150
152
|
end
|
|
151
153
|
```
|
|
152
154
|
|
|
@@ -158,31 +160,47 @@ use it to express some logic
|
|
|
158
160
|
[on your own](http://rubydoc.info/gems/attribute-filters/file/docs/USAGE.md#Custom_applications).
|
|
159
161
|
|
|
160
162
|
* **See [USAGE](http://rubydoc.info/gems/attribute-filters/file/docs/USAGE.md) for examples and detailed information about the usage.**
|
|
163
|
+
* **See [COMMON-FILTERS](http://rubydoc.info/gems/attribute-filters/file/docs/COMMON-FILTERS.md) for examples and detailed description of common filters.**
|
|
161
164
|
* See [whole documentation](http://rubydoc.info/gems/attribute-filters/) to browse all documents.
|
|
162
165
|
|
|
163
166
|
### Sneak peeks ###
|
|
164
167
|
|
|
165
168
|
```ruby
|
|
166
|
-
@user.
|
|
167
|
-
# =>
|
|
168
|
-
|
|
169
|
-
@user.attributes_that(:should_be_stripped).list.present?
|
|
170
|
-
# => #<ActiveModel::AttributeSet: {"username", "email"}>
|
|
171
|
-
|
|
169
|
+
@user.is_the_attribute.username.required_to_use_application?
|
|
170
|
+
# => true
|
|
171
|
+
|
|
172
172
|
@user.the_attribute(:username).should_be_stripped?
|
|
173
173
|
# => true
|
|
174
174
|
|
|
175
|
-
@user.
|
|
176
|
-
# =>
|
|
175
|
+
@user.is_the_attribute(:username).accessible?
|
|
176
|
+
# => true
|
|
177
177
|
|
|
178
|
-
@user.
|
|
178
|
+
@user.is_the_attribute.username.protected?
|
|
179
|
+
# => false
|
|
180
|
+
|
|
181
|
+
@user.has_the_attribute.username.changed?
|
|
182
|
+
# => false
|
|
183
|
+
|
|
184
|
+
@user.is_the_attribute.username.valid?
|
|
179
185
|
# => true
|
|
180
186
|
|
|
181
|
-
@user.
|
|
187
|
+
@user.are_attributes_that.should_be_stripped.all.present?
|
|
182
188
|
# => false
|
|
183
189
|
|
|
190
|
+
@user.the_attribute(:username).list.sets
|
|
191
|
+
# => #<ActiveModel::AttributeSet: {:should_be_downcased, :should_be_stripped}>
|
|
192
|
+
|
|
193
|
+
@user.attributes_that.should_be_stripped.list.present?
|
|
194
|
+
# => #<ActiveModel::AttributeSet: {"username", "email"}>
|
|
195
|
+
|
|
184
196
|
@user.all_attributes.list.valid?
|
|
185
197
|
# => #<ActiveModel::AttributeSet: {"username", "email"}>
|
|
198
|
+
|
|
199
|
+
@user.all_attributes.list.changed?
|
|
200
|
+
# => #<ActiveModel::AttributeSet: {"username"}>
|
|
201
|
+
|
|
202
|
+
@user.all_attributes.any.changed?
|
|
203
|
+
# => true
|
|
186
204
|
```
|
|
187
205
|
|
|
188
206
|
How it works?
|
|
@@ -251,16 +269,27 @@ Common rake tasks
|
|
|
251
269
|
* `bundle exec rake docs` – render the documentation (output in the subdirectory directory `doc`)
|
|
252
270
|
* `bundle exec rake gem:spec` – builds static gemspec file (`attribute-filters.gemspec`)
|
|
253
271
|
* `bundle exec rake gem` – builds package (output in the subdirectory `pkg`)
|
|
254
|
-
* `bundle exec rake
|
|
272
|
+
* `bundle exec rake spec` – performs spec. tests
|
|
255
273
|
* `bundle exec rake Manifest.txt` – regenerates the `Manifest.txt` file
|
|
256
274
|
* `bundle exec rake ChangeLog` – regenerates the `ChangeLog` file
|
|
257
275
|
|
|
258
276
|
Credits
|
|
259
277
|
-------
|
|
260
278
|
|
|
261
|
-
* [iConsulting](http://www.iconsulting.pl/) supports Free Software and has contributed to this library by paying for
|
|
279
|
+
* [iConsulting](http://www.iconsulting.pl/) supports Free Software and has contributed to this library by paying for my food during the coding.
|
|
262
280
|
* [MrZYX (Jonne Haß)](https://github.com/MrZYX) contributed by giving me some hints and answering basic questions on IRC – THX!
|
|
263
|
-
* [Robert Pankowecki](https://github.com/paneq/) contributed by suggesting selective inclusion of filtering helpers
|
|
281
|
+
* [Robert Pankowecki](https://github.com/paneq/) contributed by suggesting selective inclusion of filtering helpers.
|
|
282
|
+
|
|
283
|
+
Like my work?
|
|
284
|
+
-------------
|
|
285
|
+
|
|
286
|
+
You can send me some bitcoins if you would like to support me:
|
|
287
|
+
|
|
288
|
+
* `13wZbBjs6yQQuAb3zjfHubQSyer2cLAYzH`
|
|
289
|
+
|
|
290
|
+
Or you can endorse my skills on LinkedIn:
|
|
291
|
+
|
|
292
|
+
* [pl.linkedin.com/in/pwilk](http://www.linkedin.com/profile/view?id=4251568#profile-skills)
|
|
264
293
|
|
|
265
294
|
License
|
|
266
295
|
-------
|
data/Rakefile
CHANGED
data/attribute-filters.gemspec
CHANGED
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
Gem::Specification.new do |s|
|
|
4
4
|
s.name = "attribute-filters"
|
|
5
|
-
s.version = "
|
|
5
|
+
s.version = "2.0.0.20121101164113"
|
|
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-11-01"
|
|
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"]
|
|
14
|
-
s.files = [".rspec", ".yardopts", "ChangeLog", "Gemfile", "Gemfile.lock", "LGPL-LICENSE", "Manifest.txt", "README.md", "Rakefile", "attribute-filters.gemspec", "docs/COPYING", "docs/HISTORY", "docs/LEGAL", "docs/LGPL-LICENSE", "docs/TODO", "docs/USAGE.md", "docs/rdoc.css", "docs/yard-tpl/default/fulldoc/html/css/common.css", "init.rb", "lib/attribute-filters.rb", "lib/attribute-filters/active_model_insert.rb", "lib/attribute-filters/attribute_set.rb", "lib/attribute-filters/attribute_set_annotations.rb", "lib/attribute-filters/attribute_set_attrquery.rb", "lib/attribute-filters/attribute_set_enum.rb", "lib/attribute-filters/attribute_set_query.rb", "lib/attribute-filters/backports.rb", "lib/attribute-filters/common_filters.rb", "lib/attribute-filters/common_filters/case.rb", "lib/attribute-filters/common_filters/join.rb", "lib/attribute-filters/common_filters/split.rb", "lib/attribute-filters/common_filters/squeeze.rb", "lib/attribute-filters/common_filters/strip.rb", "lib/attribute-filters/dsl_attr_virtual.rb", "lib/attribute-filters/dsl_filters.rb", "lib/attribute-filters/dsl_sets.rb", "lib/attribute-filters/helpers.rb", "lib/attribute-filters/railtie.rb", "lib/attribute-filters/version.rb", "spec/attribute-filters_spec.rb", "spec/spec_helper.rb", ".gemtest"]
|
|
14
|
+
s.files = [".rspec", ".yardopts", "ChangeLog", "Gemfile", "Gemfile.lock", "LGPL-LICENSE", "Manifest.txt", "README.md", "Rakefile", "attribute-filters.gemspec", "docs/COMMON-FILTERS.md", "docs/COPYING", "docs/HISTORY", "docs/LEGAL", "docs/LGPL-LICENSE", "docs/TODO", "docs/USAGE.md", "docs/rdoc.css", "docs/yard-tpl/default/fulldoc/html/css/common.css", "init.rb", "lib/attribute-filters.rb", "lib/attribute-filters/active_model_insert.rb", "lib/attribute-filters/attribute_set.rb", "lib/attribute-filters/attribute_set_annotations.rb", "lib/attribute-filters/attribute_set_attrquery.rb", "lib/attribute-filters/attribute_set_enum.rb", "lib/attribute-filters/attribute_set_query.rb", "lib/attribute-filters/backports.rb", "lib/attribute-filters/common_filters.rb", "lib/attribute-filters/common_filters/bare.rb", "lib/attribute-filters/common_filters/case.rb", "lib/attribute-filters/common_filters/convert.rb", "lib/attribute-filters/common_filters/join.rb", "lib/attribute-filters/common_filters/order.rb", "lib/attribute-filters/common_filters/pick.rb", "lib/attribute-filters/common_filters/presence.rb", "lib/attribute-filters/common_filters/split.rb", "lib/attribute-filters/common_filters/squeeze.rb", "lib/attribute-filters/common_filters/strip.rb", "lib/attribute-filters/dsl_attr_virtual.rb", "lib/attribute-filters/dsl_filters.rb", "lib/attribute-filters/dsl_sets.rb", "lib/attribute-filters/helpers.rb", "lib/attribute-filters/meta_set.rb", "lib/attribute-filters/railtie.rb", "lib/attribute-filters/version.rb", "spec/attribute-filters_spec.rb", "spec/spec_helper.rb", ".gemtest"]
|
|
15
15
|
s.homepage = "https://rubygems.org/gems/attribute-filters/"
|
|
16
16
|
s.rdoc_options = ["--title", "Attribute::Filters Documentation", "--quiet"]
|
|
17
17
|
s.require_paths = ["lib"]
|
|
18
18
|
s.rubyforge_project = "attribute-filters"
|
|
19
|
-
s.rubygems_version = "1.8.
|
|
19
|
+
s.rubygems_version = "1.8.24"
|
|
20
20
|
s.signing_key = "/Users/siefca/.gem/gem-private_key.pem"
|
|
21
21
|
s.summary = "Concise way of filtering model attributes in Rails"
|
|
22
22
|
|
|
@@ -0,0 +1,1067 @@
|
|
|
1
|
+
Common filters
|
|
2
|
+
--------------
|
|
3
|
+
|
|
4
|
+
## List of filters ##
|
|
5
|
+
|
|
6
|
+
See the
|
|
7
|
+
[`ActiveModel::AttributeFilters::Common`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common)
|
|
8
|
+
for descriptions of common filtering modules.
|
|
9
|
+
|
|
10
|
+
Terms used when describing filters:
|
|
11
|
+
|
|
12
|
+
> Callback method – a method that does the filtering, may be called manually, registered as a callback (e.g. with `after_` or `before_` methods from Active Record) or called automatically when `filter_attributes` is called.
|
|
13
|
+
|
|
14
|
+
> Class-level helper – a DSL method that may be used in a model class to setup a filter.
|
|
15
|
+
|
|
16
|
+
> Class-level helper aliases – aliases of class-level DSL methods used to setup a filter.
|
|
17
|
+
|
|
18
|
+
> Uses set – a global attribute set (assigned to model) that is used to store information on attributes that should be filtered and optional parameters (stored as annotations) that are sometimes used by filtering method.
|
|
19
|
+
|
|
20
|
+
> Operates on – data types that the filtering method operates on.
|
|
21
|
+
|
|
22
|
+
> Uses annotations – a statement concerning the use of annotations (as parameters) by the filtering method and a list of annotations and their meanings.
|
|
23
|
+
|
|
24
|
+
> Parameters' aliases – aliases of parameters that can be used when utilizing class-level helper to setup the given filter.
|
|
25
|
+
|
|
26
|
+
> Default annotation – a parameter which value is taken from a value assigned to the whole attribute when setting up a filter.
|
|
27
|
+
|
|
28
|
+
### Case ###
|
|
29
|
+
|
|
30
|
+
* Submodule: [`Case`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Case.html)
|
|
31
|
+
|
|
32
|
+
#### `capitalize_attributes` ####
|
|
33
|
+
|
|
34
|
+
Capitalizes attributes.
|
|
35
|
+
|
|
36
|
+
* Callback method: `capitalize_attributes`
|
|
37
|
+
* Class-level helper: `capitalize_attributes(*attribute_names)`
|
|
38
|
+
* Class-level helper aliases: `capitalize_attribute`, `capitalizes_attribute`, `capitalizes_attributes`
|
|
39
|
+
* Uses set: `:should_be_capitalized`
|
|
40
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
41
|
+
* Uses annotations: no
|
|
42
|
+
|
|
43
|
+
Example:
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
class User < ActiveRecord::Base
|
|
47
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
48
|
+
|
|
49
|
+
capitalizes_attribute :name
|
|
50
|
+
before_validation :capitalize_attributes
|
|
51
|
+
end
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
or
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
class User < ActiveRecord::Base
|
|
58
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
59
|
+
|
|
60
|
+
has_attributes_that :should_be_capitalized => [ :name ]
|
|
61
|
+
before_validation :capitalize_attributes
|
|
62
|
+
end
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Then:
|
|
66
|
+
|
|
67
|
+
> `"some name"`
|
|
68
|
+
|
|
69
|
+
will become:
|
|
70
|
+
|
|
71
|
+
> `"Some name"`
|
|
72
|
+
|
|
73
|
+
#### `fully_capitalize_attributes` ####
|
|
74
|
+
|
|
75
|
+
Capitalizes attributes and squeezes spaces that separate strings.
|
|
76
|
+
|
|
77
|
+
* Callback method: `fully_capitalize_attributes`
|
|
78
|
+
* Class-level helper: `fully_capitalize_attributes(*attribute_names)`
|
|
79
|
+
* Class-level helper aliases: `fully_capitalize_attribute`, `fully_capitalizes_attribute`, `fully_capitalizes_attributes`
|
|
80
|
+
* Uses set: `:should_be_fully_capitalized`
|
|
81
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
82
|
+
* Uses annotations: no
|
|
83
|
+
|
|
84
|
+
Example:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
class User < ActiveRecord::Base
|
|
88
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
89
|
+
|
|
90
|
+
fully_capitalizes_attribute :name
|
|
91
|
+
before_validation :fully_capitalize_attributes
|
|
92
|
+
end
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
or
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
class User < ActiveRecord::Base
|
|
99
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
100
|
+
|
|
101
|
+
has_attributes_that :should_be_fully_capitalized => [ :name ]
|
|
102
|
+
before_validation :fully_capitalize_attributes
|
|
103
|
+
end
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Then:
|
|
107
|
+
|
|
108
|
+
> `"some name"`
|
|
109
|
+
|
|
110
|
+
will become:
|
|
111
|
+
|
|
112
|
+
> `"Some Name"`
|
|
113
|
+
|
|
114
|
+
#### `titleize_attributes` ####
|
|
115
|
+
|
|
116
|
+
Titleizes attributes.
|
|
117
|
+
|
|
118
|
+
* Callback method: `titleize_attributes`
|
|
119
|
+
* Class-level helper: `titleize_attributes(*attribute_names)`
|
|
120
|
+
* Class-level helper aliases: `titleize_attribute`, `titleizes_attribute`, `titleizes_attributes`
|
|
121
|
+
* Uses set: `:should_be_titleized`
|
|
122
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
123
|
+
* Uses annotations: no
|
|
124
|
+
|
|
125
|
+
Example:
|
|
126
|
+
|
|
127
|
+
```ruby
|
|
128
|
+
class User < ActiveRecord::Base
|
|
129
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
130
|
+
|
|
131
|
+
titleizes_attribute :name
|
|
132
|
+
before_validation :titleize_attributes
|
|
133
|
+
end
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
or
|
|
137
|
+
|
|
138
|
+
```ruby
|
|
139
|
+
class User < ActiveRecord::Base
|
|
140
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
141
|
+
|
|
142
|
+
has_attributes_that :should_be_titleized => [ :name ]
|
|
143
|
+
before_validation :titleize_attributes
|
|
144
|
+
end
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Then:
|
|
148
|
+
|
|
149
|
+
> `"some name"`
|
|
150
|
+
|
|
151
|
+
will become:
|
|
152
|
+
|
|
153
|
+
> `"Some Name"`
|
|
154
|
+
|
|
155
|
+
#### `upcase_attributes` ####
|
|
156
|
+
|
|
157
|
+
Upcases attributes.
|
|
158
|
+
|
|
159
|
+
* Callback method: `upcase_attributes`
|
|
160
|
+
* Class-level helper: `upcase_attributes(*attribute_names)`
|
|
161
|
+
* Class-level helper aliases: `upcase_attribute`, `upcases_attribute`, `upcases_attributes`
|
|
162
|
+
* Uses set: `:should_be_upcased`
|
|
163
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
164
|
+
* Uses annotations: no
|
|
165
|
+
|
|
166
|
+
Example:
|
|
167
|
+
|
|
168
|
+
```ruby
|
|
169
|
+
class User < ActiveRecord::Base
|
|
170
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
171
|
+
|
|
172
|
+
upcases_attribute :name
|
|
173
|
+
before_validation :upcase_attributes
|
|
174
|
+
end
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
or
|
|
178
|
+
|
|
179
|
+
```ruby
|
|
180
|
+
class User < ActiveRecord::Base
|
|
181
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
182
|
+
|
|
183
|
+
has_attributes_that :should_be_upcased => [ :name ]
|
|
184
|
+
before_validation :upcase_attributes
|
|
185
|
+
end
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Then:
|
|
189
|
+
|
|
190
|
+
> `"some name"`
|
|
191
|
+
|
|
192
|
+
will become:
|
|
193
|
+
|
|
194
|
+
> `"SOME NAME"`
|
|
195
|
+
|
|
196
|
+
#### `downcase_attributes` ####
|
|
197
|
+
|
|
198
|
+
Downcases attributes.
|
|
199
|
+
|
|
200
|
+
* Callback method: `downcase_attributes`
|
|
201
|
+
* Class-level helper: `downcase_attributes(*attribute_names)`
|
|
202
|
+
* Class-level helper aliases: `downcase_attribute`, `downcases_attribute`, `downcases_attributes`
|
|
203
|
+
* Uses set: `:should_be_downcased`
|
|
204
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
205
|
+
* Uses annotations: no
|
|
206
|
+
|
|
207
|
+
Example:
|
|
208
|
+
|
|
209
|
+
```ruby
|
|
210
|
+
class User < ActiveRecord::Base
|
|
211
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
212
|
+
|
|
213
|
+
downcases_attribute :name
|
|
214
|
+
before_validation :downcase_attributes
|
|
215
|
+
end
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
or
|
|
219
|
+
|
|
220
|
+
```ruby
|
|
221
|
+
class User < ActiveRecord::Base
|
|
222
|
+
include ActiveModel::AttributeFilters::Common::Case
|
|
223
|
+
|
|
224
|
+
has_attributes_that :should_be_downcased => [ :name ]
|
|
225
|
+
before_validation :downcase_attributes
|
|
226
|
+
end
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Then:
|
|
230
|
+
|
|
231
|
+
> `"SOME NAME"`
|
|
232
|
+
|
|
233
|
+
will become:
|
|
234
|
+
|
|
235
|
+
> `"some name"`
|
|
236
|
+
|
|
237
|
+
### Convert ###
|
|
238
|
+
|
|
239
|
+
* Submodule: [`Convert`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Convert.html)
|
|
240
|
+
|
|
241
|
+
#### `attributes_to_s` ####
|
|
242
|
+
|
|
243
|
+
Converts attributes to strings.
|
|
244
|
+
|
|
245
|
+
* Callback method: `attributes_to_s`
|
|
246
|
+
* Class-level helper: `attributes_to_s(*attribute_names)`
|
|
247
|
+
* Class-level helper aliases: `convert_to_strings`, `convert_to_string`, `converts_to_strings`, `converts_to_string`
|
|
248
|
+
* Uses set: `:should_be_strings`
|
|
249
|
+
* Operates on: numbers, arrays of numbers, hashes of numbers (as values), other convertable types
|
|
250
|
+
* Uses annotations: yes
|
|
251
|
+
* `:to_s_default` – a value used in case of a conversion error
|
|
252
|
+
* `:to_s_base` – a base used when converting numbers (2 for binary, 10 for decimal and so on) (default parameter)
|
|
253
|
+
* Parameters' aliases:
|
|
254
|
+
* `:to_s_default` – `:default`, `:on_error`
|
|
255
|
+
* `:to_s_base` – `:base`, `:with_base`
|
|
256
|
+
|
|
257
|
+
Example:
|
|
258
|
+
|
|
259
|
+
```ruby
|
|
260
|
+
class User < ActiveRecord::Base
|
|
261
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
262
|
+
|
|
263
|
+
converts_to_string :name
|
|
264
|
+
before_validation :attributes_to_strings
|
|
265
|
+
end
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
or
|
|
269
|
+
|
|
270
|
+
```ruby
|
|
271
|
+
class User < ActiveRecord::Base
|
|
272
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
273
|
+
|
|
274
|
+
has_attributes_that :should_be_strings => [ :name ]
|
|
275
|
+
before_validation :attributes_to_strings
|
|
276
|
+
end
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
#### `attributes_to_i` ####
|
|
280
|
+
|
|
281
|
+
Converts attributes to integers.
|
|
282
|
+
|
|
283
|
+
* Callback method: `attributes_to_i`
|
|
284
|
+
* Class-level helper: `attributes_to_i(*attribute_names)`
|
|
285
|
+
* Class-level helper aliases: `convert_to_integers`, `convert_to_integer`, `converts_to_integers`, `converts_to_integer`
|
|
286
|
+
* Uses set: `:should_be_integers`
|
|
287
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values), other convertable types
|
|
288
|
+
* Uses annotations: yes
|
|
289
|
+
* `:to_i_default` – a value used in case of a conversion error
|
|
290
|
+
* `:to_i_base` – a base used when converting numbers (2 for binary, 10 for decimal and so on) (default parameter)
|
|
291
|
+
* Parameters' aliases:
|
|
292
|
+
* `:to_i_default` – `:default`, `:on_error`
|
|
293
|
+
* `:to_i_base` – `:base`, `:with_base`
|
|
294
|
+
|
|
295
|
+
Example:
|
|
296
|
+
|
|
297
|
+
```ruby
|
|
298
|
+
class User < ActiveRecord::Base
|
|
299
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
300
|
+
|
|
301
|
+
converts_to_integer :number
|
|
302
|
+
before_validation :attributes_to_integers
|
|
303
|
+
end
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
or
|
|
307
|
+
|
|
308
|
+
```ruby
|
|
309
|
+
class User < ActiveRecord::Base
|
|
310
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
311
|
+
|
|
312
|
+
has_attributes_that :should_be_integers => [ :number ]
|
|
313
|
+
before_validation :attributes_to_integers
|
|
314
|
+
end
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
#### `attributes_to_f` ####
|
|
318
|
+
|
|
319
|
+
Converts attributes to floats.
|
|
320
|
+
|
|
321
|
+
* Callback method: `attributes_to_f`
|
|
322
|
+
* Class-level helper: `attributes_to_f(*attribute_names)`
|
|
323
|
+
* Class-level helper aliases: `convert_to_floats`, `convert_to_float`, `converts_to_floats`, `converts_to_float`
|
|
324
|
+
* Uses set: `:should_be_floats`
|
|
325
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values), other convertable types
|
|
326
|
+
* Uses annotations: yes
|
|
327
|
+
* `:to_f_default` – a value used in case of a conversion error (default parameter)
|
|
328
|
+
* Parameters' aliases:
|
|
329
|
+
* `:to_f_default` – `:default`, `:on_error`
|
|
330
|
+
|
|
331
|
+
Example:
|
|
332
|
+
|
|
333
|
+
```ruby
|
|
334
|
+
class User < ActiveRecord::Base
|
|
335
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
336
|
+
|
|
337
|
+
converts_to_float :number
|
|
338
|
+
before_validation :attributes_to_floats
|
|
339
|
+
end
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
or
|
|
343
|
+
|
|
344
|
+
```ruby
|
|
345
|
+
class User < ActiveRecord::Base
|
|
346
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
347
|
+
|
|
348
|
+
has_attributes_that :should_be_floats => [ :number ]
|
|
349
|
+
before_validation :attributes_to_floats
|
|
350
|
+
end
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
#### `attributes_to_numbers` ####
|
|
354
|
+
|
|
355
|
+
Converts attributes to numbers.
|
|
356
|
+
|
|
357
|
+
Works the same way as `attributes_to_f` but uses different attribute set.
|
|
358
|
+
|
|
359
|
+
* Callback method: `attributes_to_numbers`
|
|
360
|
+
* Class-level helper: `attributes_to_numbers(*attribute_names)`
|
|
361
|
+
* Class-level helper aliases: `convert_to_numbers`, `convert_to_number`, `converts_to_numbers`, `converts_to_number`
|
|
362
|
+
* Uses set: `:should_be_numbers`
|
|
363
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values), other convertable types
|
|
364
|
+
* Uses annotations: yes
|
|
365
|
+
* `:to_num_default` – a value used in case of a conversion error (default parameter)
|
|
366
|
+
* Parameters' aliases:
|
|
367
|
+
* `:to_num_default` – `:default`, `:on_error`
|
|
368
|
+
|
|
369
|
+
Example:
|
|
370
|
+
|
|
371
|
+
```ruby
|
|
372
|
+
class User < ActiveRecord::Base
|
|
373
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
374
|
+
|
|
375
|
+
converts_to_number :number
|
|
376
|
+
before_validation :attributes_to_numbers
|
|
377
|
+
end
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
or
|
|
381
|
+
|
|
382
|
+
```ruby
|
|
383
|
+
class User < ActiveRecord::Base
|
|
384
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
385
|
+
|
|
386
|
+
has_attributes_that :should_be_numbers => [ :number ]
|
|
387
|
+
before_validation :attributes_to_numbers
|
|
388
|
+
end
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
#### `attributes_to_r` ####
|
|
392
|
+
|
|
393
|
+
Converts attributes to rationals.
|
|
394
|
+
|
|
395
|
+
* Callback method: `attributes_to_r`
|
|
396
|
+
* Class-level helper: `attributes_to_r(*attribute_names)`
|
|
397
|
+
* Class-level helper aliases: `convert_to_rationals`, `convert_to_rational`, `converts_to_rationals`, `converts_to_rational`, `convert_to_fractions`, `convert_to_fraction`, `converts_to_fractions`, `converts_to_fraction`
|
|
398
|
+
* Uses set: `:should_be_rationals`
|
|
399
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values), other convertable types
|
|
400
|
+
* Uses annotations: yes
|
|
401
|
+
* `:to_r_default` – a value used in case of a conversion error (default parameter)
|
|
402
|
+
* Parameters' aliases:
|
|
403
|
+
* `:to_r_default` – `:default`, `:on_error`
|
|
404
|
+
|
|
405
|
+
Example:
|
|
406
|
+
|
|
407
|
+
```ruby
|
|
408
|
+
class User < ActiveRecord::Base
|
|
409
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
410
|
+
|
|
411
|
+
converts_to_rational :number
|
|
412
|
+
before_validation :attributes_to_rationals
|
|
413
|
+
end
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
or
|
|
417
|
+
|
|
418
|
+
```ruby
|
|
419
|
+
class User < ActiveRecord::Base
|
|
420
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
421
|
+
|
|
422
|
+
has_attributes_that :should_be_rationals => [ :number ]
|
|
423
|
+
before_validation :attributes_to_rationals
|
|
424
|
+
end
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
#### `attributes_to_b` ####
|
|
428
|
+
|
|
429
|
+
Converts attributes to boolean.
|
|
430
|
+
|
|
431
|
+
* Callback method: `attributes_to_b`
|
|
432
|
+
* Class-level helper: `attributes_to_b(*attribute_names)`
|
|
433
|
+
* Class-level helper aliases: `convert_to_booleans`, `convert_to_boolean`, `converts_to_booleans`, `converts_to_boolean`
|
|
434
|
+
* Uses set: `:should_be_boolean`
|
|
435
|
+
* Operates on: objects, arrays of objects, hashes of objects (as values)
|
|
436
|
+
* Uses annotations: yes
|
|
437
|
+
* `:to_b_default` – a value used in case of a conversion error (default parameter)
|
|
438
|
+
* Parameters' aliases:
|
|
439
|
+
* `:to_b_default` – `:default`, `:on_error`
|
|
440
|
+
|
|
441
|
+
Example:
|
|
442
|
+
|
|
443
|
+
```ruby
|
|
444
|
+
class User < ActiveRecord::Base
|
|
445
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
446
|
+
|
|
447
|
+
converts_to_boolean :number
|
|
448
|
+
before_validation :attributes_to_boolean
|
|
449
|
+
end
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
or
|
|
453
|
+
|
|
454
|
+
```ruby
|
|
455
|
+
class User < ActiveRecord::Base
|
|
456
|
+
include ActiveModel::AttributeFilters::Common::Convert
|
|
457
|
+
|
|
458
|
+
has_attributes_that :should_be_boolean => [ :number ]
|
|
459
|
+
before_validation :attributes_to_boolean
|
|
460
|
+
end
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
### Order ###
|
|
464
|
+
|
|
465
|
+
* Submodule: [`Order`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Order.html)
|
|
466
|
+
|
|
467
|
+
#### `reverse_attributes` ####
|
|
468
|
+
|
|
469
|
+
Reverses order of attribute values.
|
|
470
|
+
|
|
471
|
+
* Callback method: `reverse_attributes`
|
|
472
|
+
* Class-level helper: `reverse_attributes(*attribute_names)`
|
|
473
|
+
* Class-level helper aliases: `reverse_attribute`, `reverses_attribute`, `reverses_attributes`
|
|
474
|
+
* Uses set: `:should_be_reversed`
|
|
475
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values), other enumerable types
|
|
476
|
+
* Uses annotations: yes
|
|
477
|
+
* `:reverse_enumerable` – disables recursive traversing of arrays and hashes to filter each element
|
|
478
|
+
* Parameters' aliases:
|
|
479
|
+
* `:reverse_enumerable` – `:enum`, `:enums`, `:whole_enums`, `:reverse_enums`
|
|
480
|
+
|
|
481
|
+
Example:
|
|
482
|
+
|
|
483
|
+
```ruby
|
|
484
|
+
class User < ActiveRecord::Base
|
|
485
|
+
include ActiveModel::AttributeFilters::Common::Order
|
|
486
|
+
|
|
487
|
+
reverses_attribute :name
|
|
488
|
+
before_validation :reverse_attributes
|
|
489
|
+
end
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
or
|
|
493
|
+
|
|
494
|
+
```ruby
|
|
495
|
+
class User < ActiveRecord::Base
|
|
496
|
+
include ActiveModel::AttributeFilters::Common::Order
|
|
497
|
+
|
|
498
|
+
has_attributes_that :should_be_reversed => [ :name ]
|
|
499
|
+
before_validation :reverse_attributes
|
|
500
|
+
end
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
#### `shuffle_attributes` ####
|
|
504
|
+
|
|
505
|
+
Shuffles order of attribute values.
|
|
506
|
+
|
|
507
|
+
* Callback method: `shuffle_attributes`
|
|
508
|
+
* Class-level helper: `shuffle_attributes(*attribute_names)`
|
|
509
|
+
* Class-level helper aliases: `shuffle_attribute`, `shuffles_attribute`, `shuffles_attributes`
|
|
510
|
+
* Uses set: `:should_be_shuffled`
|
|
511
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values), other enumerable types
|
|
512
|
+
* Uses annotations: yes
|
|
513
|
+
* `:shuffle_enumerable` – disables recursive traversing of arrays and hashes to filter each element
|
|
514
|
+
* `:shuffle_generator` – random number generation source passed to the `shuffle` method
|
|
515
|
+
* Parameters' aliases:
|
|
516
|
+
* `:shuffle_enumerable` – `:enum`, `:enums`, `:whole_enums`, `:shuffle_enums`
|
|
517
|
+
* `:shuffle_generator` – `:random_generator`, `:generator`, `:rnd`, `:rng`
|
|
518
|
+
|
|
519
|
+
Example:
|
|
520
|
+
|
|
521
|
+
```ruby
|
|
522
|
+
class User < ActiveRecord::Base
|
|
523
|
+
include ActiveModel::AttributeFilters::Common::Order
|
|
524
|
+
|
|
525
|
+
shuffles_attribute :name
|
|
526
|
+
before_validation :shuffle_attributes
|
|
527
|
+
end
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
or
|
|
531
|
+
|
|
532
|
+
```ruby
|
|
533
|
+
class User < ActiveRecord::Base
|
|
534
|
+
include ActiveModel::AttributeFilters::Common::Order
|
|
535
|
+
|
|
536
|
+
has_attributes_that :should_be_shuffled => [ :name ]
|
|
537
|
+
before_validation :shuffle_attributes
|
|
538
|
+
end
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
### Pick ###
|
|
542
|
+
|
|
543
|
+
* Submodule: [`Pick`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Pick.html)
|
|
544
|
+
|
|
545
|
+
#### `pick_attributes` ####
|
|
546
|
+
|
|
547
|
+
Picks attributes from leading and trailing spaces or other surrounding characters.
|
|
548
|
+
|
|
549
|
+
* Callback method: `pick_attributes`
|
|
550
|
+
* Class-level helper: `pick_attributes(*attribute_names)`
|
|
551
|
+
* Class-level helper aliases: `pick_attribute`, `picks_attribute`, `picks_attributes`
|
|
552
|
+
* Uses set: `:should_be_picked`
|
|
553
|
+
* Operates on: strings, arrays of strings, hashes of string (as values), other enumerable types
|
|
554
|
+
* Uses annotations: yes
|
|
555
|
+
* `:pick_enumerable` – disables recursive traversing of arrays and hashes to filter each element
|
|
556
|
+
* `:pick_step` – step of picking the elements (defaults to 1)
|
|
557
|
+
* `:pick_from` – beginning of picked range (defaults to 0)
|
|
558
|
+
* `:pick_to` – end of picked range (defaults to elements count - 1)
|
|
559
|
+
* `:pick_range` – replaces `:pick_from` and `:pick_to`
|
|
560
|
+
* `:pick_separator` – if a value is a string it splits it into elements using the given character(s) or regexp (defaults to "") (default parameter)
|
|
561
|
+
* `:pick_join` – if a value is a string it joins the results using the given character(s) (defaults to the value of `:pick_separator` or `nil` in case of regexp)
|
|
562
|
+
* Parameters' aliases:
|
|
563
|
+
* `:pick_enumerable` – `:enum`, `:enums`, `:whole_enums`, `:pick_enums`
|
|
564
|
+
* `:pick_step` – `:step`, `:with_step`, `:each`
|
|
565
|
+
* `:pick_from` – `:from`, `:head`, `:take`, `:first`, `:pick_first`, `:pick_head`
|
|
566
|
+
* `:pick_to` – `:to`, `:tail`, `:last`, `:pick_last`, `:pick_tail`
|
|
567
|
+
* `:pick_range` – `:range`
|
|
568
|
+
* `:pick_separator` – `:separator`, `:regex`, `:split_with`, `:split_separator`
|
|
569
|
+
* `:pick_join` – `:joiner`, `:join`, `:join_with`
|
|
570
|
+
|
|
571
|
+
Example:
|
|
572
|
+
|
|
573
|
+
```ruby
|
|
574
|
+
class User < ActiveRecord::Base
|
|
575
|
+
include ActiveModel::AttributeFilters::Common::Pick
|
|
576
|
+
|
|
577
|
+
picks_attribute :name
|
|
578
|
+
before_validation :pick_attributes
|
|
579
|
+
end
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
or
|
|
583
|
+
|
|
584
|
+
```ruby
|
|
585
|
+
class User < ActiveRecord::Base
|
|
586
|
+
include ActiveModel::AttributeFilters::Common::Order
|
|
587
|
+
|
|
588
|
+
has_attributes_that :should_be_picked => [ :name ]
|
|
589
|
+
before_validation :pick_attributes
|
|
590
|
+
end
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
or
|
|
594
|
+
|
|
595
|
+
```ruby
|
|
596
|
+
class User < ActiveRecord::Base
|
|
597
|
+
include ActiveModel::AttributeFilters::Common::Pick
|
|
598
|
+
|
|
599
|
+
picks_attribute :name, :range => 1..-1, :step => 2
|
|
600
|
+
before_validation :pick_attributes
|
|
601
|
+
end
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
or
|
|
605
|
+
|
|
606
|
+
```ruby
|
|
607
|
+
class User < ActiveRecord::Base
|
|
608
|
+
include ActiveModel::AttributeFilters::Common::Pick
|
|
609
|
+
|
|
610
|
+
picks_attribute :name => " " # default parameter sets separator
|
|
611
|
+
before_validation :pick_attributes
|
|
612
|
+
end
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
### Presence ###
|
|
616
|
+
|
|
617
|
+
* Submodule: [`Presence`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Presence.html)
|
|
618
|
+
|
|
619
|
+
#### `fill_attributes` ####
|
|
620
|
+
|
|
621
|
+
Fills blank attributes with the given values.
|
|
622
|
+
|
|
623
|
+
* Callback method: `fill_attributes`
|
|
624
|
+
* Class-level helper: `fill_attributes(*attribute_names)`
|
|
625
|
+
* Class-level helper aliases: `fill_attribute`, `fills_attribute`, `fills_attributes`
|
|
626
|
+
* Uses set: `:should_be_filled`
|
|
627
|
+
* Operates on: objects, arrays of objects, hashes of objects (as values)
|
|
628
|
+
* Uses annotations: yes
|
|
629
|
+
* `:fill_enumerable` – disables recursive traversing of arrays and hashes to filter each element
|
|
630
|
+
* `:fill_value` – replacemant value (default parameter)
|
|
631
|
+
* `:fill_any` – flag that causes to fill any attribute value, not just blank ones
|
|
632
|
+
* Parameters' aliases:
|
|
633
|
+
* `:fill_enumerable` – `:enums`, `:replace_enumerable`, `:replace_enums`, `:whole_enums`, `:fill_enums`
|
|
634
|
+
* `:fill_value` – `:with`, `:fill_with`, `:fill`, `:value`, `:content`, `:default`, `:fill_value`
|
|
635
|
+
* `:fill_any` – `:all`, `:any`, `:fill_always`, `:always_fill`, `:always`, `:fill_present`, `:fill_all`
|
|
636
|
+
|
|
637
|
+
Example:
|
|
638
|
+
|
|
639
|
+
```ruby
|
|
640
|
+
class User < ActiveRecord::Base
|
|
641
|
+
include ActiveModel::AttributeFilters::Common::Presence
|
|
642
|
+
|
|
643
|
+
fills_attribute :name, :with => "none"
|
|
644
|
+
before_validation :fill_attributes
|
|
645
|
+
end
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
or
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
```ruby
|
|
652
|
+
class User < ActiveRecord::Base
|
|
653
|
+
include ActiveModel::AttributeFilters::Common::Order
|
|
654
|
+
|
|
655
|
+
has_attributes_that :should_be_filled => [ :name => { :fill_value => "not given" } ]
|
|
656
|
+
before_validation :fill_attributes
|
|
657
|
+
end
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
or
|
|
661
|
+
|
|
662
|
+
```ruby
|
|
663
|
+
class User < ActiveRecord::Base
|
|
664
|
+
include ActiveModel::AttributeFilters::Common::Presence
|
|
665
|
+
|
|
666
|
+
fills_attribute :name => "not given"
|
|
667
|
+
before_validation :fill_attributes
|
|
668
|
+
end
|
|
669
|
+
```
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
### Strip ###
|
|
673
|
+
|
|
674
|
+
* Submodule: [`Strip`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Strip.html)
|
|
675
|
+
|
|
676
|
+
#### `strip_attributes` ####
|
|
677
|
+
|
|
678
|
+
Strips attributes of leading and trailing spaces.
|
|
679
|
+
|
|
680
|
+
* Callback method: `strip_attributes`
|
|
681
|
+
* Class-level helper: `strip_attributes(*attribute_names)`
|
|
682
|
+
* Class-level helper aliases: `strip_attribute`, `strips_attribute`, `strips_attributes`
|
|
683
|
+
* Uses set: `:should_be_stripped`
|
|
684
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
685
|
+
* Uses annotations: no
|
|
686
|
+
|
|
687
|
+
Example:
|
|
688
|
+
|
|
689
|
+
```ruby
|
|
690
|
+
class User < ActiveRecord::Base
|
|
691
|
+
include ActiveModel::AttributeFilters::Common::Strip
|
|
692
|
+
|
|
693
|
+
strips_attribute :name
|
|
694
|
+
before_validation :strip_attributes
|
|
695
|
+
end
|
|
696
|
+
```
|
|
697
|
+
|
|
698
|
+
or
|
|
699
|
+
|
|
700
|
+
```ruby
|
|
701
|
+
class User < ActiveRecord::Base
|
|
702
|
+
include ActiveModel::AttributeFilters::Common::Strip
|
|
703
|
+
|
|
704
|
+
has_attributes_that :should_be_stripped => [ :name ]
|
|
705
|
+
before_validation :strip_attributes
|
|
706
|
+
end
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
Then:
|
|
710
|
+
|
|
711
|
+
> `" Some Name "`
|
|
712
|
+
|
|
713
|
+
will become:
|
|
714
|
+
|
|
715
|
+
> `"Some Name"`
|
|
716
|
+
|
|
717
|
+
### Squeeze ###
|
|
718
|
+
|
|
719
|
+
* Submodule: [`Squeeze`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Squeeze.html)
|
|
720
|
+
|
|
721
|
+
#### `squeeze_attributes` ####
|
|
722
|
+
|
|
723
|
+
Squeezes attributes (squeezes repeating spaces into one).
|
|
724
|
+
|
|
725
|
+
* Callback method: `squeeze_attributes`
|
|
726
|
+
* Class-level helper: `squeeze_attributes(*attribute_names)`
|
|
727
|
+
* Class-level helper aliases: `squeeze_attribute`, `squeezes_attribute`, `squeezes_attributes`
|
|
728
|
+
* Uses set: `:should_be_squeezed`
|
|
729
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
730
|
+
* Uses annotations: yes
|
|
731
|
+
* `:squeeze_other_str` – argument used when calling [`String#squeeze`](http://www.ruby-doc.org/core/String.html#method-i-squeeze) (optional)
|
|
732
|
+
* Parameters' aliases (used with class-level helper):
|
|
733
|
+
* `:squeeze_other_str` – `:other_str`, `:string`, `:with_string`, `:with_characters`, `:with_character`, `:characters`
|
|
734
|
+
|
|
735
|
+
Example:
|
|
736
|
+
|
|
737
|
+
```ruby
|
|
738
|
+
class User < ActiveRecord::Base
|
|
739
|
+
include ActiveModel::AttributeFilters::Common::Squeeze
|
|
740
|
+
|
|
741
|
+
squeezes_attribute :name
|
|
742
|
+
before_validation :squeeze_attributes
|
|
743
|
+
end
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
or
|
|
747
|
+
|
|
748
|
+
```ruby
|
|
749
|
+
class User < ActiveRecord::Base
|
|
750
|
+
include ActiveModel::AttributeFilters::Common::Squeeze
|
|
751
|
+
|
|
752
|
+
has_attributes_that :should_be_squeezed => [ :name ]
|
|
753
|
+
before_validation :squeeze_attributes
|
|
754
|
+
end
|
|
755
|
+
```
|
|
756
|
+
|
|
757
|
+
Then:
|
|
758
|
+
|
|
759
|
+
> `"Some Name"`
|
|
760
|
+
|
|
761
|
+
will become:
|
|
762
|
+
|
|
763
|
+
> `"Some Name"`
|
|
764
|
+
|
|
765
|
+
#### `squish_attributes` ####
|
|
766
|
+
|
|
767
|
+
Squishes attributes (removes all whitespace characters on both ends of the string, and then changes remaining consecutive whitespace groups into one space each).
|
|
768
|
+
|
|
769
|
+
* Callback method: `squish_attributes`
|
|
770
|
+
* Class-level helper: `squish_attributes(*attribute_names)`
|
|
771
|
+
* Class-level helper aliases: `squish_attribute`, `squishes_attribute`, `squishes_attributes`
|
|
772
|
+
* Uses set: `:should_be_squished`
|
|
773
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
774
|
+
* Uses annotations: no
|
|
775
|
+
|
|
776
|
+
Example:
|
|
777
|
+
|
|
778
|
+
```ruby
|
|
779
|
+
class User < ActiveRecord::Base
|
|
780
|
+
include ActiveModel::AttributeFilters::Common::Squeeze
|
|
781
|
+
|
|
782
|
+
squishes_attribute :name
|
|
783
|
+
before_validation :squish_attributes
|
|
784
|
+
end
|
|
785
|
+
```
|
|
786
|
+
|
|
787
|
+
or
|
|
788
|
+
|
|
789
|
+
```ruby
|
|
790
|
+
class User < ActiveRecord::Base
|
|
791
|
+
include ActiveModel::AttributeFilters::Common::Squeeze
|
|
792
|
+
|
|
793
|
+
has_attributes_that :should_be_squished => [ :name ]
|
|
794
|
+
before_validation :squish_attributes
|
|
795
|
+
end
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
Then:
|
|
799
|
+
|
|
800
|
+
> `" Some Name"`
|
|
801
|
+
|
|
802
|
+
will become:
|
|
803
|
+
|
|
804
|
+
> `"Some Name"`
|
|
805
|
+
|
|
806
|
+
### Split ###
|
|
807
|
+
|
|
808
|
+
* Submodule: [`Split`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Split.html)
|
|
809
|
+
|
|
810
|
+
#### `split_attributes` ####
|
|
811
|
+
|
|
812
|
+
Splits attributes into arrays and puts the results into other attributes or into the same attributes.
|
|
813
|
+
|
|
814
|
+
* Callback methods: `split_attributes`
|
|
815
|
+
* Class-level helpers:
|
|
816
|
+
* `split_attributes(attribute_name, parameters_hash)`
|
|
817
|
+
* `split_attributes(attribute_name)`
|
|
818
|
+
* Class-level helper aliases: `split_attribute`, `splits_attribute`, `splits_attributes`
|
|
819
|
+
* Uses set: `:should_be_splitted`
|
|
820
|
+
* Operates on: strings, arrays of strings, hashes of strings (as values)
|
|
821
|
+
* Uses annotations: yes
|
|
822
|
+
* `split_pattern` – a pattern passed to [`split`](http://www.ruby-doc.org/core/String.html#method-i-split) method (optional)
|
|
823
|
+
* `split_limit` – a limit passed to `split` method (optional)
|
|
824
|
+
* `split_into` – attribute names used as destinations for parts (default parameter)
|
|
825
|
+
* `split_flatten` – flag that causes resulting array to be flattened
|
|
826
|
+
* Parameters' aliases (used with class-level helpers):
|
|
827
|
+
* `split_pattern` – `:with`, `:pattern`
|
|
828
|
+
* `split_limit` – `:limit`
|
|
829
|
+
* `split_into` – `:into`, `:to`, `:destination`
|
|
830
|
+
* `split_flatten` – `:flatten`
|
|
831
|
+
|
|
832
|
+
If a source attribute is an array or a hash then the filter will recursively traverse it and
|
|
833
|
+
operate on each element. The filter works the same way as the `split` method from the `String` class
|
|
834
|
+
of Ruby standard library. If the filter encounters an object which is not a string nor an array or a hash,
|
|
835
|
+
it simply leaves the value as is.
|
|
836
|
+
|
|
837
|
+
You can set `:pattern` (`:split_pattern`) and `:limit` (`:split_limit`) arguments passed to
|
|
838
|
+
`split` method but note that **a limit is applied to each processed string separately**,
|
|
839
|
+
not to the resulting array **(if the processed attribute is an array)**.
|
|
840
|
+
|
|
841
|
+
For instance, if there is a string containing 3 words:
|
|
842
|
+
|
|
843
|
+
> `'A B C'`
|
|
844
|
+
|
|
845
|
+
and the limit is set to 2 then the last two words will be left intact
|
|
846
|
+
and placed in a second element of the resulting array:
|
|
847
|
+
|
|
848
|
+
> `['A', 'B C']`
|
|
849
|
+
|
|
850
|
+
If the source is an array:
|
|
851
|
+
|
|
852
|
+
> `['A', 'B B B B', 'C']`
|
|
853
|
+
|
|
854
|
+
then the result of this operation will be the array of arrays:
|
|
855
|
+
|
|
856
|
+
> `[ ['A'], ['B B'], ['C'] ]`
|
|
857
|
+
|
|
858
|
+
As you can see the limit will be applied to its second element.
|
|
859
|
+
|
|
860
|
+
If there are no destination attributes defined (`:into` or `:split_into` option)
|
|
861
|
+
then the resulting array will be written to the currently processed attribute.
|
|
862
|
+
|
|
863
|
+
If there are destination attributes given then then the resulting array
|
|
864
|
+
will be written into them (each subsequent element into each next attribute).
|
|
865
|
+
The elements that don't fit in the resulting collection of attributes
|
|
866
|
+
are simply ignored **unless the limit is given and it's the same as their count**
|
|
867
|
+
(in such case the rest is, as said before, written into the last element).
|
|
868
|
+
|
|
869
|
+
There is also `flatten` (or `:split_flatten`) parameter that causes the resulting array to be
|
|
870
|
+
flattened. Note that it doesn't change how the limits work; they still will be applied but to a single
|
|
871
|
+
split results, not to the whole resulting array (in case of array of arrays).
|
|
872
|
+
|
|
873
|
+
Examples:
|
|
874
|
+
|
|
875
|
+
(We're using `attr_virtual` here, but in some real-life the source may also be
|
|
876
|
+
a real attribute that is written into the database.)
|
|
877
|
+
|
|
878
|
+
```ruby
|
|
879
|
+
class User < ActiveRecord::Base
|
|
880
|
+
# Including common filter for splitting
|
|
881
|
+
include ActiveModel::AttributeFilters::Common::Split
|
|
882
|
+
|
|
883
|
+
# Registering virtual attribute
|
|
884
|
+
attr_virtual :real_name
|
|
885
|
+
|
|
886
|
+
# Adding attribute name to :should_be_splitted set
|
|
887
|
+
splits_attribute :real_name
|
|
888
|
+
|
|
889
|
+
# Registering callback method
|
|
890
|
+
before_validation :split_attributes
|
|
891
|
+
end
|
|
892
|
+
```
|
|
893
|
+
|
|
894
|
+
or without a `split_attributes` helper:
|
|
895
|
+
|
|
896
|
+
```ruby
|
|
897
|
+
class User < ActiveRecord::Base
|
|
898
|
+
# Including common filter for splitting
|
|
899
|
+
include ActiveModel::AttributeFilters::Common::Split
|
|
900
|
+
|
|
901
|
+
# Registering virtual attribute
|
|
902
|
+
attr_virtual :real_name
|
|
903
|
+
attr_accessible :real_name
|
|
904
|
+
|
|
905
|
+
# Adding attribute name to :should_be_splitted set (by hand)
|
|
906
|
+
has_attributes_that :should_be_splitted => :real_name
|
|
907
|
+
|
|
908
|
+
# Registering callback method
|
|
909
|
+
before_validation :split_attributes
|
|
910
|
+
end
|
|
911
|
+
```
|
|
912
|
+
|
|
913
|
+
The result of executing the filter above will be replacement of a string by an array containing
|
|
914
|
+
words (each one in a separate element). The `real_name` attribute is a virtual attribute in this example
|
|
915
|
+
but it could be real attribute. The result will be written as an array into the same attribute since there
|
|
916
|
+
are no destination attributes given. So `'Paul Wolf'` will become `['Paul', 'Wolf']`.
|
|
917
|
+
|
|
918
|
+
Using limit:
|
|
919
|
+
|
|
920
|
+
```ruby
|
|
921
|
+
class User < ActiveRecord::Base
|
|
922
|
+
include ActiveModel::AttributeFilters::Common::Split
|
|
923
|
+
|
|
924
|
+
attr_virtual :real_name
|
|
925
|
+
splits_attribute :real_name, :limit => 2
|
|
926
|
+
before_validation :split_attributes
|
|
927
|
+
end
|
|
928
|
+
```
|
|
929
|
+
|
|
930
|
+
or without a `split_attributes` keyword:
|
|
931
|
+
|
|
932
|
+
```ruby
|
|
933
|
+
class User < ActiveRecord::Base
|
|
934
|
+
include ActiveModel::AttributeFilters::Common::Split
|
|
935
|
+
|
|
936
|
+
attr_virtual :real_name
|
|
937
|
+
|
|
938
|
+
has_attributes_that :should_be_splitted => { :real_name => { :split_limit => 2 } }
|
|
939
|
+
before_validation :split_attributes
|
|
940
|
+
end
|
|
941
|
+
```
|
|
942
|
+
|
|
943
|
+
The result of the above example will be the same as the previous one with the difference that any
|
|
944
|
+
reduntant elements will be left intact and placed as the last element of an array. So for data:
|
|
945
|
+
|
|
946
|
+
> `'Paul Thomas Wolf'`
|
|
947
|
+
|
|
948
|
+
the array will be:
|
|
949
|
+
|
|
950
|
+
> `[ 'Paul', 'Thomas Wolf' ]`
|
|
951
|
+
|
|
952
|
+
Another example, let's write results to some attributes:
|
|
953
|
+
|
|
954
|
+
```ruby
|
|
955
|
+
class User < ActiveRecord::Base
|
|
956
|
+
include ActiveModel::AttributeFilters::Common::Split
|
|
957
|
+
|
|
958
|
+
attr_virtual :real_name
|
|
959
|
+
splits_attribute :real_name, :limit => 2, :into => [ :first_name, :last_name ], :pattern => ' '
|
|
960
|
+
before_validation :split_attributes
|
|
961
|
+
end
|
|
962
|
+
```
|
|
963
|
+
|
|
964
|
+
(The `:pattern` is given here but you may skip it if it's a space.)
|
|
965
|
+
|
|
966
|
+
This will split a value of the `real_name` attribute and place the results in the attributes
|
|
967
|
+
called `first_name` and `last_name`, so for:
|
|
968
|
+
|
|
969
|
+
> `'Paul Thomas Wolf'`
|
|
970
|
+
|
|
971
|
+
the result will be:
|
|
972
|
+
|
|
973
|
+
```
|
|
974
|
+
first_name: 'Paul'
|
|
975
|
+
last_name: 'Thomas Wolf'
|
|
976
|
+
```
|
|
977
|
+
|
|
978
|
+
If you remove the limit, then it will be quite different:
|
|
979
|
+
|
|
980
|
+
```
|
|
981
|
+
first_name: 'Paul'
|
|
982
|
+
last_name: 'Thomas'
|
|
983
|
+
```
|
|
984
|
+
|
|
985
|
+
That's because there are more results than attributes they fit into. You just have to keep in mind
|
|
986
|
+
that this filter behaves like the String's split method with the difference when the results are written
|
|
987
|
+
into other attributes.
|
|
988
|
+
|
|
989
|
+
The pattern parameter (`:pattern` when using `split_attributes` or `:split_pattern` when directly
|
|
990
|
+
annotating attribute in a set) should be a string.
|
|
991
|
+
|
|
992
|
+
### Join ###
|
|
993
|
+
|
|
994
|
+
* Submodule: [`Join`](http://rubydoc.info/gems/attribute-filters/ActiveModel/AttributeFilters/Common/Join.html)
|
|
995
|
+
|
|
996
|
+
#### `join_attributes` ####
|
|
997
|
+
|
|
998
|
+
Joins attributes and places the results into other attributes or into the same attributes as strings.
|
|
999
|
+
|
|
1000
|
+
* Callback method: `join_attributes`
|
|
1001
|
+
* Class-level helpers:
|
|
1002
|
+
* `join_attributes(attribute_name, parameters_hash)` (a.k.a `joint_attribute`)
|
|
1003
|
+
* `join_attributes(attribute_name)` (a.k.a `joint_attribute`)
|
|
1004
|
+
* Class-level helper aliases: `join_attribute`, `joins_attribute`, `joins_attributes`
|
|
1005
|
+
* Uses set: `:should_be_joined`
|
|
1006
|
+
* Operates on: strings, arrays of strings
|
|
1007
|
+
* Uses annotations: yes
|
|
1008
|
+
* `join_separator` – a pattern passed to [`join`](http://www.ruby-doc.org/core/Array.html#method-i-join) method (optional)
|
|
1009
|
+
* `join_compact` – compact flag; if true then an array is compacted before it's joined (optional)
|
|
1010
|
+
* `join_from` – attribute names used as sources for joins (default parameter)
|
|
1011
|
+
* Parameters' aliases (used with class-level helpers):
|
|
1012
|
+
* `join_separator` – `:with`, `:separator`
|
|
1013
|
+
* `join_compact` – `:compact`
|
|
1014
|
+
* `join_from` – `:from`, `:source`, `:sources`
|
|
1015
|
+
|
|
1016
|
+
The join filter uses `join` instance method of the `Array` class to produce single string from multiple strings.
|
|
1017
|
+
These strings may be values of other attributes (source attributes), values of an array stored in an attribute
|
|
1018
|
+
or mix of it. If the `:compact` (`:join_compact` in case of manually annotating a set) parameter is given
|
|
1019
|
+
and it's not `false` nor `nil` then the results are compacted during processing. That means any slices (components)
|
|
1020
|
+
equals to `nil` are removed just before joining.
|
|
1021
|
+
|
|
1022
|
+
If the parameter `:from` (or annotation key `:join_from`) was not given then a currently processed attribute
|
|
1023
|
+
is treated as a source (it should be an array).
|
|
1024
|
+
|
|
1025
|
+
If the joined attribute is a single attribute and its value is an array then all elements of that array will be joined.
|
|
1026
|
+
|
|
1027
|
+
Examples:
|
|
1028
|
+
|
|
1029
|
+
```ruby
|
|
1030
|
+
class User < ActiveRecord::Base
|
|
1031
|
+
include ActiveModel::AttributeFilters::Common::Join
|
|
1032
|
+
|
|
1033
|
+
attr_virtual :first_name, :last_name
|
|
1034
|
+
attr_accessible :first_name, :last_name
|
|
1035
|
+
joins_attributes_into :real_name, :from => [ :first_name, :last_name ]
|
|
1036
|
+
before_validation :join_attributes
|
|
1037
|
+
end
|
|
1038
|
+
```
|
|
1039
|
+
|
|
1040
|
+
you can also switch source with destination:
|
|
1041
|
+
|
|
1042
|
+
```ruby
|
|
1043
|
+
joins_attributes [ :first_name, :last_name ] => :real_name
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
or add a descriptive keyword `:into`:
|
|
1047
|
+
|
|
1048
|
+
```ruby
|
|
1049
|
+
joins_attributes [ :first_name, :last_name ], :into => :real_name
|
|
1050
|
+
```
|
|
1051
|
+
|
|
1052
|
+
Adding parameters:
|
|
1053
|
+
|
|
1054
|
+
```ruby
|
|
1055
|
+
joins_attributes [ :first_name, :last_name ], :into => :real_name, :compact => true
|
|
1056
|
+
```
|
|
1057
|
+
or
|
|
1058
|
+
|
|
1059
|
+
```ruby
|
|
1060
|
+
joins_attributes [ :first_name, :last_name ] => :real_name, :compact => true
|
|
1061
|
+
```
|
|
1062
|
+
|
|
1063
|
+
or without class-level helper:
|
|
1064
|
+
|
|
1065
|
+
```ruby
|
|
1066
|
+
has_attributes_that should_be_joined: { :real_name => { :join_from => [ :first_name, :last_name ], :join_compact => true } }
|
|
1067
|
+
```
|