attribute-filters 1.1.0 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/ChangeLog CHANGED
@@ -1,3 +1,48 @@
1
+ commit 0235e6184da1ca9ef4264a772032c820d76fec19
2
+ Author: Paweł Wilk <siefca@gnu.org>
3
+ Date: Sun Jul 1 00:42:03 2012 +0200
4
+
5
+ Release 1.1.2
6
+
7
+ commit dacfbe8987f3405386279884fd87f702ddc18434
8
+ Author: Paweł Wilk <siefca@gnu.org>
9
+ Date: Sat Jun 30 23:44:55 2012 +0200
10
+
11
+ Major clean-ups in attributes selecting and accessing methods (used send instead of method)
12
+
13
+ Flags parsing moved to ActiveModel::AttributeFilters::AttributeFiltersHelpers as module function;
14
+ SuperModel based test model passes specification test.
15
+
16
+ commit c34ce901a0fe2c00dbba7dfd4f900fbe4a457a18
17
+ Author: Paweł Wilk <siefca@gnu.org>
18
+ Date: Fri Jun 29 11:22:39 2012 +0200
19
+
20
+ Prepared for RSpec, added the supermodel gem
21
+
22
+ commit 91d46ba829c987a3d93f027a75cb259d7b50dff8
23
+ Author: Paweł Wilk <siefca@gnu.org>
24
+ Date: Fri Jun 29 11:22:06 2012 +0200
25
+
26
+ Removed trailing spaces
27
+
28
+ commit 959e2b53cb96e8501c3707551cf95318128c5b0b
29
+ Author: Paweł Wilk <siefca@gnu.org>
30
+ Date: Fri Jun 29 11:19:26 2012 +0200
31
+
32
+ AttributeFilters module is now loaded even if there's no full Rails stack, just the ActiveModel
33
+
34
+ commit 05098f61691ad0937d4ee97cb38bf64af760be82
35
+ Author: Paweł Wilk <siefca@gnu.org>
36
+ Date: Fri Jun 29 02:53:47 2012 +0200
37
+
38
+ Typo fixed in README
39
+
40
+ commit 04328cb9a4bdcce7d70faab8b952ebe673e6e743
41
+ Author: Paweł Wilk <siefca@gnu.org>
42
+ Date: Fri Jun 29 02:41:56 2012 +0200
43
+
44
+ Release 1.1.0
45
+
1
46
  commit 6b5aaf23fe3cb6926dac83f733544af16d3b726b
2
47
  Author: Paweł Wilk <siefca@gnu.org>
3
48
  Date: Fri Jun 29 02:13:21 2012 +0200
data/Gemfile CHANGED
@@ -12,6 +12,8 @@ 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
14
  gem "redcarpet", ">=2.1.0", :group => [:development, :test]
15
+ gem "supermodel", ">=0.1.6", :group => [:development, :test]
16
+ gem "activerecord", ">=3.0", :group => [:development, :test]
15
17
  gem "bundler", ">=1.0.10", :group => [:development, :test]
16
18
  gem "hoe-bundler", ">=1.1.0", :group => [:development, :test]
17
19
  gem "hoe-gemspec", ">=1.0.0", :group => [:development, :test]
data/Gemfile.lock CHANGED
@@ -1,26 +1,32 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- actionpack (3.2.6)
5
- activemodel (= 3.2.6)
6
- activesupport (= 3.2.6)
7
- builder (~> 3.0.0)
8
- erubis (~> 2.7.0)
9
- journey (~> 1.0.1)
10
- rack (~> 1.4.0)
11
- rack-cache (~> 1.2)
12
- rack-test (~> 0.6.1)
13
- sprockets (~> 2.1.3)
14
- activemodel (3.2.6)
15
- activesupport (= 3.2.6)
16
- builder (~> 3.0.0)
17
- activesupport (3.2.6)
18
- i18n (~> 0.6)
19
- multi_json (~> 1.0)
20
- builder (3.0.0)
4
+ abstract (1.0.0)
5
+ actionpack (3.0.15)
6
+ activemodel (= 3.0.15)
7
+ activesupport (= 3.0.15)
8
+ builder (~> 2.1.2)
9
+ erubis (~> 2.6.6)
10
+ i18n (~> 0.5.0)
11
+ rack (~> 1.2.5)
12
+ rack-mount (~> 0.6.14)
13
+ rack-test (~> 0.5.7)
14
+ tzinfo (~> 0.3.23)
15
+ activemodel (3.0.15)
16
+ activesupport (= 3.0.15)
17
+ builder (~> 2.1.2)
18
+ i18n (~> 0.5.0)
19
+ activerecord (3.0.15)
20
+ activemodel (= 3.0.15)
21
+ activesupport (= 3.0.15)
22
+ arel (~> 2.0.10)
23
+ tzinfo (~> 0.3.23)
24
+ activesupport (3.0.15)
25
+ arel (2.0.10)
26
+ builder (2.1.2)
21
27
  diff-lcs (1.1.3)
