auto_strip_attributes 1.1 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,6 +7,8 @@ It works by adding a before_validation hook to the record. No other methods are
7
7
 
8
8
  Gem has option to set empty strings to nil or to remove extra spaces inside the string.
9
9
 
10
+ [<img src="https://secure.travis-ci.org/holli/auto_strip_attributes.png" />](http://travis-ci.org/holli/auto_strip_attributes)
11
+
10
12
  ## Howto / examples
11
13
 
12
14
  Include gem in your Gemfile:
@@ -29,10 +31,74 @@ class User < ActiveRecord::Base
29
31
  end
30
32
  ```
31
33
 
34
+ # Filters
35
+ ### Default filters
36
+
37
+ By default the following filters are defined (listed in the order of processing):
38
+
39
+ - :strip (enabled by default) - removes whitespaces from the beginning and the end of string
40
+ - :nullify (enabled by default) - replaces empty strings with nil
41
+ - :squish (disabled by default) - replaces extra whitespaces (including tabs) with one space
42
+
43
+ ### Custom Filters
44
+
45
+ New version of this gem supports custom filtering methods. Custom methods can be set by calling to set_filter method
46
+ inside a block passed to AutoStripAttributes::Config.setup. set_filter method accepts either Symbol or Hash as a
47
+ parameter. If parameter is a Hash, the key should be filter name and the value is boolean whether filter is enabled by
48
+ default or not. Block should return processed value.
49
+
50
+ This is an example on how to add html tags stripping in Rails
51
+
52
+ ```ruby
53
+
54
+ E.g. inside config/initializers/auto_strip_attributes.rb
55
+
56
+ AutoStripAttributes::Config.setup do
57
+ set_filter :strip_html => false do |value|
58
+ ActionController::Base.helpers.strip_tags value
59
+ end
60
+ end
61
+
62
+
63
+ And in the model:
64
+
65
+ class User < ActiveRecord::Base
66
+ auto_strip_attributes :extra_info, :strip_html => true
67
+ end
68
+
69
+ ```
70
+
71
+ Change the order of filters is done by manipulating filters_order array. You may also enable or disable filter by
72
+ default by changing filters_enabled hash.
73
+
74
+ Example of making :strip_html filter first and enabling :squish by default
75
+
76
+ ```ruby
77
+ AutoStripAttributes::Config.setup do
78
+ filters_order.delete(:strip_html)
79
+ filters_order.insert(0, :strip_html)
80
+ filters_enabled[:squish] = true
81
+ end
82
+ ```
83
+
84
+ AutoStripAttributes::Config.setup accepts following options
85
+
86
+ - :clear => true, to clear all filters
87
+ - :defaults => true, to set three default filters mentioned above
88
+
89
+
90
+ # Versions
91
+
92
+ - 1.x : Had only basic filters
93
+ - 2.x : Includes config for extra filters (thnks to [@dadittoz](https://github.com/holli/auto_strip_attributes/issues/1))
94
+
95
+
32
96
  # Requirements
33
97
 
34
98
  Gem has been tested with ruby 1.8.7, 1.9.2 and Rails 3.x. Although it should also work with previous versions of rails.
35
99
 
100
+ [<img src="https://secure.travis-ci.org/holli/auto_strip_attributes.png" />](http://travis-ci.org/holli/auto_strip_attributes)
101
+
36
102
  http://travis-ci.org/#!/holli/auto_strip_attributes
37
103
 
38
104
  # Support
@@ -41,13 +107,13 @@ Submit suggestions or feature requests as a GitHub Issue or Pull Request. Rememb
41
107
 
42
108
  # Other approaches
43
109
 
44
- This gem works by addin before_validation hook and setting attributes with self[attribute]=stripped_value. See: https://github.com/holli/auto_strip_attributes/blob/master/lib/auto_strip_attributes.rb
110
+ This gem works by adding before_validation hook and setting attributes with self[attribute]=stripped_value. See: https://github.com/holli/auto_strip_attributes/blob/master/lib/auto_strip_attributes.rb
45
111
 
46
112
  Other approaches could include calling attribute= from before_validation. This would end up calling possible custom setters twice. Might not be desired effect (e.g. if setter does some logging).
47
113
 
48
114
  Method chaining attribute= can be also used. But then stripping would be omitted if there is some code that calls model[attribute]= directly. This could happen easily when using hashes in some places.
49
115
 
50
- ## Similar gems
116
+ ### Similar gems
51
117
 
52
118
  There are many similar gems. Most of those don't have :squish or :nullify options. Those gems
53
119
  might have some extra methods whereas this gem is kept as simple as possible. These gems have a bit
@@ -22,8 +22,11 @@ Gem::Specification.new do |s|
22
22
  s.add_runtime_dependency "activerecord", ">= 3.0"
23
23
 
24
24
  s.add_development_dependency "activerecord", ">= 3.0"
25
- s.add_development_dependency "minitest", "2.6.0"
25
+ s.add_development_dependency "minitest", "2.8.1"
26
26
  s.add_development_dependency "mocha", "~> 0.9.12"
27
- #s.add_development_dependency 'ruby-debug'
28
27
  s.add_development_dependency 'rake'
28
+
29
+ #s.add_development_dependency 'ruby-debug'
30
+ #s.add_development_dependency 'ruby-debug19'
31
+
29
32
  end
@@ -1,37 +1,80 @@
1
1
  require "auto_strip_attributes/version"
2
2
 
3
3
  module AutoStripAttributes
4
-
5
4
  def auto_strip_attributes(*attributes)
6
- options = {:nullify => true, :squish => false}
5
+ options = AutoStripAttributes::Config.filters_enabled
7
6
  if attributes.last.is_a?(Hash)
8
7
  options = options.merge(attributes.pop)
9
8
  end
10
9
 
11
10
  attributes.each do |attribute|
12
11
  before_validation do |record|
13
- #value = record.send(attribute)
12
+ #debugger
14
13
  value = record[attribute]
15
- if value.respond_to?(:strip)
16
- value_stripped = (options[:nullify] && value.blank?) ? nil : value.strip
14
+ AutoStripAttributes::Config.filters_order.each do |filter_name|
15
+ next unless options[filter_name]
16
+ value = AutoStripAttributes::Config.filters[filter_name].call value
17
+ record[attribute] = value
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
17
23
 
18
- # gsub(/\s+/, ' ') is same as in Rails#String.squish http://api.rubyonrails.org/classes/String.html#method-i-squish-21
19
- value_stripped = value_stripped.gsub(/\s+/, ' ') if options[:squish] && value_stripped.respond_to?(:gsub)
24
+ class AutoStripAttributes::Config
25
+ class << self
26
+ attr_accessor :filters
27
+ attr_accessor :filters_enabled
28
+ attr_accessor :filters_order
29
+ end
20
30
 
21
- record[attribute] = value_stripped
31
+ def self.setup(user_options=nil,&block)
32
+ options = {
33
+ :clear => false,
34
+ :defaults => true,
35
+ }
36
+ options = options.merge user_options if user_options.is_a?(Hash)
22
37
 
23
- # Alternate way would be to use send(attribute=)
24
- # But that would end up calling =-method twice, once when setting, once in before_validate
25
- # if (attr != attr_stripped)
26
- # record.send("#{attribute}=", value_stripped) # does add some overhead, attribute= will be called before each validation
27
- # end
28
- end
38
+ @filters, @filters_enabled, @filters_order = {}, {}, [] if options[:clear]
39
+
40
+ @filters ||= {}
41
+ @filters_enabled ||= {}
42
+ @filters_order ||= []
43
+
44
+ if options[:defaults]
45
+ set_filter :strip => true do |value|
46
+ value.respond_to?(:strip) ? value.strip : value
47
+ end
48
+ set_filter :nullify => true do |value|
49
+ # We check for blank? and empty? because rails uses empty? inside blank?
50
+ # e.g. MiniTest::Mock.new() only responds to .blank? but not empty?, check tests for more info
51
+ # Basically same as value.blank? ? nil : value
52
+ (value.respond_to?(:'blank?') and value.respond_to?(:'empty?') and value.blank?) ? nil : value
53
+ end
54
+ set_filter :squish => false do |value|
55
+ value.respond_to?(:gsub) ? value.gsub(/\s+/, ' ') : value
29
56
  end
30
57
  end
58
+
59
+ instance_eval &block if block_given?
31
60
  end
32
61
 
62
+ def self.set_filter(filter,&block)
63
+ if filter.is_a?(Hash) then
64
+ filter_name = filter.keys.first
65
+ filter_enabled = filter.values.first
66
+ else
67
+ filter_name = filter
68
+ filter_enabled = false
69
+ end
70
+ @filters[filter_name] = block
71
+ @filters_enabled[filter_name] = filter_enabled
72
+ # in case filter is redefined, we probably don't want to change the order
73
+ @filters_order << filter_name unless @filters_order.include? filter_name
74
+ end
33
75
  end
34
76
 
35
77
  ActiveRecord::Base.send(:extend, AutoStripAttributes) if defined? ActiveRecord
78
+ AutoStripAttributes::Config.setup
36
79
  #ActiveModel::Validations::HelperMethods.send(:include, AutoStripAttributes) if defined? ActiveRecord
37
80
 
@@ -1,4 +1,3 @@
1
1
  module AutoStripAttributes
2
- #VERSION = "0.0.1"
3
- VERSION = "1.1"
2
+ VERSION = "2.0"
4
3
  end
@@ -1,16 +1,15 @@
1
+ #to use:
2
+ #require "test/test_helper"
3
+ #bundle exec ruby test/auto_strip_attributes_test.rb -v --name /test_name/
4
+
1
5
  require 'minitest/autorun'
6
+ require 'minitest/spec'
2
7
  require "active_record"
3
8
  require "auto_strip_attributes"
4
- #require 'ruby-debug'
5
9
  require 'mocha'
10
+ #require 'ruby-debug'
6
11
 
7
12
 
8
- #require "test/test_helper"
9
- # bundle exec ruby test/auto_strip_attributes_test.rb -v --name /test_name/
10
-
11
- #class AutoStripAttributesTest < Test::Unit::TestCase
12
- #class AutoStripAttributesTest < MiniTest::Unit::TestCase
13
-
14
13
  class MockRecordParent
15
14
  include ActiveModel::Validations
16
15
  include ActiveModel::Validations::Callbacks
@@ -75,9 +74,15 @@ describe AutoStripAttributes do
75
74
  #@record.foo.must_be_same_as @stripped_str
76
75
  end
77
76
 
78
- it "should not call strip method for non strippable attributes" do
77
+ it "should not call strip or nullify method for non strippable attributes" do
79
78
  @record = MockRecordBasic.new()
80
- str_mock = MiniTest::Mock.new() # answers false to str_mock.respond_to?(:strip)
79
+
80
+ str_mock = MiniTest::Mock.new() # answers false to str_mock.respond_to?(:strip) and respond_to?(:blank)
81
+ # Mock.new is problematic in ruby 1.9 because it responds to blank? but doesn't respond to !
82
+ # rails blank? method returns !self if an object doesn't respond to :empty?
83
+ # Now we check in the validator also for :empty? so !self is never called.
84
+ # Other way could be to mock !self in here
85
+
81
86
  @record.foo = str_mock
82
87
  @record.valid?
83
88
  assert @record.foo === str_mock
@@ -103,7 +108,7 @@ describe AutoStripAttributes do
103
108
 
104
109
  describe "Attribute with squish option" do
105
110
  class MockRecordWithSqueeze < MockRecordParent #< ActiveRecord::Base
106
- #column :foo, :string
111
+ #column :foo, :st
107
112
  attr_accessor :foo
108
113
  auto_strip_attributes :foo, :squish => true
109
114
  end
@@ -166,5 +171,118 @@ describe AutoStripAttributes do
166
171
  end
167
172
  end
168
173
 
174
+ describe "Configuration tests" do
175
+ it "should have defined AutoStripAttributes::Config" do
176
+ assert AutoStripAttributes.const_defined?(:Config)
177
+ end
178
+
179
+ it "should have default filters set in right order" do
180
+ AutoStripAttributes::Config.setup :clear => true
181
+ filters_order = AutoStripAttributes::Config.filters_order
182
+ filters_order.must_equal [:strip, :nullify, :squish]
183
+ end
184
+
185
+ it "should reset filters to defaults when :clear is true" do
186
+ AutoStripAttributes::Config.setup do
187
+ set_filter :test do
188
+ 'test'
189
+ end
190
+ end
191
+ AutoStripAttributes::Config.setup :clear => true
192
+ filters_order = AutoStripAttributes::Config.filters_order
193
+ filters_order.must_equal [:strip, :nullify, :squish]
194
+ end
195
+
196
+ it "should remove all filters when :clear is true and :defaults is false" do
197
+ AutoStripAttributes::Config.setup do
198
+ set_filter :test do
199
+ 'test'
200
+ end
201
+ end
202
+ AutoStripAttributes::Config.setup :clear => true, :defaults => false
203
+ filter_order = AutoStripAttributes::Config.filters_order
204
+ filter_order.must_equal []
205
+
206
+ # returning to original state
207
+ AutoStripAttributes::Config.setup :clear => true
208
+ end
209
+
210
+ it "should correctly define and process custom filters" do
211
+ class MockRecordWithCustomFilter < MockRecordParent #< ActiveRecord::Base
212
+ attr_accessor :foo
213
+ auto_strip_attributes :foo
214
+ end
215
+
216
+ AutoStripAttributes::Config.setup do
217
+ set_filter :test => true do |value|
218
+ value.downcase
219
+ end
220
+ end
221
+
222
+ filters_block = AutoStripAttributes::Config.filters
223
+ filters_order = AutoStripAttributes::Config.filters_order
224
+ filters_enabled = AutoStripAttributes::Config.filters_enabled
225
+
226
+ filters_order.must_equal [:strip, :nullify, :squish, :test]
227
+ assert Proc === filters_block[:test]
228
+ filters_enabled[:test].must_equal true
229
+
230
+ @record = MockRecordWithCustomFilter.new
231
+ @record.foo = " FOO "
232
+ @record.valid?
233
+ @record.foo.must_equal "foo"
234
+
235
+ # returning to original state
236
+ AutoStripAttributes::Config.setup :clear => true
237
+ end
238
+
239
+ end
240
+
241
+ describe "complex usecase with custom config" do
242
+ class ComplexFirstMockRecord < MockRecordParent
243
+ #column :foo, :string
244
+ attr_accessor :foo, :bar_downcase
245
+ auto_strip_attributes :foo
246
+ auto_strip_attributes :bar_downcase, :downcase => true, :nullify => false
247
+ end
248
+
249
+ # before will not work currently: https://github.com/seattlerb/minitest/issues/50 using def setup
250
+ #before do
251
+ #end
252
+
253
+ def setup
254
+ AutoStripAttributes::Config.setup do
255
+ set_filter :downcase => false do |value|
256
+ value.downcase if value.respond_to?(:downcase)
257
+ end
258
+ end
259
+ end
260
+
261
+ def teardown
262
+ AutoStripAttributes::Config.setup :defaults => true
263
+ end
264
+
265
+ it "should not use extra filters when not in setup" do
266
+ @record = ComplexFirstMockRecord.new
267
+ @record.foo = " FOO "
268
+ @record.valid?
269
+ @record.foo.must_equal "FOO"
270
+ end
271
+
272
+ it "should use extra filters when given" do
273
+ @record = ComplexFirstMockRecord.new
274
+ @record.bar_downcase = " BAR "
275
+ @record.valid?
276
+ @record.bar_downcase.must_equal "bar"
277
+ end
278
+
279
+ it "should use extra filters when given and also respect given other configs" do
280
+ @record = ComplexFirstMockRecord.new
281
+ @record.bar_downcase = " "
282
+ @record.valid?
283
+ @record.bar_downcase.must_equal ""
284
+ end
285
+ end
286
+
169
287
 
170
288
  end
metadata CHANGED
@@ -1,107 +1,81 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: auto_strip_attributes
3
- version: !ruby/object:Gem::Version
4
- hash: 13
3
+ version: !ruby/object:Gem::Version
4
+ version: '2.0'
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 1
9
- version: "1.1"
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Olli Huotari
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-10-07 00:00:00 Z
18
- dependencies:
19
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
20
15
  name: activerecord
21
- prerelease: false
22
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &22367580 !ruby/object:Gem::Requirement
23
17
  none: false
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- hash: 7
28
- segments:
29
- - 3
30
- - 0
31
- version: "3.0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: activerecord
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *22367580
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &22367080 !ruby/object:Gem::Requirement
38
28
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 7
43
- segments:
44
- - 3
45
- - 0
46
- version: "3.0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
47
33
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: minitest
51
34
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *22367080
36
+ - !ruby/object:Gem::Dependency
37
+ name: minitest
38
+ requirement: &22416220 !ruby/object:Gem::Requirement
53
39
  none: false
54
- requirements:
55
- - - "="
56
- - !ruby/object:Gem::Version
57
- hash: 23
58
- segments:
59
- - 2
60
- - 6
61
- - 0
62
- version: 2.6.0
40
+ requirements:
41
+ - - =
42
+ - !ruby/object:Gem::Version
43
+ version: 2.8.1
63
44
  type: :development
64
- version_requirements: *id003
65
- - !ruby/object:Gem::Dependency
66
- name: mocha
67
45
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *22416220
47
+ - !ruby/object:Gem::Dependency
48
+ name: mocha
49
+ requirement: &22415760 !ruby/object:Gem::Requirement
69
50
  none: false
70
- requirements:
51
+ requirements:
71
52
  - - ~>
72
- - !ruby/object:Gem::Version
73
- hash: 35
74
- segments:
75
- - 0
76
- - 9
77
- - 12
53
+ - !ruby/object:Gem::Version
78
54
  version: 0.9.12
79
55
  type: :development
80
- version_requirements: *id004
81
- - !ruby/object:Gem::Dependency
82
- name: rake
83
56
  prerelease: false
84
- requirement: &id005 !ruby/object:Gem::Requirement
57
+ version_requirements: *22415760
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &22415380 !ruby/object:Gem::Requirement
85
61
  none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- hash: 3
90
- segments:
91
- - 0
92
- version: "0"
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
93
66
  type: :development
94
- version_requirements: *id005
95
- description: AutoStripAttributes helps to remove unnecessary whitespaces from ActiveRecord or ActiveModel attributes. It's good for removing accidental spaces from user inputs. It works by adding a before_validation hook to the record. It has option to set empty strings to nil or to remove extra spaces inside the string.
96
- email:
67
+ prerelease: false
68
+ version_requirements: *22415380
69
+ description: AutoStripAttributes helps to remove unnecessary whitespaces from ActiveRecord
70
+ or ActiveModel attributes. It's good for removing accidental spaces from user inputs.
71
+ It works by adding a before_validation hook to the record. It has option to set
72
+ empty strings to nil or to remove extra spaces inside the string.
73
+ email:
97
74
  - olli.huotari@iki.fi
98
75
  executables: []
99
-
100
76
  extensions: []
101
-
102
77
  extra_rdoc_files: []
103
-
104
- files:
78
+ files:
105
79
  - .gitignore
106
80
  - .travis.yml
107
81
  - Gemfile
@@ -115,36 +89,27 @@ files:
115
89
  - test/auto_strip_attributes_test.rb
116
90
  homepage: https://github.com/holli/auto_strip_attributes
117
91
  licenses: []
118
-
119
92
  post_install_message:
120
93
  rdoc_options: []
121
-
122
- require_paths:
94
+ require_paths:
123
95
  - lib
124
- required_ruby_version: !ruby/object:Gem::Requirement
96
+ required_ruby_version: !ruby/object:Gem::Requirement
125
97
  none: false
126
- requirements:
127
- - - ">="
128
- - !ruby/object:Gem::Version
129
- hash: 3
130
- segments:
131
- - 0
132
- version: "0"
133
- required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
103
  none: false
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- hash: 3
139
- segments:
140
- - 0
141
- version: "0"
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
142
108
  requirements: []
143
-
144
109
  rubyforge_project: auto_strip_attributes
145
110
  rubygems_version: 1.8.6
146
111
  signing_key:
147
112
  specification_version: 3
148
- summary: Removes unnecessary whitespaces in attributes. Extension to ActiveRecord or ActiveModel.
113
+ summary: Removes unnecessary whitespaces in attributes. Extension to ActiveRecord
114
+ or ActiveModel.
149
115
  test_files: []
150
-