attribute-filters 1.0.2 → 1.0.3
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/.yardopts +2 -0
- data/ChangeLog +69 -0
- data/Manifest.txt +1 -0
- data/README.md +24 -11
- data/Rakefile +10 -9
- data/attribute-filters.gemspec +3 -3
- data/docs/HISTORY +6 -0
- data/docs/TODO +2 -1
- data/docs/rdoc.css +1 -0
- data/docs/yard-tpl/default/fulldoc/html/css/common.css +5 -0
- data/lib/attribute-filters/attribute_set_query.rb +1 -1
- data/lib/attribute-filters/dsl_filters.rb +156 -98
- data/lib/attribute-filters/railtie.rb +35 -32
- data/lib/attribute-filters/version.rb +17 -15
- data.tar.gz.sig +0 -0
- metadata +26 -25
- metadata.gz.sig +0 -0
data/.yardopts
CHANGED
data/ChangeLog
CHANGED
@@ -1,3 +1,72 @@
|
|
1
|
+
commit 60ca933e0a03f70ea9f04684ebab8b888388e9a3
|
2
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
3
|
+
Date: Tue Jul 10 11:35:52 2012 +0200
|
4
|
+
|
5
|
+
ChangeLog updated
|
6
|
+
|
7
|
+
commit 19e31e31f6f9039eec91cdb97133112cd597d429
|
8
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
9
|
+
Date: Tue Jul 10 11:27:31 2012 +0200
|
10
|
+
|
11
|
+
Bugfix release 1.0.3
|
12
|
+
|
13
|
+
commit 6b5aaf23fe3cb6926dac83f733544af16d3b726b
|
14
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
15
|
+
Date: Fri Jun 29 02:13:21 2012 +0200
|
16
|
+
|
17
|
+
Updated release-related files
|
18
|
+
|
19
|
+
commit c4fe9eb630f342f2b5ffd4d6ff72948e0e77a864
|
20
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
21
|
+
Date: Fri Jun 29 02:10:56 2012 +0200
|
22
|
+
|
23
|
+
Attributes filtering code optimization
|
24
|
+
|
25
|
+
- Removed unneeded caching that might cause troubles;
|
26
|
+
- Removed calls to accessible_attributes;
|
27
|
+
- Added a flag to iterate over/filter all attributes from set;
|
28
|
+
- Added a flag controlling attributes presence checking;
|
29
|
+
- Moved main logic to the method operate_on_attrs_from_set;
|
30
|
+
- Renamed the method call_attrs_from_set to for_each_attr_from_set;
|
31
|
+
- Module is now more DRY and clean;
|
32
|
+
- Inline documentation updated.
|
33
|
+
|
34
|
+
commit 11e85ab7d0f0028c2569b0c1f46e6296559e0bda
|
35
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
36
|
+
Date: Fri Jun 29 02:04:10 2012 +0200
|
37
|
+
|
38
|
+
Module AttributeFilters moved totally to ActiveModel namespace
|
39
|
+
|
40
|
+
commit 8ddf4312278fa6538ce746b210be504e817bc265
|
41
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
42
|
+
Date: Fri Jun 29 02:03:00 2012 +0200
|
43
|
+
|
44
|
+
README updated
|
45
|
+
|
46
|
+
commit fd5ece4acf16b0beb068f69f0f874019a8383879
|
47
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
48
|
+
Date: Fri Jun 29 02:02:49 2012 +0200
|
49
|
+
|
50
|
+
Added custom.css and custom YARD template
|
51
|
+
|
52
|
+
commit d60d50b868f9b4a0eb91960ba4e5fa9a573da7b1
|
53
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
54
|
+
Date: Thu Jun 28 15:09:32 2012 +0200
|
55
|
+
|
56
|
+
README updated
|
57
|
+
|
58
|
+
commit c3353208b5787ce20b80daa129a6cbd6eafcafbc
|
59
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
60
|
+
Date: Thu Jun 28 15:03:54 2012 +0200
|
61
|
+
|
62
|
+
README updated
|
63
|
+
|
64
|
+
commit 1db1ff90bc56e6798d1bd6e80e43345fcded9c26
|
65
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
66
|
+
Date: Thu Jun 28 14:47:13 2012 +0200
|
67
|
+
|
68
|
+
README updated
|
69
|
+
|
1
70
|
commit fc3cd20ea98315736374780179bf49637646aa34
|
2
71
|
Author: Paweł Wilk <siefca@gnu.org>
|
3
72
|
Date: Thu Jun 28 14:29:29 2012 +0200
|
data/Manifest.txt
CHANGED
data/README.md
CHANGED
@@ -20,10 +20,18 @@ model attributes in a concise and clean way.
|
|
20
20
|
When?
|
21
21
|
-----
|
22
22
|
|
23
|
-
|
24
|
-
recently and uses callbacks to
|
25
|
-
|
26
|
-
|
23
|
+
You may want to try it when your Rails application often modifies
|
24
|
+
attribute values that changed recently and uses callbacks to do that.
|
25
|
+
|
26
|
+
When the number of attributes that are altered in such a way increases
|
27
|
+
then you can observe that the same thing happends with your filtering
|
28
|
+
methods. That's because each one is tied to some attribute.
|
29
|
+
|
30
|
+
To refine that process you may write more generic methods
|
31
|
+
for altering attributes. They should be designed to handle
|
32
|
+
common operations and not tied to certain attributes.
|
33
|
+
|
34
|
+
Enough words, let's see that approach in action.
|
27
35
|
|
28
36
|
### Before ###
|
29
37
|
|
@@ -57,7 +65,8 @@ class User < ActiveRecord::Base
|
|
57
65
|
end
|
58
66
|
```
|
59
67
|
|
60
|
-
|
68
|
+
The more attributes there is the more messy it becomes.
|
69
|
+
The filtering code is not reusable since it operates on specific attributes.
|
61
70
|
|
62
71
|
### After ###
|
63
72
|
|
@@ -92,8 +101,10 @@ class User < ActiveRecord::Base
|
|
92
101
|
end
|
93
102
|
```
|
94
103
|
|
95
|
-
|
96
|
-
and
|
104
|
+
Attributes that have to be altered in a common ways simply may be added to sets
|
105
|
+
and then filtered with more generic methods. You can share these methods
|
106
|
+
across all your models if you wish to by putting them into some base class
|
107
|
+
or (better) by including your own handy module to your models.
|
97
108
|
|
98
109
|
Usage
|
99
110
|
-----
|
@@ -132,12 +143,14 @@ quite often; e.g. ActiveRecord and other popular ORM-s use it. (I'm calling it
|
|
132
143
|
That's why you can make use of attribute filters without explicitly including
|
133
144
|
the module, as long as your application relies on any popular ORM.
|
134
145
|
|
135
|
-
|
146
|
+
However, if something would go wrong or your application is somehow unusual, you can always
|
136
147
|
include the AttributeFilters module manually in any of your models:
|
137
148
|
|
138
|
-
|
139
|
-
|
140
|
-
|
149
|
+
```
|
150
|
+
class ExampleModel
|
151
|
+
include ActiveModel::AttributeFilters
|
152
|
+
end
|
153
|
+
```
|
141
154
|
|
142
155
|
Requirements
|
143
156
|
------------
|
data/Rakefile
CHANGED
@@ -29,13 +29,13 @@ Hoe.plugin :yard
|
|
29
29
|
Hoe.plugin :gemspec
|
30
30
|
|
31
31
|
Hoe.spec 'attribute-filters' do
|
32
|
-
developer AttributeFilters::DEVELOPER, AttributeFilters::EMAIL
|
32
|
+
developer ActiveModel::AttributeFilters::DEVELOPER, ActiveModel::AttributeFilters::EMAIL
|
33
33
|
|
34
|
-
self.version = AttributeFilters::VERSION
|
35
|
-
self.rubyforge_name = AttributeFilters::NAME
|
36
|
-
self.summary = AttributeFilters::SUMMARY
|
37
|
-
self.description = AttributeFilters::DESCRIPTION
|
38
|
-
self.url = AttributeFilters::URL
|
34
|
+
self.version = ActiveModel::AttributeFilters::VERSION
|
35
|
+
self.rubyforge_name = ActiveModel::AttributeFilters::NAME
|
36
|
+
self.summary = ActiveModel::AttributeFilters::SUMMARY
|
37
|
+
self.description = ActiveModel::AttributeFilters::DESCRIPTION
|
38
|
+
self.url = ActiveModel::AttributeFilters::URL
|
39
39
|
|
40
40
|
self.remote_rdoc_dir = ''
|
41
41
|
self.rsync_args << '--chmod=a+rX'
|
@@ -76,12 +76,13 @@ end
|
|
76
76
|
|
77
77
|
desc "Create signed tag in Git"
|
78
78
|
task :tag do
|
79
|
-
sh %{git tag -s v#{AttributeFilters::VERSION} -m 'version #{AttributeFilters::VERSION}'}
|
79
|
+
sh %{git tag -s v#{ActiveModel::AttributeFilters::VERSION} -m 'version #{ActiveModel::AttributeFilters::VERSION}'}
|
80
80
|
end
|
81
81
|
|
82
82
|
desc "Create external GnuPG signature for Gem"
|
83
83
|
task :gemsign do
|
84
|
-
sh %{gpg -u #{AttributeFilters::EMAIL}
|
85
|
-
-
|
84
|
+
sh %{gpg -u #{ActiveModel::AttributeFilters::EMAIL} \
|
85
|
+
-ab pkg/#{ActiveModel::AttributeFilters::NAME}-#{ActiveModel::AttributeFilters::VERSION}.gem \
|
86
|
+
-o pkg/#{ActiveModel::AttributeFilters::NAME}-#{ActiveModel::AttributeFilters::VERSION}.gem.sig}
|
86
87
|
end
|
87
88
|
|
data/attribute-filters.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
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.3.20120710112322"
|
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-07-10"
|
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", "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"]
|
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", "docs/yard-tpl/default/fulldoc/html/css/common.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"]
|
data/docs/HISTORY
CHANGED
data/docs/TODO
CHANGED
data/docs/rdoc.css
CHANGED
@@ -66,7 +66,7 @@ module ActiveModel
|
|
66
66
|
@attribute_set.method(method_sym).call(*args, &block)
|
67
67
|
end
|
68
68
|
else
|
69
|
-
|
69
|
+
m, args, block = @next_method
|
70
70
|
@next_method = nil
|
71
71
|
@attribute_set.method(m).call { |a| @am_object[a].method(method_sym).call(*args, &block) }
|
72
72
|
end
|
@@ -8,71 +8,85 @@
|
|
8
8
|
|
9
9
|
module ActiveModel
|
10
10
|
module AttributeFilters
|
11
|
+
# @private
|
12
|
+
PROCESSING_FLAGS = {
|
13
|
+
:process_blank => false,
|
14
|
+
:process_all => false,
|
15
|
+
:no_presence_check => false
|
16
|
+
}.freeze
|
17
|
+
|
11
18
|
# Gets names of attributes for which filters should be applied by
|
12
|
-
# selecting attributes that
|
13
|
-
# the given set
|
19
|
+
# selecting attributes that are meeting certain criteria and belong
|
20
|
+
# to the given attribute set.
|
14
21
|
#
|
15
|
-
# @param
|
16
|
-
#
|
17
|
-
#
|
18
|
-
# @
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
buf
|
29
|
-
end
|
22
|
+
# @param set_name [AttributeSet] set of attributes used to get attributes
|
23
|
+
# @param alter_mode [Boolean] if set then the existence
|
24
|
+
# of attribute is checked by testing if writer is defined; otherwise the reader is checked
|
25
|
+
# @param process_all [Boolean] if set then all the attributes from the attribute set are selected,
|
26
|
+
# not just attributes that has changed
|
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 (see also +alter_mode+)
|
29
|
+
# @return [AttributeSet] set of attributes (attribute name => previous_value)
|
30
|
+
def attributes_to_filter(set_name, alter_mode = true, process_all = false, no_presence_check = false)
|
31
|
+
if process_all
|
32
|
+
atf = attribute_set(set_name)
|
33
|
+
needs_write = alter_mode ? "=" : ""
|
34
|
+
no_presence_check ? atf : atf.select{ |atr| respond_to?("#{atr}#{needs_write}") }
|
30
35
|
else
|
31
|
-
|
32
|
-
@__attr_op_apply[1][set_name] ||= changed_attributes.select do |a,pv|
|
33
|
-
attribute_set(set_name).include?(a)
|
34
|
-
end
|
36
|
+
attribute_set(set_name) & changed_attributes.keys
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
40
|
+
# @overload filter_attrs_from_set(set_name, *flags, *args, &block)
|
41
|
+
#
|
42
|
+
# @note Combining the flags +process_all+ and +no_presence_check+ may raise
|
43
|
+
# exception if some attribute from the given set doesn't exist
|
44
|
+
#
|
45
|
+
# This generic method writes the result of execution of the passed block
|
46
|
+
# to each attribute that belongs to the given set of attributes.
|
47
|
+
# It's major purpose is to create filtering methods.
|
48
|
+
#
|
49
|
+
# Only the
|
50
|
+
# {http://rubydoc.info/gems/activemodel/ActiveModel/Dirty#changed_attributes-instance_method changed attributes}
|
51
|
+
# are selected, unless the +process_all+ flag is
|
52
|
+
# given. If that flag is given then presence of each attribute is verified,
|
53
|
+
# unless the +no_presence_check+ flag is also set. The presence is tested
|
54
|
+
# by checking whether the setter method exists; if it doesn't then the attribute
|
55
|
+
# is excluded from processing. Attributes with empty or unset values are ignored
|
56
|
+
# too (but see the flag called +process_blank+).
|
57
|
+
#
|
58
|
+
# The result of the given block is used to set a new values for processed attributes.
|
59
|
+
#
|
60
|
+
# @param set_name [Symbol] name of the attribute set
|
61
|
+
# @param args [Array] optional additional arguments that will be passed to the block
|
62
|
+
# @param flags [Array<Symbol>] optional additional flags controlling the processing of attributes:
|
63
|
+
# * +:process_blank+ – tells to also process attributes that are blank (empty or +nil+)
|
64
|
+
# * +:process_all+ - tells to process all attributes, not just the ones that has changed
|
65
|
+
# * +:no_presence_check+ – tells not to check for existence of each processed attribute when processing
|
66
|
+
# all attributes; increases performance but you must care about putting into set only the existing attributes
|
67
|
+
# @yield [attribute_value, set_name, attribute_name, *args] block that will be called for each attribute
|
68
|
+
# @yieldparam attribute_value [Object] current attribute value that should be altered
|
69
|
+
# @yieldparam set_name [Symbol] a name of the processed attribute set
|
70
|
+
# @yieldparam attribute_name [Object] a name of currently processed attribute
|
71
|
+
# @yieldparam args [Array] optional arguments passed to the method
|
72
|
+
# @yieldreturn [Object] the result of calling the block
|
73
|
+
# @return [void]
|
74
|
+
#
|
75
|
+
# @example
|
76
|
+
# class User < ActiveRecord::Base
|
77
|
+
#
|
78
|
+
# attributes_that should_be_downcased: [ :username, :email ]
|
79
|
+
# before_filter :downcase_names
|
80
|
+
#
|
81
|
+
# def downcase_names
|
82
|
+
# filter_attributes_that :should_be_downcased do |atr|
|
83
|
+
# atr.mb_chars.downcase.to_s
|
84
|
+
# end
|
63
85
|
# end
|
86
|
+
#
|
64
87
|
# end
|
65
|
-
#
|
66
|
-
# end
|
67
88
|
def filter_attrs_from_set(set_name, *args, &block)
|
68
|
-
|
69
|
-
pb = flags[:process_blank]
|
70
|
-
pu = flags[:process_unchanged]
|
71
|
-
attributes_to_filter(set_name, pu).each do |a, pv|
|
72
|
-
v = self[a]
|
73
|
-
pv = v if pu
|
74
|
-
self[a] = yield(v, pv, set_name, a, *args) if (pb || v.present?)
|
75
|
-
end
|
89
|
+
operate_on_attrs_from_set(set_name, true, *args, &block)
|
76
90
|
end
|
77
91
|
alias_method :attribute_filter_for_set, :filter_attrs_from_set
|
78
92
|
alias_method :filter_attributes_which, :filter_attrs_from_set
|
@@ -84,62 +98,106 @@ module ActiveModel
|
|
84
98
|
alias_method :alter_attributes_that_are, :filter_attrs_from_set
|
85
99
|
alias_method :alter_attributes_which_are, :filter_attrs_from_set
|
86
100
|
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
# altering attribute values is calling altering method on attributes
|
92
|
-
# (usually with bang methods).
|
101
|
+
# @overload for_each_attr_from_set(set_name, *flags, *args, &block)
|
102
|
+
#
|
103
|
+
# @note If you're looking for a method that is designed to alter attributes
|
104
|
+
# by rewritting their contents see {#filter_attrs_from_set}
|
93
105
|
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
# @yield [attribute_value, previous_value, set_name, attribute_name, *args] block that will be called for each attribute
|
97
|
-
# @yieldparam attribute_value [Object] current attribute value that should be altered
|
98
|
-
# @yieldparam previous_value [Object] the value of an attribute before change was made by ORM
|
99
|
-
# @yieldparam set_name [Symbol] a name of the processed attribute set
|
100
|
-
# @yieldparam attribute_name [Object] a name of currently processed attribute
|
101
|
-
# @yieldparam args [Array] optional arguments passed to the method
|
102
|
-
# @yieldreturn [void]
|
103
|
-
# @return [void]
|
106
|
+
# @note Combining the flags +process_all+ and +no_presence_check+ may raise
|
107
|
+
# exception if some attribute from the given set doesn't exist
|
104
108
|
#
|
105
|
-
#
|
106
|
-
#
|
109
|
+
# This generic method calls the passed block for each attribute
|
110
|
+
# that belongs to the given set of attributes.
|
111
|
+
# It's major purpose is to iterate through attributes and/or work directly with their values.
|
112
|
+
#
|
113
|
+
# Only the
|
114
|
+
# {http://rubydoc.info/gems/activemodel/ActiveModel/Dirty#changed_attributes-instance_method changed attributes}
|
115
|
+
# are selected, unless the +process_all+ flag is
|
116
|
+
# given. If that flag is given then presence of each attribute is verified,
|
117
|
+
# unless the +no_presence_check+ flag is also set. The presence is tested
|
118
|
+
# by checking whether the method of the same name as the attribute exists (the getter);
|
119
|
+
# if it doesn't then the attribute is excluded from processing. Attributes with
|
120
|
+
# empty or unset values are ignored too (but see the flag called +process_blank+).
|
121
|
+
#
|
122
|
+
# The result of the given block is not used to set the processed attribute.
|
123
|
+
# The only way to alter attribute values using this method is to use bang
|
124
|
+
# method in a block or explicitly assign new, calculated value to the attribute
|
125
|
+
# using its name (also passed to a block as one of arguments).
|
107
126
|
#
|
108
|
-
#
|
109
|
-
#
|
127
|
+
# @param set_name [Symbol] name of the attribute set
|
128
|
+
# @param args [Array] optional additional arguments that will be passed to a block
|
129
|
+
# @param flags [Array<Symbol>] optional additional flags controlling the processing of attributes:
|
130
|
+
# * +:process_blank+ – tells to also process attributes that are blank (empty or +nil+)
|
131
|
+
# * +:process_all+ - tells to process all attributes, not just the ones that has changed
|
132
|
+
# * +:no_presence_check+ – tells not to check for existence of each processed attribute when processing
|
133
|
+
# all attributes; increases performance but you must care about putting into set only the existing attributes
|
134
|
+
# @yield [attribute_value, set_name, attribute_name, *args] block that will be called for each attribute
|
135
|
+
# @yieldparam attribute_value [Object] current attribute value that should be altered
|
136
|
+
# @yieldparam set_name [Symbol] a name of the processed attribute set
|
137
|
+
# @yieldparam attribute_name [Object] a name of currently processed attribute
|
138
|
+
# @yieldparam args [Array] optional arguments passed to the method
|
139
|
+
# @yieldreturn [Object] the result of calling the block
|
140
|
+
# @return [void]
|
141
|
+
#
|
142
|
+
# @example
|
143
|
+
# class User < ActiveRecord::Base
|
110
144
|
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
145
|
+
# attributes_that should_be_stripped: [ :username, :email ]
|
146
|
+
# before_filter :strip_names
|
147
|
+
#
|
148
|
+
# def strip_names
|
149
|
+
# for_attributes_that :should_be_stripped do |atr|
|
150
|
+
# atr.strip!
|
151
|
+
# end
|
114
152
|
# end
|
153
|
+
#
|
115
154
|
# end
|
116
|
-
|
117
|
-
|
118
|
-
def call_attrs_from_set(set_name, *args, &block)
|
119
|
-
flags = attr_filter_process_flags(args)
|
120
|
-
pb = flags[:process_blank]
|
121
|
-
pu = flags[:process_unchanged]
|
122
|
-
attributes_to_filter(set_name, pu).each do |a, pv|
|
123
|
-
v = self[a]
|
124
|
-
pv = v if pu
|
125
|
-
yield(v, pv, set_name, a, *args) if (pb || v.present?)
|
126
|
-
end
|
155
|
+
def for_each_attr_from_set(set_name, *args, &block)
|
156
|
+
operate_on_attrs_from_set(set_name, false, *args, &block)
|
127
157
|
end
|
128
|
-
alias_method :attribute_call_for_set, :
|
129
|
-
alias_method :
|
130
|
-
alias_method :
|
131
|
-
alias_method :
|
132
|
-
alias_method :
|
158
|
+
alias_method :attribute_call_for_set, :for_each_attr_from_set
|
159
|
+
alias_method :call_attrs_from_set, :for_each_attr_from_set
|
160
|
+
alias_method :for_attributes_which, :for_each_attr_from_set
|
161
|
+
alias_method :for_attributes_that, :for_each_attr_from_set
|
162
|
+
alias_method :for_attributes_that_are, :for_each_attr_from_set
|
163
|
+
alias_method :for_attributes_which_are, :for_each_attr_from_set
|
133
164
|
|
134
165
|
private
|
135
166
|
|
136
167
|
def attr_filter_process_flags(args)
|
137
|
-
flags =
|
168
|
+
flags = ActiveModel::AttributeFilters::PROCESSING_FLAGS.dup
|
138
169
|
while flags.key?(a=args[0]) do
|
139
170
|
flags[a] = !!args.shift
|
140
171
|
end
|
141
172
|
flags
|
142
173
|
end
|
143
174
|
|
144
|
-
|
145
|
-
|
175
|
+
def operate_on_attrs_from_set(set_name, alter_mode, *args, &block)
|
176
|
+
flags = attr_filter_process_flags(args)
|
177
|
+
process_all = flags[:process_all]
|
178
|
+
process_blank = flags[:process_blank]
|
179
|
+
no_presence_check = flags[:no_presence_check]
|
180
|
+
attrs_to_process = attributes_to_filter(set_name, alter_mode, process_all, no_presence_check)
|
181
|
+
if alter_mode
|
182
|
+
if process_blank
|
183
|
+
attrs_to_process.each { |atr| self[atr] = yield(self[atr], set_name, atr, *args) }
|
184
|
+
else
|
185
|
+
attrs_to_process.each do |atr|
|
186
|
+
v = self[atr]
|
187
|
+
self[atr] = yield(v, set_name, atr, *args) if v.present?
|
188
|
+
end
|
189
|
+
end
|
190
|
+
else
|
191
|
+
if process_blank
|
192
|
+
attrs_to_process.each { |atr| yield(self[atr], set_name, atr, *args) }
|
193
|
+
else
|
194
|
+
attrs_to_process.each do |atr|
|
195
|
+
v = self[atr]
|
196
|
+
yield(v, set_name, atr, *args) if v.present?
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
end # module AttributeFilters
|
203
|
+
end # module ActiveModel
|
@@ -8,39 +8,42 @@
|
|
8
8
|
|
9
9
|
require 'attribute-filters'
|
10
10
|
|
11
|
-
module
|
12
|
-
|
11
|
+
module ActiveModel
|
12
|
+
module AttributeFilters
|
13
|
+
require 'rails'
|
14
|
+
|
15
|
+
# This class is a glue that allows us to integrate with Rails.
|
16
|
+
class Railtie < ::Rails::Railtie
|
17
|
+
# Alters ActiveModel::AttributeMethods.inluded method so
|
18
|
+
# when that module is included the ActiveModel::AttributeFilters
|
19
|
+
# module is also included.
|
20
|
+
def self.insert
|
21
|
+
require 'active_model'
|
22
|
+
if defined?(ActiveModel::AttributeMethods)
|
13
23
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
ActiveModel::AttributeMethods.class_eval do
|
24
|
-
|
25
|
-
def self.included_with_attribute_methods(base)
|
26
|
-
base.class_eval do
|
27
|
-
include ActiveModel::AttributeFilters
|
28
|
-
if method_defined?(:included_without_attribute_methods)
|
29
|
-
included_without_attribute_methods(base)
|
24
|
+
ActiveModel::AttributeMethods.class_eval do
|
25
|
+
|
26
|
+
def self.included_with_attribute_methods(base)
|
27
|
+
base.class_eval do
|
28
|
+
include ActiveModel::AttributeFilters
|
29
|
+
if method_defined?(:included_without_attribute_methods)
|
30
|
+
included_without_attribute_methods(base)
|
31
|
+
end
|
30
32
|
end
|
31
33
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end #
|
40
|
-
end #
|
41
|
-
|
34
|
+
if singleton_class.method_defined?(:included)
|
35
|
+
singleton_class.send(:alias_method_chain, :included, :attribute_methods)
|
36
|
+
end
|
37
|
+
|
38
|
+
end # ActiveModel::AttributeMethods.class_eval
|
39
|
+
|
40
|
+
end # if defined?(ActiveModel::AttributeMethods)
|
41
|
+
end # def self.insert
|
42
|
+
end # class Railtie
|
43
|
+
|
44
|
+
class Railtie
|
45
|
+
AttributeFilters::Railtie.insert
|
46
|
+
end # class Railtie
|
47
|
+
end # module AttributeFilters
|
48
|
+
end # module ActiveModel
|
42
49
|
|
43
|
-
class Railtie
|
44
|
-
AttributeFilters::Railtie.insert
|
45
|
-
end # class Railtie
|
46
|
-
end # module AttributeFilters
|
@@ -6,19 +6,21 @@
|
|
6
6
|
#
|
7
7
|
# This file contains version information.
|
8
8
|
|
9
|
-
module
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
9
|
+
module ActiveModel
|
10
|
+
module AttributeFilters
|
11
|
+
# @private
|
12
|
+
DEVELOPER = 'Paweł Wilk'
|
13
|
+
# @private
|
14
|
+
EMAIL = 'pw@gnu.org'
|
15
|
+
# @private
|
16
|
+
VERSION = '1.0.3'
|
17
|
+
# @private
|
18
|
+
NAME = 'attribute-filters'
|
19
|
+
# @private
|
20
|
+
SUMMARY = 'Concise way of filtering model attributes in Rails'
|
21
|
+
# @private
|
22
|
+
URL = 'https://rubygems.org/gems/attribute-filters/'
|
23
|
+
# @private
|
24
|
+
DESCRIPTION = 'Concise way of filtering model attributes in Rails.'
|
25
|
+
end
|
24
26
|
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.3
|
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-07-10 00:00:00.000000000 Z
|
52
52
|
dependencies:
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: railties
|
55
|
-
requirement: &
|
55
|
+
requirement: &2157667380 !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: *2157667380
|
64
64
|
- !ruby/object:Gem::Dependency
|
65
65
|
name: activemodel
|
66
|
-
requirement: &
|
66
|
+
requirement: &2157666920 !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: *2157666920
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
76
|
name: hoe-yard
|
77
|
-
requirement: &
|
77
|
+
requirement: &2157666480 !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: *2157666480
|
86
86
|
- !ruby/object:Gem::Dependency
|
87
87
|
name: rspec
|
88
|
-
requirement: &
|
88
|
+
requirement: &2157666000 !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: *2157666000
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: yard
|
99
|
-
requirement: &
|
99
|
+
requirement: &2157665520 !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: *2157665520
|
108
108
|
- !ruby/object:Gem::Dependency
|
109
109
|
name: rdoc
|
110
|
-
requirement: &
|
110
|
+
requirement: &2157665080 !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: *2157665080
|
119
119
|
- !ruby/object:Gem::Dependency
|
120
120
|
name: redcarpet
|
121
|
-
requirement: &
|
121
|
+
requirement: &2157664640 !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: *2157664640
|
130
130
|
- !ruby/object:Gem::Dependency
|
131
131
|
name: bundler
|
132
|
-
requirement: &
|
132
|
+
requirement: &2157664200 !ruby/object:Gem::Requirement
|
133
133
|
none: false
|
134
134
|
requirements:
|
135
135
|
- - ! '>='
|
@@ -137,10 +137,10 @@ dependencies:
|
|
137
137
|
version: 1.0.10
|
138
138
|
type: :development
|
139
139
|
prerelease: false
|
140
|
-
version_requirements: *
|
140
|
+
version_requirements: *2157664200
|
141
141
|
- !ruby/object:Gem::Dependency
|
142
142
|
name: hoe-bundler
|
143
|
-
requirement: &
|
143
|
+
requirement: &2157663760 !ruby/object:Gem::Requirement
|
144
144
|
none: false
|
145
145
|
requirements:
|
146
146
|
- - ! '>='
|
@@ -148,10 +148,10 @@ dependencies:
|
|
148
148
|
version: 1.1.0
|
149
149
|
type: :development
|
150
150
|
prerelease: false
|
151
|
-
version_requirements: *
|
151
|
+
version_requirements: *2157663760
|
152
152
|
- !ruby/object:Gem::Dependency
|
153
153
|
name: hoe-gemspec
|
154
|
-
requirement: &
|
154
|
+
requirement: &2157663320 !ruby/object:Gem::Requirement
|
155
155
|
none: false
|
156
156
|
requirements:
|
157
157
|
- - ! '>='
|
@@ -159,10 +159,10 @@ dependencies:
|
|
159
159
|
version: 1.0.0
|
160
160
|
type: :development
|
161
161
|
prerelease: false
|
162
|
-
version_requirements: *
|
162
|
+
version_requirements: *2157663320
|
163
163
|
- !ruby/object:Gem::Dependency
|
164
164
|
name: hoe
|
165
|
-
requirement: &
|
165
|
+
requirement: &2157662880 !ruby/object:Gem::Requirement
|
166
166
|
none: false
|
167
167
|
requirements:
|
168
168
|
- - ~>
|
@@ -170,7 +170,7 @@ dependencies:
|
|
170
170
|
version: '2.16'
|
171
171
|
type: :development
|
172
172
|
prerelease: false
|
173
|
-
version_requirements: *
|
173
|
+
version_requirements: *2157662880
|
174
174
|
description: Concise way of filtering model attributes in Rails.
|
175
175
|
email:
|
176
176
|
- pw@gnu.org
|
@@ -196,6 +196,7 @@ files:
|
|
196
196
|
- docs/TODO
|
197
197
|
- docs/USAGE
|
198
198
|
- docs/rdoc.css
|
199
|
+
- docs/yard-tpl/default/fulldoc/html/css/common.css
|
199
200
|
- init.rb
|
200
201
|
- lib/attribute-filters.rb
|
201
202
|
- lib/attribute-filters/attribute_set.rb
|
@@ -225,7 +226,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
225
226
|
version: '0'
|
226
227
|
segments:
|
227
228
|
- 0
|
228
|
-
hash:
|
229
|
+
hash: 1024401863572671819
|
229
230
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
230
231
|
none: false
|
231
232
|
requirements:
|
metadata.gz.sig
CHANGED
Binary file
|