22
- erubis (2.7.0)
23
- hike (1.2.1)
28
+ erubis (2.6.6)
29
+ abstract (>= 1.0.0)
24
30
  hoe (2.16.1)
25
31
  rake (~> 0.8)
26
32
  hoe-bundler (1.1.0)
@@ -29,24 +35,19 @@ GEM
29
35
  hoe (>= 2.2.0)
30
36
  hoe-yard (0.1.2)
31
37
  yard (>= 0.2.3.1)
32
- i18n (0.6.0)
33
- journey (1.0.4)
38
+ i18n (0.5.0)
34
39
  json (1.7.3)
35
- multi_json (1.3.6)
36
- rack (1.4.1)
37
- rack-cache (1.2)
38
- rack (>= 0.4)
39
- rack-ssl (1.3.2)
40
- rack
41
- rack-test (0.6.1)
40
+ rack (1.2.5)
41
+ rack-mount (0.6.14)
42
+ rack (>= 1.0.0)
43
+ rack-test (0.5.7)
42
44
  rack (>= 1.0)
43
- railties (3.2.6)
44
- actionpack (= 3.2.6)
45
- activesupport (= 3.2.6)
46
- rack-ssl (~> 1.3.2)
45
+ railties (3.0.15)
46
+ actionpack (= 3.0.15)
47
+ activesupport (= 3.0.15)
47
48
  rake (>= 0.8.7)
48
49
  rdoc (~> 3.4)
49
- thor (>= 0.14.6, < 2.0)
50
+ thor (~> 0.14.4)
50
51
  rake (0.9.2.2)
51
52
  rdoc (3.12)
52
53
  json (~> 1.4)
@@ -59,12 +60,10 @@ GEM
59
60
  rspec-expectations (2.10.0)
60
61
  diff-lcs (~> 1.1.3)
61
62
  rspec-mocks (2.10.1)
62
- sprockets (2.1.3)
63
- hike (~> 1.2)
64
- rack (~> 1.0)
65
- tilt (~> 1.1, != 1.3.0)
66
- thor (0.15.3)
67
- tilt (1.3.3)
63
+ supermodel (0.1.6)
64
+ activemodel (~> 3.0.0)
65
+ thor (0.14.6)
66
+ tzinfo (0.3.33)
68
67
  yard (0.8.2.1)
69
68
 
70
69
  PLATFORMS
@@ -72,6 +71,7 @@ PLATFORMS
72
71
 
73
72
  DEPENDENCIES
74
73
  activemodel (~> 3.0)
74
+ activerecord (>= 3.0)
75
75
  bundler (>= 1.0.10)
76
76
  hoe (~> 2.16)
77
77
  hoe-bundler (>= 1.1.0)
@@ -81,4 +81,5 @@ DEPENDENCIES
81
81
  rdoc (>= 3.8.0)
82
82
  redcarpet (>= 2.1.0)
83
83
  rspec (>= 2.6.0)
84
+ supermodel (>= 0.1.6)
84
85
  yard (>= 0.7.2)
data/Manifest.txt CHANGED
@@ -18,11 +18,13 @@ docs/rdoc.css
18
18
  docs/yard-tpl/default/fulldoc/html/css/common.css
19
19
  init.rb
20
20
  lib/attribute-filters.rb
21
+ lib/attribute-filters/active_model_insert.rb
21
22
  lib/attribute-filters/attribute_set.rb
22
23
  lib/attribute-filters/attribute_set_query.rb
23
24
  lib/attribute-filters/common_filters.rb
24
25
  lib/attribute-filters/dsl_filters.rb
25
26
  lib/attribute-filters/dsl_sets.rb
27
+ lib/attribute-filters/helpers.rb
26
28
  lib/attribute-filters/railtie.rb
27
29
  lib/attribute-filters/version.rb
28
30
  spec/attribute-filters_spec.rb
data/README.md CHANGED
@@ -76,20 +76,20 @@ class User < ActiveRecord::Base
76
76
 
