auto_strip_attributes 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +6 -0
- data/README.md +5 -4
- data/auto_strip_attributes.gemspec +1 -1
- data/lib/auto_strip_attributes.rb +18 -22
- data/lib/auto_strip_attributes/version.rb +1 -1
- data/test/auto_strip_attributes_test.rb +43 -18
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d58364e7bfb8fa25863e584c90647d420866a120
|
4
|
+
data.tar.gz: 71e36458fb5db6d524bbd9190afdad815ea61b10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ac067485bacc17e0bf9a445468a85563e135fa144f974a07416cbc80813542a16d2468094332d3b7fea1bd9b61b68131a07196e4d67092333dd7641e53216b0
|
7
|
+
data.tar.gz: c90837187b66e9e7c508ae74e4a70329d2a9a3fe1e38a34934b52db31003e3ced4291fd0f6d846d3a6cf16239364fdbc204381d0c6e550e7ee86e6bb091c3616
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
## [Master]
|
2
2
|
|
3
|
+
## [2.4]
|
4
|
+
|
5
|
+
- Using `ActiveSupport.on_load(:active_record)` instead of direct `extend`. ([#26](https://github.com/holli/auto_strip_attributes/commit/02431f07fcd880baaa352fc3e5a47d07c6d3935d))
|
6
|
+
- Possibility to pass options to custom filters. ([@nasa42#27](https://github.com/holli/auto_strip_attributes/pull/27))
|
7
|
+
- Rewritten to use keyword arguments instead of hash, and other Ruby 2 stuff
|
8
|
+
|
3
9
|
## [2.3] - 2018-02-06
|
4
10
|
|
5
11
|
- Replacing all utf8 characters in squish (thnks to [@nasa42](https://github.com/holli/auto_strip_attributes/pull/24))
|
data/README.md
CHANGED
@@ -16,7 +16,7 @@ Gem has option to set empty strings to nil or to remove extra spaces inside the
|
|
16
16
|
Include gem in your Gemfile:
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
gem "auto_strip_attributes", "~> 2.
|
19
|
+
gem "auto_strip_attributes", "~> 2.4"
|
20
20
|
```
|
21
21
|
|
22
22
|
```ruby
|
@@ -47,13 +47,14 @@ By default the following options are defined (listed in the order of processing)
|
|
47
47
|
- `:delete_whitespaces` (disabled by default) - deletes all spaces (U+0020) and tabs (U+0009).
|
48
48
|
- `:convert_non_breaking_spaces` (disabled by default) - converts non-breaking spaces (U+00A0) to normal spaces (U+0020).
|
49
49
|
- `:virtual` (disabled by default) - By default `auto_strip_attributes` doesn't work with non-persistent attributes (e.g., attributes that are created with `attr_accessor`). This is to avoid calling their custom getter/setter methods. Use this option with non-persistent attributes.
|
50
|
+
- For more filters use custom filters (more examples at https://github.com/holli/auto_strip_attributes/wiki)
|
50
51
|
|
51
52
|
### Custom Filters
|
52
53
|
|
53
54
|
Gem supports custom filtering methods. Custom methods can be set by calling to set_filter method
|
54
55
|
inside a block passed to AutoStripAttributes::Config.setup. set_filter method accepts either Symbol or Hash as a
|
55
56
|
parameter. If parameter is a Hash, the key should be filter name and the value is boolean whether filter is enabled by
|
56
|
-
default or not. Block should return processed value.
|
57
|
+
default or not. Block should return processed value. See examples of custom filters at https://github.com/holli/auto_strip_attributes/wiki
|
57
58
|
|
58
59
|
This is an example on how to add html tags stripping in Rails
|
59
60
|
|
@@ -62,7 +63,7 @@ This is an example on how to add html tags stripping in Rails
|
|
62
63
|
E.g. inside config/initializers/auto_strip_attributes.rb
|
63
64
|
|
64
65
|
AutoStripAttributes::Config.setup do
|
65
|
-
set_filter
|
66
|
+
set_filter(strip_html: false) do |value|
|
66
67
|
ActionController::Base.helpers.strip_tags value
|
67
68
|
end
|
68
69
|
end
|
@@ -71,7 +72,7 @@ end
|
|
71
72
|
And in the model:
|
72
73
|
|
73
74
|
class User < ActiveRecord::Base
|
74
|
-
auto_strip_attributes :extra_info, :
|
75
|
+
auto_strip_attributes :extra_info, strip_html: true
|
75
76
|
end
|
76
77
|
|
77
78
|
```
|
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
22
|
|
23
|
-
s.add_runtime_dependency "activerecord", ">=
|
23
|
+
s.add_runtime_dependency "activerecord", ">= 4.0"
|
24
24
|
|
25
25
|
#s.add_development_dependency "activerecord", ">= 3.0"
|
26
26
|
s.add_development_dependency "minitest", ">= 2.8.1"
|
@@ -13,15 +13,14 @@ module AutoStripAttributes
|
|
13
13
|
|
14
14
|
attributes.each do |attribute|
|
15
15
|
before_validation do |record|
|
16
|
-
#debugger
|
17
16
|
if virtual
|
18
17
|
value = record.public_send(attribute)
|
19
18
|
else
|
20
19
|
value = record[attribute]
|
21
20
|
end
|
22
21
|
AutoStripAttributes::Config.filters_order.each do |filter_name|
|
23
|
-
next
|
24
|
-
value = AutoStripAttributes::Config.filters[filter_name].call
|
22
|
+
next if !options[filter_name]
|
23
|
+
value = AutoStripAttributes::Config.filters[filter_name].call(value, options[filter_name])
|
25
24
|
if virtual
|
26
25
|
record.public_send("#{attribute}=", value)
|
27
26
|
else
|
@@ -40,37 +39,31 @@ class AutoStripAttributes::Config
|
|
40
39
|
attr_accessor :filters_order
|
41
40
|
end
|
42
41
|
|
43
|
-
def self.setup(
|
44
|
-
|
45
|
-
:clear => false,
|
46
|
-
:defaults => true,
|
47
|
-
}
|
48
|
-
options = options.merge user_options if user_options.is_a?(Hash)
|
49
|
-
|
50
|
-
@filters, @filters_enabled, @filters_order = {}, {}, [] if options[:clear]
|
42
|
+
def self.setup(clear_previous: false, defaults: true, &block)
|
43
|
+
@filters, @filters_enabled, @filters_order = {}, {}, [] if clear_previous
|
51
44
|
|
52
45
|
@filters ||= {}
|
53
46
|
@filters_enabled ||= {}
|
54
47
|
@filters_order ||= []
|
55
48
|
|
56
|
-
if
|
57
|
-
set_filter
|
49
|
+
if defaults
|
50
|
+
set_filter(convert_non_breaking_spaces: false) do |value|
|
58
51
|
value.respond_to?(:gsub) ? value.gsub("\u00A0", " ") : value
|
59
52
|
end
|
60
|
-
set_filter
|
53
|
+
set_filter(strip: true) do |value|
|
61
54
|
value.respond_to?(:strip) ? value.strip : value
|
62
55
|
end
|
63
|
-
set_filter
|
56
|
+
set_filter(nullify: true) do |value|
|
64
57
|
# We check for blank? and empty? because rails uses empty? inside blank?
|
65
58
|
# e.g. MiniTest::Mock.new() only responds to .blank? but not empty?, check tests for more info
|
66
59
|
# Basically same as value.blank? ? nil : value
|
67
60
|
(value.respond_to?(:'blank?') and value.respond_to?(:'empty?') and value.blank?) ? nil : value
|
68
61
|
end
|
69
|
-
set_filter
|
62
|
+
set_filter(squish: false) do |value|
|
70
63
|
value = value.respond_to?(:gsub) ? value.gsub(/[[:space:]]+/, ' ') : value
|
71
64
|
value.respond_to?(:strip) ? value.strip : value
|
72
65
|
end
|
73
|
-
set_filter
|
66
|
+
set_filter(delete_whitespaces: false) do |value|
|
74
67
|
value.respond_to?(:delete) ? value.delete(" \t") : value
|
75
68
|
end
|
76
69
|
end
|
@@ -78,8 +71,8 @@ class AutoStripAttributes::Config
|
|
78
71
|
instance_eval(&block) if block_given?
|
79
72
|
end
|
80
73
|
|
81
|
-
def self.set_filter(filter
|
82
|
-
if filter.is_a?(Hash)
|
74
|
+
def self.set_filter(filter, &block)
|
75
|
+
if filter.is_a?(Hash)
|
83
76
|
filter_name = filter.keys.first
|
84
77
|
filter_enabled = filter.values.first
|
85
78
|
else
|
@@ -89,10 +82,13 @@ class AutoStripAttributes::Config
|
|
89
82
|
@filters[filter_name] = block
|
90
83
|
@filters_enabled[filter_name] = filter_enabled
|
91
84
|
# in case filter is redefined, we probably don't want to change the order
|
92
|
-
@filters_order << filter_name
|
85
|
+
@filters_order << filter_name if !@filters_order.include?(filter_name)
|
93
86
|
end
|
94
87
|
end
|
95
88
|
|
96
|
-
ActiveRecord::Base.send(:extend, AutoStripAttributes) if defined? ActiveRecord
|
89
|
+
#ActiveRecord::Base.send(:extend, AutoStripAttributes) if defined? ActiveRecord
|
90
|
+
ActiveSupport.on_load(:active_record) do
|
91
|
+
extend AutoStripAttributes
|
92
|
+
end
|
93
|
+
|
97
94
|
AutoStripAttributes::Config.setup
|
98
|
-
#ActiveModel::Validations::HelperMethods.send(:include, AutoStripAttributes) if defined? ActiveRecord
|
@@ -122,7 +122,7 @@ describe AutoStripAttributes do
|
|
122
122
|
class MockRecordWithConvertNBSP < MockRecordParent
|
123
123
|
#column :foo, :string
|
124
124
|
attr_accessor :foo
|
125
|
-
auto_strip_attributes :foo, :
|
125
|
+
auto_strip_attributes :foo, convert_non_breaking_spaces: true
|
126
126
|
end
|
127
127
|
|
128
128
|
it "should delete non breaking spaces" do
|
@@ -139,7 +139,7 @@ describe AutoStripAttributes do
|
|
139
139
|
class MockRecordWithNullify < MockRecordParent
|
140
140
|
#column :foo, :string
|
141
141
|
attr_accessor :foo
|
142
|
-
auto_strip_attributes :foo, :
|
142
|
+
auto_strip_attributes :foo, nullify: false
|
143
143
|
end
|
144
144
|
|
145
145
|
it "should not set blank strings to nil" do
|
@@ -152,10 +152,9 @@ describe AutoStripAttributes do
|
|
152
152
|
|
153
153
|
describe "Attribute with squish option" do
|
154
154
|
class MockRecordWithSqueeze < MockRecordParent #< ActiveRecord::Base
|
155
|
-
#column :foo, :st
|
156
155
|
attr_accessor :foo
|
157
|
-
#
|
158
|
-
auto_strip_attributes :foo, :
|
156
|
+
# testing also that `:squish => true` implies `:strip => true`
|
157
|
+
auto_strip_attributes :foo, squish: true, strip: false
|
159
158
|
end
|
160
159
|
|
161
160
|
it "should squish string also form inside" do
|
@@ -177,7 +176,7 @@ describe AutoStripAttributes do
|
|
177
176
|
class MockRecordWithDelete < MockRecordParent
|
178
177
|
#column :foo, :string
|
179
178
|
attr_accessor :foo
|
180
|
-
auto_strip_attributes :foo, :
|
179
|
+
auto_strip_attributes :foo, delete_whitespaces: true
|
181
180
|
end
|
182
181
|
|
183
182
|
it "should delete all spaces and tabs" do
|
@@ -196,7 +195,7 @@ describe AutoStripAttributes do
|
|
196
195
|
#column :bang, :integer
|
197
196
|
attr_accessor :foo, :bar, :biz, :bang
|
198
197
|
auto_strip_attributes :foo, :bar
|
199
|
-
auto_strip_attributes :biz, {:
|
198
|
+
auto_strip_attributes :biz, {nullify: false, squish: true}
|
200
199
|
end
|
201
200
|
|
202
201
|
it "should handle everything ok" do
|
@@ -262,34 +261,34 @@ describe AutoStripAttributes do
|
|
262
261
|
end
|
263
262
|
|
264
263
|
it "should have default filters set in right order" do
|
265
|
-
AutoStripAttributes::Config.setup
|
264
|
+
AutoStripAttributes::Config.setup(clear_previous: true)
|
266
265
|
filters_order = AutoStripAttributes::Config.filters_order
|
267
266
|
filters_order.must_equal [:convert_non_breaking_spaces, :strip, :nullify, :squish, :delete_whitespaces]
|
268
267
|
end
|
269
268
|
|
270
269
|
it "should reset filters to defaults when :clear is true" do
|
271
270
|
AutoStripAttributes::Config.setup do
|
272
|
-
set_filter
|
271
|
+
set_filter(:test) do
|
273
272
|
'test'
|
274
273
|
end
|
275
274
|
end
|
276
|
-
AutoStripAttributes::Config.setup
|
275
|
+
AutoStripAttributes::Config.setup(clear_previous: true)
|
277
276
|
filters_order = AutoStripAttributes::Config.filters_order
|
278
277
|
filters_order.must_equal [:convert_non_breaking_spaces, :strip, :nullify, :squish, :delete_whitespaces]
|
279
278
|
end
|
280
279
|
|
281
280
|
it "should remove all filters when :clear is true and :defaults is false" do
|
282
281
|
AutoStripAttributes::Config.setup do
|
283
|
-
set_filter
|
282
|
+
set_filter(:test) do
|
284
283
|
'test'
|
285
284
|
end
|
286
285
|
end
|
287
|
-
AutoStripAttributes::Config.setup
|
286
|
+
AutoStripAttributes::Config.setup(clear_previous: true, defaults: false)
|
288
287
|
filter_order = AutoStripAttributes::Config.filters_order
|
289
288
|
filter_order.must_equal []
|
290
289
|
|
291
290
|
# returning to original state
|
292
|
-
AutoStripAttributes::Config.setup
|
291
|
+
AutoStripAttributes::Config.setup(clear_previous: true)
|
293
292
|
end
|
294
293
|
|
295
294
|
it "should correctly define and process custom filters" do
|
@@ -299,7 +298,7 @@ describe AutoStripAttributes do
|
|
299
298
|
end
|
300
299
|
|
301
300
|
AutoStripAttributes::Config.setup do
|
302
|
-
set_filter
|
301
|
+
set_filter(test: true) do |value|
|
303
302
|
value.downcase
|
304
303
|
end
|
305
304
|
end
|
@@ -318,17 +317,43 @@ describe AutoStripAttributes do
|
|
318
317
|
@record.foo.must_equal "foo"
|
319
318
|
|
320
319
|
# returning to original state
|
321
|
-
AutoStripAttributes::Config.setup
|
320
|
+
AutoStripAttributes::Config.setup(clear_previous: true)
|
322
321
|
end
|
323
322
|
|
324
323
|
end
|
325
324
|
|
325
|
+
describe "Using options in custom filters" do
|
326
|
+
class CustomOptionsMockRecord < MockRecordParent
|
327
|
+
attr_accessor :foo, :bar_downcase
|
328
|
+
auto_strip_attributes :foo, truncate_test: {length: 5, separator: " ", omission: "…"}
|
329
|
+
end
|
330
|
+
|
331
|
+
def setup
|
332
|
+
AutoStripAttributes::Config.setup do
|
333
|
+
set_filter(:truncate_test) do |value, options|
|
334
|
+
!value.blank? && value.respond_to?(:truncate) ? value.truncate(options[:length], omission: options[:omission]) : value
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
def teardown
|
340
|
+
AutoStripAttributes::Config.setup(defaults: true, clear_previous: true)
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should be able to truncate as asked" do
|
344
|
+
@record = CustomOptionsMockRecord.new
|
345
|
+
@record.foo = " abcdefghijklmnopqrstijklmn"
|
346
|
+
@record.valid?
|
347
|
+
@record.foo.must_equal "abcd…"
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
326
351
|
describe "complex usecase with custom config" do
|
327
352
|
class ComplexFirstMockRecord < MockRecordParent
|
328
353
|
#column :foo, :string
|
329
354
|
attr_accessor :foo, :bar_downcase
|
330
355
|
auto_strip_attributes :foo
|
331
|
-
auto_strip_attributes :bar_downcase, :
|
356
|
+
auto_strip_attributes :bar_downcase, downcase: true, nullify: false
|
332
357
|
end
|
333
358
|
|
334
359
|
# before will not work currently: https://github.com/seattlerb/minitest/issues/50 using def setup
|
@@ -337,14 +362,14 @@ describe AutoStripAttributes do
|
|
337
362
|
|
338
363
|
def setup
|
339
364
|
AutoStripAttributes::Config.setup do
|
340
|
-
set_filter
|
365
|
+
set_filter(downcase: false) do |value|
|
341
366
|
value.downcase if value.respond_to?(:downcase)
|
342
367
|
end
|
343
368
|
end
|
344
369
|
end
|
345
370
|
|
346
371
|
def teardown
|
347
|
-
AutoStripAttributes::Config.setup :
|
372
|
+
AutoStripAttributes::Config.setup(defaults: true, clear_previous: true)
|
348
373
|
end
|
349
374
|
|
350
375
|
it "should not use extra filters when not in setup" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auto_strip_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Olli Huotari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|