attribute-filters 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +2 -1
- data/ChangeLog +18 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/Manifest.txt +2 -1
- data/README.md +206 -0
- data/Rakefile +2 -1
- data/attribute-filters.gemspec +6 -3
- data/docs/USAGE +0 -0
- data/lib/attribute-filters/dsl_sets.rb +19 -6
- data/lib/attribute-filters/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +35 -24
- metadata.gz.sig +0 -0
- data/README.rdoc +0 -186
data/.yardopts
CHANGED
data/ChangeLog
CHANGED
@@ -1,3 +1,21 @@
|
|
1
|
+
commit fc3cd20ea98315736374780179bf49637646aa34
|
2
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
3
|
+
Date: Thu Jun 28 14:29:29 2012 +0200
|
4
|
+
|
5
|
+
Release 1.0.2
|
6
|
+
|
7
|
+
commit 4605f5c4cf5e0762c33315e96b1c4bb0574dc045
|
8
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
9
|
+
Date: Thu Jun 28 14:25:22 2012 +0200
|
10
|
+
|
11
|
+
Extended arguments parging for DSL class method attribute_set
|
12
|
+
|
13
|
+
commit 3bb26fc1cc2827e08eec11ea5f57c305184daca1
|
14
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
15
|
+
Date: Thu Jun 28 14:24:40 2012 +0200
|
16
|
+
|
17
|
+
README format changed to Markdown, added some development deps (Redcarped) required by YARD
|
18
|
+
|
1
19
|
commit 2b72084f1eef2a7b8dfa632a2fd918eae203f106
|
2
20
|
Author: Paweł Wilk <siefca@gnu.org>
|
3
21
|
Date: Thu Jun 28 11:53:58 2012 +0200
|
data/Gemfile
CHANGED
@@ -11,6 +11,7 @@ gem "hoe-yard", ">=0.1.2", :group => [:development, :test]
|
|
11
11
|
gem "rspec", ">=2.6.0", :group => [:development, :test]
|
12
12
|
gem "yard", ">=0.7.2", :group => [:development, :test]
|
13
13
|
gem "rdoc", ">=3.8.0", :group => [:development, :test]
|
14
|
+
gem "redcarpet", ">=2.1.0", :group => [:development, :test]
|
14
15
|
gem "bundler", ">=1.0.10", :group => [:development, :test]
|
15
16
|
gem "hoe-bundler", ">=1.1.0", :group => [:development, :test]
|
16
17
|
gem "hoe-gemspec", ">=1.0.0", :group => [:development, :test]
|
data/Gemfile.lock
CHANGED
@@ -50,6 +50,7 @@ GEM
|
|
50
50
|
rake (0.9.2.2)
|
51
51
|
rdoc (3.12)
|
52
52
|
json (~> 1.4)
|
53
|
+
redcarpet (2.1.1)
|
53
54
|
rspec (2.10.0)
|
54
55
|
rspec-core (~> 2.10.0)
|
55
56
|
rspec-expectations (~> 2.10.0)
|
@@ -78,5 +79,6 @@ DEPENDENCIES
|
|
78
79
|
hoe-yard (>= 0.1.2)
|
79
80
|
railties (~> 3.0)
|
80
81
|
rdoc (>= 3.8.0)
|
82
|
+
redcarpet (>= 2.1.0)
|
81
83
|
rspec (>= 2.6.0)
|
82
84
|
yard (>= 0.7.2)
|
data/Manifest.txt
CHANGED
@@ -5,7 +5,7 @@ Gemfile
|
|
5
5
|
Gemfile.lock
|
6
6
|
LGPL-LICENSE
|
7
7
|
Manifest.txt
|
8
|
-
README.
|
8
|
+
README.md
|
9
9
|
Rakefile
|
10
10
|
attribute-filters.gemspec
|
11
11
|
docs/COPYING
|
@@ -13,6 +13,7 @@ docs/HISTORY
|
|
13
13
|
docs/LEGAL
|
14
14
|
docs/LGPL-LICENSE
|
15
15
|
docs/TODO
|
16
|
+
docs/USAGE
|
16
17
|
docs/rdoc.css
|
17
18
|
init.rb
|
18
19
|
lib/attribute-filters.rb
|
data/README.md
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
Attribute Filters for Rails
|
2
|
+
===========================
|
3
|
+
|
4
|
+
**attribute-filters version `1.0`** (**`Sugar, ah honey honey`**)
|
5
|
+
|
6
|
+
**THIS IS BETA!**
|
7
|
+
|
8
|
+
* https://rubygems.org/gems/attribute-filters
|
9
|
+
* https://github.com/siefca/attribute-filters/tree
|
10
|
+
* mailto:pw@gnu.org
|
11
|
+
|
12
|
+
|
13
|
+
Summary
|
14
|
+
-------
|
15
|
+
|
16
|
+
Attribute Filters adds couple of DSL keywords and some syntactic sugar
|
17
|
+
to Rails thereby allowing you to express filtering and groupping
|
18
|
+
model attributes in a concise and clean way.
|
19
|
+
|
20
|
+
When?
|
21
|
+
-----
|
22
|
+
|
23
|
+
If your Rails application often filters the attributes that has changed
|
24
|
+
recently and uses callbacks to achieve that, then you may consider
|
25
|
+
refining that process and write methods for handling common operations
|
26
|
+
not certain attributes. See what I mean below.
|
27
|
+
|
28
|
+
### Before ###
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class User < ActiveRecord::Base
|
32
|
+
|
33
|
+
before_validation :strip_and_downcase_username
|
34
|
+
before_validation :strip_and_downcase_email
|
35
|
+
before_validation :strip_and_capitalize_real_name
|
36
|
+
|
37
|
+
def strip_and_downcase_username
|
38
|
+
if username.present?
|
39
|
+
self.username = self.username.strip.mb_chars.downcase.to_s
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def strip_and_downcase_email
|
44
|
+
if email.present?
|
45
|
+
self.email.strip!
|
46
|
+
self.email.downcase!
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def strip_and_capitalize_real_name
|
51
|
+
if real_name.present?
|
52
|
+
self.real_name = self.real_name.strip.mb_chars.split(' ').
|
53
|
+
map { |n| n.capitalize }.join(' ')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
(The more attributes there is the more messy it becomes and the filtering code is not reusable.)
|
61
|
+
|
62
|
+
### After ###
|
63
|
+
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class User < ActiveRecord::Base
|
67
|
+
|
68
|
+
attributes_that should_be_stripped: [ :username, :email, :real_name ]
|
69
|
+
attributes_that should_be_downcased: [ :username, :email ]
|
70
|
+
attributes_that should_be_capitalized: [ :username, :email ]
|
71
|
+
|
72
|
+
before_validation :strip_names
|
73
|
+
before_validation :downcase_names
|
74
|
+
before_validation :capitalize_names
|
75
|
+
|
76
|
+
def downcase_names
|
77
|
+
filter_attributes_that(:should_be_downcased) do |atr|
|
78
|
+
atr.mb_chars.downcase.to_s
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def capitalize_names
|
83
|
+
filter_attributes_that(:should_be_capitalized) do |atr|
|
84
|
+
atr.mb_chars.split(' ').map { |n| n.capitalize }.join(' ')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def strip_names
|
89
|
+
for_attributes_that(:should_be_stripped) { |atr| atr.strip! }
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
(Names of new attributes that have to be altered in the same way can just be added to sets
|
96
|
+
and you can use the same altering methods across all models if you wish to.)
|
97
|
+
|
98
|
+
Usage
|
99
|
+
-----
|
100
|
+
|
101
|
+
You can use it to do attribute filtering as presented above but you can also
|
102
|
+
try using ActiveModel::AttributeSet directly, which helps to express some logic.
|
103
|
+
For example:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class User < ActiveRecord::Base
|
107
|
+
|
108
|
+
attributes_that_are required_to_trade: [ :username, :home_address, :bank_account ]
|
109
|
+
|
110
|
+
def can_trade?
|
111
|
+
are_attributes(:required_to_trade).all.present?
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
* See [USAGE](http://rubydoc.info/gems/attribute-filters/file/docs/USAGE) for more examples and detailed information about the usage.
|
118
|
+
* See [whole documentation](http://rubydoc.info/gems/attribute-filters/) to browse all documents.
|
119
|
+
|
120
|
+
How it works?
|
121
|
+
-------------
|
122
|
+
|
123
|
+
It creates a new Active Model module called ActiveModel::AttributeFilters. That module
|
124
|
+
contains the needed DSL that goes into your models. It also creates ActiveModel::AttributeSet
|
125
|
+
class which is just a new kind of set, a structure for storing sets of attribute names.
|
126
|
+
|
127
|
+
Then it forces Rails to include the AttributeFilters in any model that
|
128
|
+
at any time will include ActiveModel::AttributeMethods. The last one is included
|
129
|
+
quite often; e.g. ActiveRecord and other popular ORM-s use it. (I'm calling it
|
130
|
+
"the accompanying module".)
|
131
|
+
|
132
|
+
That's why you can make use of attribute filters without explicitly including
|
133
|
+
the module, as long as your application relies on any popular ORM.
|
134
|
+
|
135
|
+
If something will go wrong however or your application is somehow unusual, you can always
|
136
|
+
include the AttributeFilters module manually in any of your models:
|
137
|
+
|
138
|
+
class ExampleModel
|
139
|
+
include ActiveModel::AttributeFilters
|
140
|
+
end
|
141
|
+
|
142
|
+
Requirements
|
143
|
+
------------
|
144
|
+
|
145
|
+
* [activemodel](https://rubygems.org/gems/activemodel)
|
146
|
+
* [rake](https://rubygems.org/gems/rake)
|
147
|
+
* [rubygems](http://docs.rubygems.org/)
|
148
|
+
* [bundler](http://gembundler.com/)
|
149
|
+
|
150
|
+
Download
|
151
|
+
--------
|
152
|
+
|
153
|
+
### Source code ###
|
154
|
+
|
155
|
+
* https://github.com/siefca/attribute-filters/tree
|
156
|
+
* `git clone git://github.com/siefca/attribute-filters.git`
|
157
|
+
|
158
|
+
### Gem ###
|
159
|
+
|
160
|
+
* https://rubygems.org/gems/attribute-filters
|
161
|
+
|
162
|
+
Installation
|
163
|
+
------------
|
164
|
+
|
165
|
+
```
|
166
|
+
gem install attribute-filters
|
167
|
+
```
|
168
|
+
|
169
|
+
Specs
|
170
|
+
-----
|
171
|
+
|
172
|
+
You can run RSpec examples both with
|
173
|
+
|
174
|
+
* `rake spec` or just `rake`
|
175
|
+
* run a test file directly, e.g. `ruby -Ilib -Ispec spec/attribute-filters_spec.rb`
|
176
|
+
|
177
|
+
Common rake tasks
|
178
|
+
-----------------
|
179
|
+
|
180
|
+
* `bundle exec rake bundler:gemfile` – regenerate the `Gemfile`
|
181
|
+
* `bundle exec rake docs` – render the documentation (output in the subdirectory directory `doc`)
|
182
|
+
* `bundle exec rake gem` – builds package (output in the subdirectory `pkg`)
|
183
|
+
* `bundle exec rake test` – performs tests
|
184
|
+
* `bundle exec rake Manifest.txt` – regenerates the `Manifest.txt` file
|
185
|
+
* `bundle exec rake ChangeLog` – regenerates the `ChangeLog` file
|
186
|
+
|
187
|
+
Credits
|
188
|
+
-------
|
189
|
+
|
190
|
+
* [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.
|
191
|
+
* [MrZYX (Jonne Haß)](https://github.com/MrZYX) contributed by giving me some hints and answering basic questions on IRC – THX!
|
192
|
+
|
193
|
+
License
|
194
|
+
-------
|
195
|
+
|
196
|
+
Copyright (c) 2012 by Paweł Wilk.
|
197
|
+
|
198
|
+
attribute-filters is copyrighted software owned by Paweł Wilk (pw@gnu.org).
|
199
|
+
You may redistribute and/or modify this software as long as you
|
200
|
+
comply with either the terms of the LGPL (see {file:docs/LGPL}),
|
201
|
+
or Ruby's license (see {file:docs/COPYING}).
|
202
|
+
|
203
|
+
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS
|
204
|
+
OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION,
|
205
|
+
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
206
|
+
FOR A PARTICULAR PURPOSE.
|
data/Rakefile
CHANGED
@@ -39,7 +39,7 @@ Hoe.spec 'attribute-filters' do
|
|
39
39
|
|
40
40
|
self.remote_rdoc_dir = ''
|
41
41
|
self.rsync_args << '--chmod=a+rX'
|
42
|
-
self.readme_file = 'README.
|
42
|
+
self.readme_file = 'README.md'
|
43
43
|
self.history_file = 'docs/HISTORY'
|
44
44
|
|
45
45
|
extra_deps << ['railties', '~> 3.0'] <<
|
@@ -47,6 +47,7 @@ Hoe.spec 'attribute-filters' do
|
|
47
47
|
extra_dev_deps << ['rspec', '>= 2.6.0'] <<
|
48
48
|
['yard', '>= 0.7.2'] <<
|
49
49
|
['rdoc', '>= 3.8.0'] <<
|
50
|
+
['redcarpet', '>= 2.1.0'] <<
|
50
51
|
['bundler', '>= 1.0.10'] <<
|
51
52
|
['hoe-bundler', '>= 1.1.0'] <<
|
52
53
|
['hoe-gemspec', '>= 1.0.0']
|
data/attribute-filters.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "attribute-filters"
|
5
|
-
s.version = "1.0.
|
5
|
+
s.version = "1.0.2.20120628142850"
|
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"]
|
@@ -10,8 +10,8 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.date = "2012-06-28"
|
11
11
|
s.description = "Concise way of filtering model attributes in Rails."
|
12
12
|
s.email = ["pw@gnu.org"]
|
13
|
-
s.extra_rdoc_files = ["Manifest.txt"
|
14
|
-
s.files = [".rspec", ".yardopts", "ChangeLog", "Gemfile", "Gemfile.lock", "LGPL-LICENSE", "Manifest.txt", "README.
|
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", "docs/rdoc.css", "init.rb", "lib/attribute-filters.rb", "lib/attribute-filters/attribute_set.rb", "lib/attribute-filters/attribute_set_query.rb", "lib/attribute-filters/common_filters.rb", "lib/attribute-filters/dsl_filters.rb", "lib/attribute-filters/dsl_sets.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"]
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
s.add_development_dependency(%q<rspec>, [">= 2.6.0"])
|
31
31
|
s.add_development_dependency(%q<yard>, [">= 0.7.2"])
|
32
32
|
s.add_development_dependency(%q<rdoc>, [">= 3.8.0"])
|
33
|
+
s.add_development_dependency(%q<redcarpet>, [">= 2.1.0"])
|
33
34
|
s.add_development_dependency(%q<bundler>, [">= 1.0.10"])
|
34
35
|
s.add_development_dependency(%q<hoe-bundler>, [">= 1.1.0"])
|
35
36
|
s.add_development_dependency(%q<hoe-gemspec>, [">= 1.0.0"])
|
@@ -41,6 +42,7 @@ Gem::Specification.new do |s|
|
|
41
42
|
s.add_dependency(%q<rspec>, [">= 2.6.0"])
|
42
43
|
s.add_dependency(%q<yard>, [">= 0.7.2"])
|
43
44
|
s.add_dependency(%q<rdoc>, [">= 3.8.0"])
|
45
|
+
s.add_dependency(%q<redcarpet>, [">= 2.1.0"])
|
44
46
|
s.add_dependency(%q<bundler>, [">= 1.0.10"])
|
45
47
|
s.add_dependency(%q<hoe-bundler>, [">= 1.1.0"])
|
46
48
|
s.add_dependency(%q<hoe-gemspec>, [">= 1.0.0"])
|
@@ -53,6 +55,7 @@ Gem::Specification.new do |s|
|
|
53
55
|
s.add_dependency(%q<rspec>, [">= 2.6.0"])
|
54
56
|
s.add_dependency(%q<yard>, [">= 0.7.2"])
|
55
57
|
s.add_dependency(%q<rdoc>, [">= 3.8.0"])
|
58
|
+
s.add_dependency(%q<redcarpet>, [">= 2.1.0"])
|
56
59
|
s.add_dependency(%q<bundler>, [">= 1.0.10"])
|
57
60
|
s.add_dependency(%q<hoe-bundler>, [">= 1.1.0"])
|
58
61
|
s.add_dependency(%q<hoe-gemspec>, [">= 1.0.0"])
|
data/docs/USAGE
ADDED
File without changes
|
@@ -63,21 +63,34 @@ module ActiveModel
|
|
63
63
|
# Creates one new set or many new sets of attribute of the given name.
|
64
64
|
# @param set_options [Hash{Symbol,String => Array<Symbol,String>}] hash containing set names and arrays of attributes
|
65
65
|
# @return [nil]
|
66
|
+
#
|
67
|
+
# @overload attribute_set(set_options, *attribute_names)
|
68
|
+
# Creates one new set of attributes of the given names.
|
69
|
+
# @param set_options [Hash{Symbol,String => String,Array<Symbol,String>}] hash containing set names and arrays of attributes
|
70
|
+
# @param attribute_names [Array<Symbol,String>] names of additional attributes to be stored in set
|
71
|
+
# @return [nil]
|
66
72
|
def attribute_set(*args)
|
67
73
|
case args.size
|
68
74
|
when 0
|
69
75
|
attribute_sets
|
70
76
|
when 1
|
71
|
-
|
72
|
-
if
|
73
|
-
|
77
|
+
first_arg = args.first
|
78
|
+
if first_arg.is_a?(Hash)
|
79
|
+
first_arg.each_pair { |k,v| attribute_set(k,v) }
|
74
80
|
nil
|
75
81
|
else
|
76
|
-
attribute_sets[
|
82
|
+
attribute_sets[first_arg.to_sym] || ActiveModel::AttributeSet.new.freeze
|
77
83
|
end
|
78
84
|
else
|
79
|
-
|
80
|
-
|
85
|
+
first_arg = args.shift
|
86
|
+
if first_arg.is_a?(Hash)
|
87
|
+
first_arg.each_pair do |k,v|
|
88
|
+
attribute_set(k,v,args)
|
89
|
+
end
|
90
|
+
else
|
91
|
+
set_name = first_arg.to_sym
|
92
|
+
attribute_sets[set_name] = ActiveModel::AttributeSet.new(args.flatten.compact.map{|a|a.to_s}).freeze
|
93
|
+
end
|
81
94
|
nil
|
82
95
|
end
|
83
96
|
end
|
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.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -52,7 +52,7 @@ date: 2012-06-28 00:00:00.000000000 Z
|
|
52
52
|
dependencies:
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: railties
|
55
|
-
requirement: &
|
55
|
+
requirement: &2157562220 !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: *2157562220
|
64
64
|
- !ruby/object:Gem::Dependency
|
65
65
|
name: activemodel
|
66
|
-
requirement: &
|
66
|
+
requirement: &2157561760 !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: *2157561760
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
76
|
name: hoe-yard
|
77
|
-
requirement: &
|
77
|
+
requirement: &2157561300 !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: *2157561300
|
86
86
|
- !ruby/object:Gem::Dependency
|
87
87
|
name: rspec
|
88
|
-
requirement: &
|
88
|
+
requirement: &2157560820 !ruby/object:Gem::Requirement
|
89
89
|
none: false
|
90
90
|
requirements:
|
91
91
|
- - ! '>='
|
@@ -93,10 +93,10 @@ dependencies:
|
|
93
93
|
version: 2.6.0
|
94
94
|
type: :development
|
95
95
|
prerelease: false
|
96
|
-
version_requirements: *
|
96
|
+
version_requirements: *2157560820
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: yard
|
99
|
-
requirement: &
|
99
|
+
requirement: &2157560280 !ruby/object:Gem::Requirement
|
100
100
|
none: false
|
101
101
|
requirements:
|
102
102
|
- - ! '>='
|
@@ -104,10 +104,10 @@ dependencies:
|
|
104
104
|
version: 0.7.2
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
|
-
version_requirements: *
|
107
|
+
version_requirements: *2157560280
|
108
108
|
- !ruby/object:Gem::Dependency
|
109
109
|
name: rdoc
|
110
|
-
requirement: &
|
110
|
+
requirement: &2157559820 !ruby/object:Gem::Requirement
|
111
111
|
none: false
|
112
112
|
requirements:
|
113
113
|
- - ! '>='
|
@@ -115,10 +115,21 @@ dependencies:
|
|
115
115
|
version: 3.8.0
|
116
116
|
type: :development
|
117
117
|
prerelease: false
|
118
|
-
version_requirements: *
|
118
|
+
version_requirements: *2157559820
|
119
|
+
- !ruby/object:Gem::Dependency
|
120
|
+
name: redcarpet
|
121
|
+
requirement: &2157559380 !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 2.1.0
|
127
|
+
type: :development
|
128
|
+
prerelease: false
|
129
|
+
version_requirements: *2157559380
|
119
130
|
- !ruby/object:Gem::Dependency
|
120
131
|
name: bundler
|
121
|
-
requirement: &
|
132
|
+
requirement: &2157558880 !ruby/object:Gem::Requirement
|
122
133
|
none: false
|
123
134
|
requirements:
|
124
135
|
- - ! '>='
|
@@ -126,10 +137,10 @@ dependencies:
|
|
126
137
|
version: 1.0.10
|
127
138
|
type: :development
|
128
139
|
prerelease: false
|
129
|
-
version_requirements: *
|
140
|
+
version_requirements: *2157558880
|
130
141
|
- !ruby/object:Gem::Dependency
|
131
142
|
name: hoe-bundler
|
132
|
-
requirement: &
|
143
|
+
requirement: &2157558440 !ruby/object:Gem::Requirement
|
133
144
|
none: false
|
134
145
|
requirements:
|
135
146
|
- - ! '>='
|
@@ -137,10 +148,10 @@ dependencies:
|
|
137
148
|
version: 1.1.0
|
138
149
|
type: :development
|
139
150
|
prerelease: false
|
140
|
-
version_requirements: *
|
151
|
+
version_requirements: *2157558440
|
141
152
|
- !ruby/object:Gem::Dependency
|
142
153
|
name: hoe-gemspec
|
143
|
-
requirement: &
|
154
|
+
requirement: &2157574360 !ruby/object:Gem::Requirement
|
144
155
|
none: false
|
145
156
|
requirements:
|
146
157
|
- - ! '>='
|
@@ -148,10 +159,10 @@ dependencies:
|
|
148
159
|
version: 1.0.0
|
149
160
|
type: :development
|
150
161
|
prerelease: false
|
151
|
-
version_requirements: *
|
162
|
+
version_requirements: *2157574360
|
152
163
|
- !ruby/object:Gem::Dependency
|
153
164
|
name: hoe
|
154
|
-
requirement: &
|
165
|
+
requirement: &2157573920 !ruby/object:Gem::Requirement
|
155
166
|
none: false
|
156
167
|
requirements:
|
157
168
|
- - ~>
|
@@ -159,7 +170,7 @@ dependencies:
|
|
159
170
|
version: '2.16'
|
160
171
|
type: :development
|
161
172
|
prerelease: false
|
162
|
-
version_requirements: *
|
173
|
+
version_requirements: *2157573920
|
163
174
|
description: Concise way of filtering model attributes in Rails.
|
164
175
|
email:
|
165
176
|
- pw@gnu.org
|
@@ -167,7 +178,6 @@ executables: []
|
|
167
178
|
extensions: []
|
168
179
|
extra_rdoc_files:
|
169
180
|
- Manifest.txt
|
170
|
-
- README.rdoc
|
171
181
|
files:
|
172
182
|
- .rspec
|
173
183
|
- .yardopts
|
@@ -176,7 +186,7 @@ files:
|
|
176
186
|
- Gemfile.lock
|
177
187
|
- LGPL-LICENSE
|
178
188
|
- Manifest.txt
|
179
|
-
- README.
|
189
|
+
- README.md
|
180
190
|
- Rakefile
|
181
191
|
- attribute-filters.gemspec
|
182
192
|
- docs/COPYING
|
@@ -184,6 +194,7 @@ files:
|
|
184
194
|
- docs/LEGAL
|
185
195
|
- docs/LGPL-LICENSE
|
186
196
|
- docs/TODO
|
197
|
+
- docs/USAGE
|
187
198
|
- docs/rdoc.css
|
188
199
|
- init.rb
|
189
200
|
- lib/attribute-filters.rb
|
@@ -214,7 +225,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
214
225
|
version: '0'
|
215
226
|
segments:
|
216
227
|
- 0
|
217
|
-
hash:
|
228
|
+
hash: -1969500921463447156
|
218
229
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
219
230
|
none: false
|
220
231
|
requirements:
|
metadata.gz.sig
CHANGED
Binary file
|
data/README.rdoc
DELETED
@@ -1,186 +0,0 @@
|
|
1
|
-
= Attribute Filters for Rails
|
2
|
-
|
3
|
-
<b>attribute-filters version <tt>1.0.0</tt></b> (<b><tt>Sugar, ah honey honey</tt></b>)
|
4
|
-
|
5
|
-
<b>THIS IS BETA!</b>
|
6
|
-
|
7
|
-
* https://rubygems.org/gems/attribute-filters
|
8
|
-
* https://github.com/siefca/attribute-filters/tree
|
9
|
-
* mailto:pw@gnu.org
|
10
|
-
|
11
|
-
|
12
|
-
== Summary
|
13
|
-
|
14
|
-
Attribute Filters adds couple of DSL keywords and some syntactic sugar
|
15
|
-
to Rails thereby allowing you to express filtering and groupping
|
16
|
-
model attributes in a concise and clean way.
|
17
|
-
|
18
|
-
== When?
|
19
|
-
|
20
|
-
If your Rails application often filters the attributes that has changed
|
21
|
-
recently and uses callbacks to achieve that, then you may consider
|
22
|
-
refining that process and write methods for handling common operations
|
23
|
-
not certain attributes. See what I mean below.
|
24
|
-
|
25
|
-
=== Before
|
26
|
-
|
27
|
-
class User < ActiveRecord::Base
|
28
|
-
|
29
|
-
before_validation :strip_and_downcase_username
|
30
|
-
before_validation :strip_and_downcase_email
|
31
|
-
before_validation :strip_and_capitalize_real_name
|
32
|
-
|
33
|
-
def strip_and_downcase_username
|
34
|
-
if username.present?
|
35
|
-
self.username = self.username.strip.mb_chars.downcase.to_s
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def strip_and_downcase_email
|
40
|
-
if email.present?
|
41
|
-
self.email.strip!
|
42
|
-
self.email.downcase!
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def strip_and_capitalize_real_name
|
47
|
-
if real_name.present?
|
48
|
-
self.real_name = self.real_name.strip.mb_chars.split(' ').
|
49
|
-
map { |n| n.capitalize }.join(' ')
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
(The more attributes there is the more messy it becomes and the filtering code is not reusable.)
|
56
|
-
|
57
|
-
=== After
|
58
|
-
|
59
|
-
class User < ActiveRecord::Base
|
60
|
-
|
61
|
-
attributes_that :should_be_downcased => [ :username, :email ]
|
62
|
-
attributes_that :should_be_stripped => [ :username, :email, :real_name ]
|
63
|
-
attributes_that :should_be_capitalized => [ :username, :email ]
|
64
|
-
|
65
|
-
before_validation :strip_names
|
66
|
-
before_validation :downcase_names
|
67
|
-
before_validation :capitalize_names
|
68
|
-
|
69
|
-
def downcase_names
|
70
|
-
filter_attributes_that(:should_be_downcased) do |atr|
|
71
|
-
atr.mb_chars.downcase.to_s
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def capitalize_names
|
76
|
-
filter_attributes_that(:should_be_capitalized) do |atr|
|
77
|
-
atr.mb_chars.split(' ').map { |n| n.capitalize }.join(' ')
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def strip_names
|
82
|
-
for_attributes_that(:should_be_stripped) { |atr| atr.strip! }
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
(Names of new attributes that have to be altered in the same way can just be added to sets
|
88
|
-
and you can use the same altering methods across all models if you wish to.)
|
89
|
-
|
90
|
-
== Usage
|
91
|
-
|
92
|
-
You can use it to do attribute filtering as presented above but you can also
|
93
|
-
try using ActiveModel::AttributeSet directly, which helps to express some logic.
|
94
|
-
For example:
|
95
|
-
|
96
|
-
class User < ActiveRecord::Base
|
97
|
-
|
98
|
-
attributes_that_are :required_to_trade => [ :username, :home_address, :bank_account ]
|
99
|
-
|
100
|
-
def can_trade?
|
101
|
-
are_attributes(:required_to_trade).all.present?
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
* See {USAGE}[http://rubydoc.info/gems/attribute-filters/file/docs/USAGE] for more examples and detailed information about the usage.
|
107
|
-
* See {whole documentation}[http://rubydoc.info/gems/attribute-filters/] to browse all documents.
|
108
|
-
|
109
|
-
== How it works?
|
110
|
-
|
111
|
-
It creates a new Active Model module called ActiveModel::AttributeFilters. That module
|
112
|
-
contains the needed DSL that goes into your models. It also creates ActiveModel::AttributeSet
|
113
|
-
class which is just a new kind of set, a structure for storing sets of attribute names.
|
114
|
-
|
115
|
-
Then it forces Rails to include the AttributeFilters in any model that
|
116
|
-
at any time will include ActiveModel::AttributeMethods. The last one is included
|
117
|
-
quite often; e.g. ActiveRecord and other popular ORM-s use it. (I'm calling it
|
118
|
-
"the accompanying module".)
|
119
|
-
|
120
|
-
That's why you can make use of attribute filters without explicitly including
|
121
|
-
the module, as long as your application relies on any popular ORM.
|
122
|
-
|
123
|
-
If something will go wrong however or your application is somehow unusual, you can always
|
124
|
-
include the AttributeFilters module manually in any of your models:
|
125
|
-
|
126
|
-
class ExampleModel
|
127
|
-
include ActiveModel::AttributeFilters
|
128
|
-
end
|
129
|
-
|
130
|
-
== Requirements
|
131
|
-
|
132
|
-
* activemodel[https://rubygems.org/gems/activemodel]
|
133
|
-
* rake[https://rubygems.org/gems/rake]
|
134
|
-
* rubygems[http://docs.rubygems.org/]
|
135
|
-
* bundler[http://gembundler.com/]
|
136
|
-
|
137
|
-
== Download
|
138
|
-
|
139
|
-
==== Source code
|
140
|
-
|
141
|
-
* https://github.com/siefca/attribute-filters/tree
|
142
|
-
* <tt>git clone git://github.com/siefca/attribute-filters.git</tt>
|
143
|
-
|
144
|
-
==== Gem
|
145
|
-
|
146
|
-
* https://rubygems.org/gems/attribute-filters
|
147
|
-
|
148
|
-
== Installation
|
149
|
-
|
150
|
-
* <tt>gem install attribute-filters</tt>
|
151
|
-
|
152
|
-
== Specs
|
153
|
-
|
154
|
-
You can run RSpec examples both with
|
155
|
-
|
156
|
-
* <tt>rake spec</tt> or just <tt>rake</tt>
|
157
|
-
* run a test file directly, e.g. <tt>ruby -Ilib -Ispec spec/attribute-filters_spec.rb</tt>
|
158
|
-
|
159
|
-
== Common rake tasks
|
160
|
-
|
161
|
-
* <tt>bundle exec rake bundler:gemfile</tt> – regenerate the +Gemfile+
|
162
|
-
* <tt>bundle exec rake docs</tt> – render the documentation (output in the subdirectory directory +doc+)
|
163
|
-
* <tt>bundle exec rake gem</tt> – builds package (output in the subdirectory +pkg+)
|
164
|
-
* <tt>bundle exec rake test</tt> – performs tests
|
165
|
-
* <tt>bundle exec rake Manifest.txt</tt> – regenerates the +Manifest.txt+ file
|
166
|
-
* <tt>bundle exec rake ChangeLog</tt> – regenerates the +ChangeLog+ file
|
167
|
-
|
168
|
-
== Credits
|
169
|
-
|
170
|
-
* {iConsulting}[http://www.iconsulting.pl/] supports Free Software and has
|
171
|
-
contributed to this library by paying for me to eat when I've been coding.
|
172
|
-
* MrZYX (Jonne Haß) contributed by giving me some hints and answering basic questions on IRC – THX!
|
173
|
-
|
174
|
-
== License
|
175
|
-
|
176
|
-
Copyright (c) 2012 by Paweł Wilk.
|
177
|
-
|
178
|
-
attribute-filters is copyrighted software owned by Paweł Wilk (pw@gnu.org).
|
179
|
-
You may redistribute and/or modify this software as long as you
|
180
|
-
comply with either the terms of the LGPL (see {file:docs/LGPL}),
|
181
|
-
or Ruby's license (see {file:docs/COPYING}).
|
182
|
-
|
183
|
-
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS
|
184
|
-
OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION,
|
185
|
-
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
186
|
-
FOR A PARTICULAR PURPOSE.
|