77
77
  attributes_that should_be_stripped: [ :username, :email, :real_name ]
78
78
  attributes_that should_be_downcased: [ :username, :email ]
79
- attributes_that should_be_capitalized: [ :username, :email ]
79
+ attributes_that should_be_capitalized: [ :real_name ]
80
80
 
81
81
  before_validation :strip_names
82
82
  before_validation :downcase_names
83
83
  before_validation :capitalize_names
84
84
 
85
85
  def downcase_names
86
- filter_attributes_that(:should_be_downcased) do |atr|
86
+ filter_attributes_that :should_be_downcased do |atr|
87
87
  atr.mb_chars.downcase.to_s
88
88
  end
89
89
  end
90
90
 
91
91
  def capitalize_names
92
- filter_attributes_that(:should_be_capitalized) do |atr|
92
+ filter_attributes_that :should_be_capitalized do |atr|
93
93
  atr.mb_chars.split(' ').map { |n| n.capitalize }.join(' ')
94
94
  end
95
95
  end
@@ -101,10 +101,13 @@ class User < ActiveRecord::Base
101
101
  end
102
102
  ```
103
103
 
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.
104
+ Attributes that need to be altered may be simply added to the attribute sets
105
+ that you define and then filtered with generic methods. You can use
106
+ these methods in all your models if you wish.
107
+
108
+ The last action can be performed by putting the filtering methods into
109
+ some base class that models inherit form or (better) into your own
110
+ handy module that is included in your models.
108
111
 
109
112
  Usage
110
113
  -----
@@ -184,8 +187,8 @@ Specs
184
187
 
185
188
  You can run RSpec examples both with
186
189
 
187
- * `rake spec` or just `rake`
188
- * run a test file directly, e.g. `ruby -Ilib -Ispec spec/attribute-filters_spec.rb`
190
+ * `bundle exec rake spec` or just `bundle exec rake`
191
+ * run a test file directly, e.g. `ruby -S rspec spec/attribute-filters_spec.rb -Ispec:lib`
189
192
 
190
193
  Common rake tasks
191
194
  -----------------
@@ -210,7 +213,7 @@ Copyright (c) 2012 by Paweł Wilk.
210
213
 
211
214
  attribute-filters is copyrighted software owned by Paweł Wilk (pw@gnu.org).
212
215
  You may redistribute and/or modify this software as long as you
213
- comply with either the terms of the LGPL (see {file:docs/LGPL}),
216
+ comply with either the terms of the LGPL (see {file:docs/LGPL-LICENSE}),
214
217
  or Ruby's license (see {file:docs/COPYING}).
215
218
 
216
219
  THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ require "attribute-filters"
15
15
  require 'attribute-filters/version'
16
16
  require 'hoe'
17
17
 
18
- task :default => [:test]
18
+ task :default => [:spec]
19
19
 
20
20
  desc "install by setup.rb"
21
21
  task :install do
@@ -48,6 +48,8 @@ Hoe.spec 'attribute-filters' do
48
48
  ['yard', '>= 0.7.2'] <<
49
49
  ['rdoc', '>= 3.8.0'] <<
50
50
  ['redcarpet', '>= 2.1.0'] <<
51
+ ['supermodel', '>= 0.1.6'] <<
52
+ ['activerecord', '>= 3.0'] <<
51
53
  ['bundler', '>= 1.0.10'] <<
52
54
  ['hoe-bundler', '>= 1.1.0'] <<
53
55
  ['hoe-gemspec', '>= 1.0.0']
@@ -2,16 +2,16 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "attribute-filters"
5
- s.version = "1.1.0.20120629021118"
5
+ s.version = "1.1.2.20120701004000"
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-06-29"
10
+ s.date = "2012-06-30"
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/active_model_insert.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/helpers.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"]
@@ -31,6 +31,8 @@ Gem::Specification.new do |s|
31
31
  s.add_development_dependency(%q<yard>, [">= 0.7.2"])
32
32
  s.add_development_dependency(%q<rdoc>, [">= 3.8.0"])
33
33
  s.add_development_dependency(%q<redcarpet>, [">= 2.1.0"])
34
+ s.add_development_dependency(%q<supermodel>, [">= 0.1.6"])
35
+ s.add_development_dependency(%q<activerecord>, [">= 3.0"])
34
36
  s.add_development_dependency(%q<bundler>, [">= 1.0.10"])
35
37
  s.add_development_dependency(%q<hoe-bundler>, [">= 1.1.0"])
36
38
  s.add_development_dependency(%q<hoe-gemspec>, [">= 1.0.0"])
@@ -43,6 +45,8 @@ Gem::Specification.new do |s|
43
45
  s.add_dependency(%q<yard>, [">= 0.7.2"])
44
46
  s.add_dependency(%q<rdoc>, [">= 3.8.0"])
45
47
  s.add_dependency(%q<redcarpet>, [">= 2.1.0"])
48
+ s.add_dependency(%q<supermodel>, [">= 0.1.6"])
49
+ s.add_dependency(%q<activerecord>, [">= 3.0"])
46
50
  s.add_dependency(%q<bundler>, [">= 1.0.10"])
47
51
  s.add_dependency(%q<hoe-bundler>, [">= 1.1.0"])
48
52
  s.add_dependency(%q<hoe-gemspec>, [">= 1.0.0"])
@@ -56,6 +60,8 @@ Gem::Specification.new do |s|
56
60
  s.add_dependency(%q<yard>, [">= 0.7.2"])
57
61
  s.add_dependency(%q<rdoc>, [">= 3.8.0"])
58
62
  s.add_dependency(%q<redcarpet>, [">= 2.1.0"])
63
+ s.add_dependency(%q<supermodel>, [">= 0.1.6"])
64
+ s.add_dependency(%q<activerecord>, [">= 3.0"])
59
65
  s.add_dependency(%q<bundler>, [">= 1.0.10"])
60
66
  s.add_dependency(%q<hoe-bundler>, [">= 1.1.0"])
61
67
  s.add_dependency(%q<hoe-gemspec>, [">= 1.0.0"])
data/docs/HISTORY CHANGED
@@ -1,6 +1,52 @@
1
- === 0.0.1 / 2012-06-27
1
+ === 1.1.2 / 2012-06-30
2
2
 
3
- * 1 major enhancement
3
+ * major bugfixes
4
+
5
+ * In operate_on_attrs_from_set: replaced self[attr] and method(attr) calls with send (to be ORM agnostic)
6
+ * In attributes_to_filter: inefficient respond_to? calls replaced by the attributes method call
7
+
8
+ * major enhancements
9
+
10
+ * AttributeFilters module can be now used without full Rails stack, just with Active Model loaded
11
+
12
+ * minor enhancements
13
+
14
+ * Documentation updated
15
+ * Added attribute-filters/helpers.rb containing AttributeFiltersHelpers module
16
+ * Flags parsing method attr_filter_process_flags moved to AttributeFiltersHelpers as process_flags
17
+ * Added SuperModel and ActiveRecord dependencies for testing purposes
18
+ * Added first RSpec example
19
+
20
+ === 1.1.0 / 2012-06-29
21
+
22
+ * major enhancement
23
+
24
+ * API changed; call_attrs_from_set name changed to for_each_attr_from_set, caching removed
25
+
26
+ * minor enhancements
27
+
28
+ * Optimized code for attributes filtering
29
+ * Namespace organized (AttributeFilters completely moved under ActiveModel)
30
+ * Prepared for testing with RSpec
31
+ * Added custom CSS file for YARD formatter
32
+
33
+ === 1.0.2 / 2012-06-28
34
+
35
+ * minor enhancements
36
+
37
+ * Documentation updated
38
+ * Extended arguments parsing for DSL class method attribute_set
39
+
40
+ === 1.0.1 / 2012-06-28
41
+
42
+ * minor enhancements
43
+
44
+ * Documentation updated
45
+ * Rakefile and gemspec updated
46
+
47
+ === 1.0.0 / 2012-06-28
48
+
49
+ * major enhancement
4
50
 
5
51
  * Birthday!
6
52
 
data/docs/TODO CHANGED
@@ -3,6 +3,17 @@
3
3
 
4
4
  * replace flags by some simple hash given as the second, optional argument
5
5
 
6
+ * new way of adding to sets:
7
+
8
+ filtered_attribute user: :should_be_stripped
9
+ filtered_attribute user: :should_be_downcased
10
+ altered_attribute user: :should_be_downcased
11
+ filter_attribute user: [ :should_be_downcased, :should_be_stripped ]
12
+ filter_attribute :user, :should_be_downcased, :should_be_stripped
13
+ add_to_set
14
+ add_attribute_to_set
15
+ attribute_to_set
16
+
6
17
  * add some predefined, often used filters/sets
7
18
 
8
19
  * add rspec tests
@@ -5,7 +5,12 @@ require 'attribute-filters/version'
5
5
  require 'attribute-filters/attribute_set'
6
6
  require 'attribute-filters/attribute_set_query'
7
7
 
8
+ require 'attribute-filters/helpers'
8
9
  require 'attribute-filters/dsl_sets'
9
10
  require 'attribute-filters/dsl_filters'
10
11
 
11
- require 'attribute-filters/railtie' if defined? ::Rails
12
+ if defined? ::Rails
13
+ require 'attribute-filters/railtie'
14
+ else
15
+ require 'attribute-filters/active_model_insert'
16
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: (c) 2012 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {file:LGPL-LICENSE GNU Lesser General Public License} or {file:COPYING Ruby License}.
6
+ #
7
+ # This file loads Attribute Filters goodies into ActiveModel.
8
+
9
+ require 'active_model'
10
+
11
+ module ActiveModel
12
+
13
+ if defined?(AttributeMethods)
14
+
15
+ AttributeMethods.class_eval do
16
+
17
+ # Replaces the ActiveModel::AttributeMethods.inluded method so
18
+ # when the AttributeMethods module is included then
19
+ # the AttributeFilters module is also included.
20
+ def self.included_with_attribute_methods(base)
21
+ base.class_eval do
22
+ include ActiveModel::AttributeFilters
23
+ if method_defined?(:included_without_attribute_methods)
24
+ included_without_attribute_methods(base)
25
+ end
26
+ end
27
+ end
28
+ if singleton_class.method_defined?(:included)
29
+ singleton_class.send(:alias_method, :included_without_attribute_methods, :included)
30
+ singleton_class.send(:alias_method, :included, :included_with_attribute_methods)
31
+ #singleton_class.send(:alias_method_chain, :included, :attribute_methods)
32
+ end
33
+
34
+ end # ActiveModel::AttributeMethods.class_eval
35
+
36
+ end # if defined?(AttributeMethods)
37
+
38
+ end # module ActiveModel
@@ -25,15 +25,14 @@ module ActiveModel
25
25
  # @param process_all [Boolean] if set then all the attributes from the attribute set are selected,
26
26
  # not just attributes that has changed
27
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+)
28
+ # disabled (matters only when +process_all+ is also set
29
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)
30
+ def attributes_to_filter(set_name, process_all = false, no_presence_check = false)
31
+ atf = attribute_set(set_name)
31
32
  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}") }
33
+ no_presence_check ? atf : atf & attributes.keys
35
34
  else
36
- attribute_set(set_name) & changed_attributes.keys
35
+ atf & changed_attributes.keys
37
36
  end
38
37
  end
39
38
 
@@ -50,10 +49,8 @@ module ActiveModel
50
49
  # {http://rubydoc.info/gems/activemodel/ActiveModel/Dirty#changed_attributes-instance_method changed attributes}
51
50
  # are selected, unless the +process_all+ flag is
52
51
  # 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+).
52
+ # unless the +no_presence_check+ flag is also set. Attributes with empty or unset values
53
+ # are ignored (but see the flag called +process_blank+).
57
54
  #
58
55
  # The result of the given block is used to set a new values for processed attributes.
59
56
  #
@@ -114,10 +111,8 @@ module ActiveModel
114
111
  # {http://rubydoc.info/gems/activemodel/ActiveModel/Dirty#changed_attributes-instance_method changed attributes}
115
112
  # are selected, unless the +process_all+ flag is
116
113
  # 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+).
114
+ # unless the +no_presence_check+ flag is also set. Attributes with
115
+ # empty or unset values are ignored (but see the flag called +process_blank+).
121
116
  #
122
117
  # The result of the given block is not used to set the processed attribute.
123
118
  # The only way to alter attribute values using this method is to use bang
@@ -164,35 +159,35 @@ module ActiveModel
164
159
 
165
160
  private
166
161
 
167
- def attr_filter_process_flags(args)
168
- flags = ActiveModel::AttributeFilters::PROCESSING_FLAGS.dup
169
- while flags.key?(a=args[0]) do
170
- flags[a] = !!args.shift
171
- end
172
- flags
173
- end
174
-
175
162
  def operate_on_attrs_from_set(set_name, alter_mode, *args, &block)
176
- flags = attr_filter_process_flags(args)
163
+ flags = AttributeFiltersHelpers.process_flags(args)
177
164
  process_all = flags[:process_all]
178
165
  process_blank = flags[:process_blank]
179
166
  no_presence_check = flags[:no_presence_check]
180
- attrs_to_process = attributes_to_filter(set_name, alter_mode, process_all, no_presence_check)
167
+ attrs_to_process = attributes_to_filter(set_name, process_all, no_presence_check)
181
168
  if alter_mode
182
169
  if process_blank
183
- attrs_to_process.each { |atr| self[atr] = yield(self[atr], set_name, atr, *args) }
170
+ # filtering without testing for blank
171
+ attrs_to_process.each do |atr|
172
+ send("#{atr}=", yield(send(atr), set_name, atr, *args))
173
+ end
184
174
  else
175
+ # filtering present only
185
176
  attrs_to_process.each do |atr|
186
- v = self[atr]
187
- self[atr] = yield(v, set_name, atr, *args) if v.present?
177
+ v = send(atr)
178
+ send("#{atr}=", yield(v, set_name, atr, *args)) if v.present?
188
179
  end
189
180
  end
190
181
  else
191
182
  if process_blank
192
- attrs_to_process.each { |atr| yield(self[atr], set_name, atr, *args) }
183
+ # calling without testing for blank
184
+ attrs_to_process.each do |atr|
185
+ yield(send(atr), set_name, atr, *args)
186
+ end
193
187
  else
188
+ # calling present only
194
189
  attrs_to_process.each do |atr|
195
- v = self[atr]
190
+ v = send(atr)
196
191
  yield(v, set_name, atr, *args) if v.present?
197
192
  end
198
193
  end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: (c) 2012 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {file:LGPL-LICENSE GNU Lesser General Public License} or {file:COPYING Ruby License}.
6
+ #
7
+ # This file contains some helper methods for Attribute Filters.
8
+
9
+ # @abstract This namespace is shared with ActveModel.
10
+ module ActiveModel
11
+ module AttributeFilters
12
+ module AttributeFiltersHelpers
13
+
14
+ # @private
15
+ def process_flags(args)
16
+ flags = ActiveModel::AttributeFilters::PROCESSING_FLAGS.dup
17
+ while flags.key?(a=args[0]) do
18
+ flags[a] = !!args.shift
19
+ end
20
+ flags
21
+ end
22
+ module_function :process_flags
23
+
24
+ end # module AttributeFiltersHelpers
25
+ end # module AttributeFilters
26
+ end # module ActiveModel
@@ -14,30 +14,9 @@ module ActiveModel
14
14
 
15
15
  # This class is a glue that allows us to integrate with Rails.
16
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.
17
+ # Inserts the AttributeFilters module into ActiveModel
20
18
  def self.insert
21
- require 'active_model'
22
- if defined?(ActiveModel::AttributeMethods)
23
-
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
32
- end
33
- end
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)
19
+ require 'attribute-filters/active_model_insert'
41
20
  end # def self.insert
42
21
  end # class Railtie
43
22
 
@@ -46,4 +25,3 @@ module ActiveModel
46
25
  end # class Railtie
47
26
  end # module AttributeFilters
48
27
  end # module ActiveModel
49
-
@@ -13,7 +13,7 @@ module ActiveModel
13
13
  # @private
14
14
  EMAIL = 'pw@gnu.org'
15
15
  # @private
16
- VERSION = '1.1.0'
16
+ VERSION = '1.1.2'
17
17
  # @private
18
18
  NAME = 'attribute-filters'
19
19
  # @private
@@ -1,5 +1,36 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
- class TestModel
4
- include ActiveModel::AttributeMethods
5
+ describe ActiveModel::AttributeFilters do
6
+
7
+ before do
8
+ @tm = TestModel.new
9
+ end
10
+
11
+ describe @tm do
12
+
13
+ it "is able to filter model attributes properly" do
14
+ @tm.username = " UPCASEĄĘŚĆ "
15
+ @tm.email = " Some@EXAMPLE.com "
16
+ @tm.real_name = " sir rails "
17
+ -> { @tm.save }.should_not raise_error
18
+ @tm.username.should == "upcaseąęść"
19
+ @tm.email.should == "Some@EXAMPLE.com"
20
+ @tm.real_name.should == "Sir Rails"
21
+ end
22
+
23
+ #it "is able to filter model attributes with Active Record as ORM" do
24
+ # @tm = TestModelAR.new
25
+ # @tm.username = " UPCASEĄĘŚĆ "
26
+ # @tm.email = " Some@EXAMPLE.com "
27
+ # @tm.real_name = " sir rails "
28
+ # -> { @tm.save }.should_not raise_error
29
+ # @tm.username.should == "upcaseąęść"
30
+ # @tm.email.should == "Some@EXAMPLE.com"
31
+ # @tm.real_name.should == "Sir Rails"
32
+ #end
33
+
34
+ end
35
+
5
36
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,71 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
1
3
  require 'rspec/core'
4
+
5
+ Bundler.require(:default, :test)
6
+
7
+ require 'active_support/all'
2
8
  require 'attribute-filters'
9
+
10
+ require 'supermodel'
11
+ require 'active_record'
12
+
13
+ class TestModel < SuperModel::Base
14
+
15
+ attributes_that should_be_stripped: [ :username, :email, :real_name ]
16
+ attributes_that should_be_downcased: [ :username ]
17
+ attributes_that should_be_capitalized: [ :real_name ]
18
+ attributes_that should_be_tested: [ :test_attribute ]
19
+ attributes_that does_not_exist: [ :nonexistent_attribute ]
20
+
21
+ before_save :strip_names
22
+ before_save :downcase_names
23
+ before_save :capitalize_names
24
+
25
+ def downcase_names
26
+ filter_attributes_that :should_be_downcased do |atr|
27
+ atr.mb_chars.downcase.to_s
28
+ end
29
+ end
30
+
31
+ def capitalize_names
32
+ filter_attributes_that :should_be_capitalized do |atr|
33
+ atr.mb_chars.split(' ').map { |n| n.capitalize }.join(' ')
34
+ end
35
+ end
36
+
37
+ def strip_names
38
+ for_attributes_that(:should_be_stripped) { |atr| atr.strip! }
39
+ end
40
+
41
+ end
42
+
43
+ class TestModelAR < ActiveRecord::Base
44
+
45
+ attributes_that should_be_stripped: [ :username, :email, :real_name ]
46
+ attributes_that should_be_downcased: [ :username ]
47
+ attributes_that should_be_capitalized: [ :real_name ]
48
+ attributes_that should_be_tested: [ :test_attribute ]
49
+ attributes_that does_not_exist: [ :nonexistent_attribute ]
50
+
51
+ before_save :strip_names
52
+ before_save :downcase_names
53
+ before_save :capitalize_names
54
+
55
+ def downcase_names
56
+ filter_attributes_that :should_be_downcased do |atr|
57
+ atr.mb_chars.downcase.to_s
58
+ end
59
+ end
60
+
61
+ def capitalize_names
62
+ filter_attributes_that :should_be_capitalized do |atr|
63
+ atr.mb_chars.split(' ').map { |n| n.capitalize }.join(' ')
64
+ end
65
+ end
66
+
67
+ def strip_names
68
+ for_attributes_that(:should_be_stripped) { |atr| atr.strip! }
69
+ end
70
+
71
+ end
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.1.0
4
+ version: 1.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -48,11 +48,11 @@ cert_chain:
48
48
  -----END CERTIFICATE-----
49
49
 
50
50
  '
51
- date: 2012-06-29 00:00:00.000000000 Z
51
+ date: 2012-06-30 00:00:00.000000000 Z
52
52
  dependencies:
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: railties
55
- requirement: &2154556340 !ruby/object:Gem::Requirement
55
+ requirement: &2151903580 !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: *2154556340
63
+ version_requirements: *2151903580
64
64
  - !ruby/object:Gem::Dependency
65
65
  name: activemodel
66
- requirement: &2154555860 !ruby/object:Gem::Requirement
66
+ requirement: &2151918120 !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: *2154555860
74
+ version_requirements: *2151918120
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: hoe-yard
77
- requirement: &2154555400 !ruby/object:Gem::Requirement
77
+ requirement: &2151917140 !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: *2154555400
85
+ version_requirements: *2151917140
86
86
  - !ruby/object:Gem::Dependency
87
87
  name: rspec
88
- requirement: &2154554920 !ruby/object:Gem::Requirement
88
+ requirement: &2151915240 !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: *2154554920
96
+ version_requirements: *2151915240
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: yard
99
- requirement: &2154554440 !ruby/object:Gem::Requirement
99
+ requirement: &2151913420 !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: *2154554440
107
+ version_requirements: *2151913420
108
108
  - !ruby/object:Gem::Dependency
109
109
  name: rdoc
110
- requirement: &2154554000 !ruby/object:Gem::Requirement
110
+ requirement: &2151912140 !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: *2154554000
118
+ version_requirements: *2151912140
119
119
  - !ruby/object:Gem::Dependency
120
120
  name: redcarpet
121
- requirement: &2154553560 !ruby/object:Gem::Requirement
121
+ requirement: &2151926640 !ruby/object:Gem::Requirement
122
122
  none: false
123
123
  requirements:
124
124
  - - ! '>='
@@ -126,10 +126,32 @@ dependencies:
126
126
  version: 2.1.0
127
127
  type: :development
128
128
  prerelease: false
129
- version_requirements: *2154553560
129
+ version_requirements: *2151926640
130
+ - !ruby/object:Gem::Dependency
131
+ name: supermodel
132
+ requirement: &2151925560 !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: 0.1.6
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: *2151925560
141
+ - !ruby/object:Gem::Dependency
142
+ name: activerecord
143
+ requirement: &2151922320 !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '3.0'
149
+ type: :development
150
+ prerelease: false
151
+ version_requirements: *2151922320
130
152
  - !ruby/object:Gem::Dependency
131
153
  name: bundler
132
- requirement: &2154649580 !ruby/object:Gem::Requirement
154
+ requirement: &2151921000 !ruby/object:Gem::Requirement
133
155
  none: false
134
156
  requirements:
135
157
  - - ! '>='
@@ -137,10 +159,10 @@ dependencies:
137
159
  version: 1.0.10
138
160
  type: :development
139
161
  prerelease: false
140
- version_requirements: *2154649580
162
+ version_requirements: *2151921000
141
163
  - !ruby/object:Gem::Dependency
142
164
  name: hoe-bundler
143
- requirement: &2154649120 !ruby/object:Gem::Requirement
165
+ requirement: &2151934040 !ruby/object:Gem::Requirement
144
166
  none: false
145
167
  requirements:
146
168
  - - ! '>='
@@ -148,10 +170,10 @@ dependencies:
148
170
  version: 1.1.0
149
171
  type: :development
150
172
  prerelease: false
151
- version_requirements: *2154649120
173
+ version_requirements: *2151934040
152
174
  - !ruby/object:Gem::Dependency
153
175
  name: hoe-gemspec
154
- requirement: &2154648640 !ruby/object:Gem::Requirement
176
+ requirement: &2151931380 !ruby/object:Gem::Requirement
155
177
  none: false
156
178
  requirements:
157
179
  - - ! '>='
@@ -159,10 +181,10 @@ dependencies:
159
181
  version: 1.0.0
160
182
  type: :development
161
183
  prerelease: false
162
- version_requirements: *2154648640
184
+ version_requirements: *2151931380
163
185
  - !ruby/object:Gem::Dependency
164
186
  name: hoe
165
- requirement: &2154648180 !ruby/object:Gem::Requirement
187
+ requirement: &2151929860 !ruby/object:Gem::Requirement
166
188
  none: false
167
189
  requirements:
168
190
  - - ~>
@@ -170,7 +192,7 @@ dependencies:
170
192
  version: '2.16'
171
193
  type: :development
172
194
  prerelease: false
173
- version_requirements: *2154648180
195
+ version_requirements: *2151929860
174
196
  description: Concise way of filtering model attributes in Rails.
175
197
  email:
176
198
  - pw@gnu.org
@@ -199,11 +221,13 @@ files:
199
221
  - docs/yard-tpl/default/fulldoc/html/css/common.css
200
222
  - init.rb
201
223
  - lib/attribute-filters.rb
224
+ - lib/attribute-filters/active_model_insert.rb
202
225
  - lib/attribute-filters/attribute_set.rb
203
226
  - lib/attribute-filters/attribute_set_query.rb
204
227
  - lib/attribute-filters/common_filters.rb
205
228
  - lib/attribute-filters/dsl_filters.rb
206
229
  - lib/attribute-filters/dsl_sets.rb
230
+ - lib/attribute-filters/helpers.rb
207
231
  - lib/attribute-filters/railtie.rb
208
232
  - lib/attribute-filters/version.rb
209
233
  - spec/attribute-filters_spec.rb
@@ -226,7 +250,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
226
250
  version: '0'
227
251
  segments:
228
252
  - 0
229
- hash: 4307248510658754218
253
+ hash: -2814345581129542450
230
254
  required_rubygems_version: !ruby/object:Gem::Requirement
231
255
  none: false
232
256
  requirements:
metadata.gz.sig CHANGED
Binary file