auto_strip_attributes 2.5.0 → 2.6.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 +5 -5
- data/.travis.yml +3 -4
- data/CHANGELOG.md +5 -1
- data/README.md +34 -7
- data/lib/auto_strip_attributes.rb +8 -1
- data/lib/auto_strip_attributes/version.rb +1 -1
- data/test/auto_strip_attributes_test.rb +56 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: caf66b63e636244d95515113c583385ec94ab1daa42bd79305a49b573b5e2b07
|
4
|
+
data.tar.gz: ed94dfab3e498829c13d8e017c0527ea7ce1dc1df9920b7addaeb41ef8fba3f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa25b08795e57b2aea579998f814f2c9d630ad529b0caa8e3672e784d87f4a1d6f6358b8eb31d9aef8c79af1917573f46f6e3a33c3b5d8a32bba92bb4c981015
|
7
|
+
data.tar.gz: 8d9f2a5f2cd96a8099cfb67ae68291ec93c22013693899dae525471408626ff5f741c1170e7a1c3666f0405a22915f5e8f6dad6bc3ee00c3887d1aff8cba6732
|
data/.travis.yml
CHANGED
@@ -2,13 +2,12 @@ language: ruby
|
|
2
2
|
sudo: false
|
3
3
|
cache: bundler
|
4
4
|
rvm:
|
5
|
-
- 2.2.9
|
6
|
-
- 2.3.1
|
7
|
-
- 2.4.4
|
8
5
|
- 2.5.1
|
6
|
+
- 2.6.0
|
7
|
+
- 2.7.0
|
9
8
|
env:
|
10
|
-
- "RAILS_VERSION=4.2"
|
11
9
|
- "RAILS_VERSION=5.0"
|
12
10
|
- "RAILS_VERSION=5.1"
|
13
11
|
- "RAILS_VERSION=5.2"
|
12
|
+
- "RAILS_VERSION=6.0"
|
14
13
|
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
## [Master]
|
2
2
|
|
3
|
+
## [2.6]
|
4
|
+
|
5
|
+
- Support for `array` attributes (thnks to [@sharshenov](https://github.com/holli/auto_strip_attributes/pull/29))
|
6
|
+
|
3
7
|
## [2.5]
|
4
8
|
|
5
|
-
- Support for callback options (e.g. if: -> ...) (thnks to [watsonjon](https://github.com/holli/auto_strip_attributes/pull/28)
|
9
|
+
- Support for callback options (e.g. if: -> ...) (thnks to [@watsonjon#28](https://github.com/holli/auto_strip_attributes/pull/28))
|
6
10
|
|
7
11
|
## [2.4]
|
8
12
|
|
data/README.md
CHANGED
@@ -7,18 +7,20 @@ 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
|
-
[
|
11
|
-
[](https://travis-ci.org/holli/auto_strip_attributes)
|
11
|
+
[](https://rubygems.org/gems/auto_strip_attributes/)
|
12
|
+
[](https://rubygems.org/gems/auto_strip_attributes/)
|
13
13
|
|
14
14
|
## Howto / examples
|
15
15
|
|
16
16
|
Include gem in your Gemfile:
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
gem "auto_strip_attributes", "~> 2.
|
19
|
+
gem "auto_strip_attributes", "~> 2.6"
|
20
20
|
```
|
21
21
|
|
22
|
+
Example ActiveRecord usage:
|
23
|
+
|
22
24
|
```ruby
|
23
25
|
class User < ActiveRecord::Base
|
24
26
|
|
@@ -31,11 +33,37 @@ class User < ActiveRecord::Base
|
|
31
33
|
# Won't set to null even if string is blank. " " => ""
|
32
34
|
auto_strip_attributes :email, nullify: false
|
33
35
|
|
36
|
+
# Won't set to null even if array is blank. [" "] => []
|
37
|
+
auto_strip_attributes :tags, nullify_array: false
|
38
|
+
|
34
39
|
# Use with attributes that are not mapped to a column
|
35
40
|
auto_strip_attributes :password, virtual: true
|
36
41
|
end
|
37
42
|
```
|
38
43
|
|
44
|
+
Example ActiveModel usage:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class VirtualUser
|
48
|
+
include ActiveModel::Validations
|
49
|
+
include ActiveModel::Validations::Callbacks
|
50
|
+
|
51
|
+
extend AutoStripAttributes
|
52
|
+
|
53
|
+
attr_accessor :email
|
54
|
+
|
55
|
+
# Use the `virtual` option because attributes are not mapped to a column
|
56
|
+
auto_strip_attributes :email, virtual: true
|
57
|
+
end
|
58
|
+
|
59
|
+
virtual_user = VirtualUser.new
|
60
|
+
virtual_user.email = " alice@example.com "
|
61
|
+
|
62
|
+
virtual_user.validate
|
63
|
+
virtual_user.email #=> "alice@example.com"
|
64
|
+
|
65
|
+
```
|
66
|
+
|
39
67
|
# Options
|
40
68
|
### Default options
|
41
69
|
|
@@ -43,6 +71,7 @@ By default the following options are defined (listed in the order of processing)
|
|
43
71
|
|
44
72
|
- `:strip` (enabled by default) - removes whitespaces from the beginning and the end of string. Works exactly same as `String#strip`, i.e., may not strip non-ASCII whitespaces.
|
45
73
|
- `:nullify` (enabled by default) - replaces empty strings with nil.
|
74
|
+
- `:nullify_array` (enabled by default) - replaces empty arrays with nil.
|
46
75
|
- `:squish` (disabled by default) - replaces all consecutive Unicode whitespace characters (including tabs and new lines) with single space (U+0020). Works exactly same as Rails `String#squish`
|
47
76
|
- `:delete_whitespaces` (disabled by default) - deletes all spaces (U+0020) and tabs (U+0009).
|
48
77
|
- `:convert_non_breaking_spaces` (disabled by default) - converts non-breaking spaces (U+00A0) to normal spaces (U+0020).
|
@@ -105,9 +134,7 @@ AutoStripAttributes::Config.setup accepts following options
|
|
105
134
|
|
106
135
|
Gem has been tested with newest Ruby & Rails combination and it probably works also with older versions. See test matrix at https://github.com/holli/auto_strip_attributes/blob/master/.travis.yml
|
107
136
|
|
108
|
-
[
|
109
|
-
|
110
|
-
http://travis-ci.org/#!/holli/auto_strip_attributes
|
137
|
+
[](https://travis-ci.org/holli/auto_strip_attributes)
|
111
138
|
|
112
139
|
# Support
|
113
140
|
|
@@ -20,7 +20,13 @@ module AutoStripAttributes
|
|
20
20
|
end
|
21
21
|
AutoStripAttributes::Config.filters_order.each do |filter_name|
|
22
22
|
next if !options[filter_name]
|
23
|
-
|
23
|
+
filter = lambda { |original| AutoStripAttributes::Config.filters[filter_name].call(original, options[filter_name]) }
|
24
|
+
value = if value.respond_to?(:is_a?) && value.is_a?(Array)
|
25
|
+
array = value.map { |item| filter.call(item) }.compact
|
26
|
+
options[:nullify_array] && array.empty? ? nil : array
|
27
|
+
else
|
28
|
+
filter.call(value)
|
29
|
+
end
|
24
30
|
if virtual
|
25
31
|
record.public_send("#{attribute}=", value)
|
26
32
|
else
|
@@ -59,6 +65,7 @@ class AutoStripAttributes::Config
|
|
59
65
|
# Basically same as value.blank? ? nil : value
|
60
66
|
(value.respond_to?(:'blank?') and value.respond_to?(:'empty?') and value.blank?) ? nil : value
|
61
67
|
end
|
68
|
+
set_filter(nullify_array: true) {|value| value}
|
62
69
|
set_filter(squish: false) do |value|
|
63
70
|
value = value.respond_to?(:gsub) ? value.gsub(/[[:space:]]+/, ' ') : value
|
64
71
|
value.respond_to?(:strip) ? value.strip : value
|
@@ -83,6 +83,13 @@ describe AutoStripAttributes do
|
|
83
83
|
@record.foo.must_equal "aaa"
|
84
84
|
end
|
85
85
|
|
86
|
+
it "should be ok for strings arrays" do
|
87
|
+
@record = MockRecordBasic.new()
|
88
|
+
@record.foo = [" aaa \t", " "]
|
89
|
+
@record.valid?
|
90
|
+
@record.foo.must_equal ["aaa"]
|
91
|
+
end
|
92
|
+
|
86
93
|
it "should not delete non breaking spaces" do
|
87
94
|
@record = MockRecordBasic.new()
|
88
95
|
@record.foo = " aaa \t\u00A0"
|
@@ -105,6 +112,13 @@ describe AutoStripAttributes do
|
|
105
112
|
@record.foo.must_be_nil
|
106
113
|
end
|
107
114
|
|
115
|
+
it "should set empty strings arrays to nil" do
|
116
|
+
@record = MockRecordBasic.new()
|
117
|
+
@record.foo = [" "]
|
118
|
+
@record.valid?
|
119
|
+
@record.foo.must_be_nil
|
120
|
+
end
|
121
|
+
|
108
122
|
it "should call strip method to attribute if possible" do
|
109
123
|
@record = MockRecordBasic.new()
|
110
124
|
str_mock = " strippable_str "
|
@@ -169,6 +183,29 @@ describe AutoStripAttributes do
|
|
169
183
|
@record.valid?
|
170
184
|
@record.foo.must_equal ""
|
171
185
|
end
|
186
|
+
|
187
|
+
it "should not set blank strings arrays to nil" do
|
188
|
+
@record = MockRecordWithNullify.new()
|
189
|
+
@record.foo = [" "]
|
190
|
+
@record.valid?
|
191
|
+
@record.foo.must_equal [""]
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe "Attribute with nullify_array option" do
|
196
|
+
#class MockRecordWithNullifyArray < ActiveRecord::Base
|
197
|
+
class MockRecordWithNullifyArray < MockRecordParent
|
198
|
+
#column :foo, :string
|
199
|
+
attr_accessor :foo
|
200
|
+
auto_strip_attributes :foo, nullify_array: false
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should not set blank strings array to nil" do
|
204
|
+
@record = MockRecordWithNullifyArray.new
|
205
|
+
@record.foo = [" "]
|
206
|
+
@record.valid?
|
207
|
+
@record.foo.must_equal []
|
208
|
+
end
|
172
209
|
end
|
173
210
|
|
174
211
|
describe "Attribute with squish option" do
|
@@ -212,22 +249,31 @@ describe AutoStripAttributes do
|
|
212
249
|
class MockRecordWithMultipleAttributes < MockRecordParent #< ActiveRecord::Base
|
213
250
|
#column :foo, :string
|
214
251
|
#column :bar, :string
|
215
|
-
#column :
|
216
|
-
#column :
|
217
|
-
|
218
|
-
|
219
|
-
|
252
|
+
#column :baz, :string
|
253
|
+
#column :qux, :string, array: true
|
254
|
+
#column :quux, :string, array: true
|
255
|
+
#column :quuz, :string, array: true
|
256
|
+
attr_accessor :foo, :bar, :baz, :qux, :quux, :quuz
|
257
|
+
auto_strip_attributes :foo, :bar, :qux
|
258
|
+
auto_strip_attributes :baz, :quux, {nullify: false, squish: true}
|
259
|
+
auto_strip_attributes :quuz, {nullify: true, nullify_array: false}
|
220
260
|
end
|
221
261
|
|
222
262
|
it "should handle everything ok" do
|
223
263
|
@record = MockRecordWithMultipleAttributes.new
|
224
264
|
@record.foo = " foo\tfoo"
|
225
265
|
@record.bar = " "
|
226
|
-
@record.
|
266
|
+
@record.baz = " "
|
267
|
+
@record.qux = ["\n"]
|
268
|
+
@record.quux = [" foo\tfoo", " "]
|
269
|
+
@record.quuz = [" "]
|
227
270
|
@record.valid?
|
228
271
|
@record.foo.must_equal "foo\tfoo"
|
229
272
|
@record.bar.must_be_nil
|
230
|
-
@record.
|
273
|
+
@record.baz.must_equal ""
|
274
|
+
@record.qux.must_be_nil
|
275
|
+
@record.quux.must_equal ["foo foo", ""]
|
276
|
+
@record.quuz.must_equal []
|
231
277
|
end
|
232
278
|
end
|
233
279
|
|
@@ -284,7 +330,7 @@ describe AutoStripAttributes do
|
|
284
330
|
it "should have default filters set in right order" do
|
285
331
|
AutoStripAttributes::Config.setup(clear_previous: true)
|
286
332
|
filters_order = AutoStripAttributes::Config.filters_order
|
287
|
-
filters_order.must_equal [:convert_non_breaking_spaces, :strip, :nullify, :squish, :delete_whitespaces]
|
333
|
+
filters_order.must_equal [:convert_non_breaking_spaces, :strip, :nullify, :nullify_array, :squish, :delete_whitespaces]
|
288
334
|
end
|
289
335
|
|
290
336
|
it "should reset filters to defaults when :clear is true" do
|
@@ -295,7 +341,7 @@ describe AutoStripAttributes do
|
|
295
341
|
end
|
296
342
|
AutoStripAttributes::Config.setup(clear_previous: true)
|
297
343
|
filters_order = AutoStripAttributes::Config.filters_order
|
298
|
-
filters_order.must_equal [:convert_non_breaking_spaces, :strip, :nullify, :squish, :delete_whitespaces]
|
344
|
+
filters_order.must_equal [:convert_non_breaking_spaces, :strip, :nullify, :nullify_array, :squish, :delete_whitespaces]
|
299
345
|
end
|
300
346
|
|
301
347
|
it "should remove all filters when :clear is true and :defaults is false" do
|
@@ -328,7 +374,7 @@ describe AutoStripAttributes do
|
|
328
374
|
filters_order = AutoStripAttributes::Config.filters_order
|
329
375
|
filters_enabled = AutoStripAttributes::Config.filters_enabled
|
330
376
|
|
331
|
-
filters_order.must_equal [:convert_non_breaking_spaces, :strip, :nullify, :squish, :delete_whitespaces, :test]
|
377
|
+
filters_order.must_equal [:convert_non_breaking_spaces, :strip, :nullify, :nullify_array, :squish, :delete_whitespaces, :test]
|
332
378
|
assert Proc === filters_block[:test]
|
333
379
|
filters_enabled[:test].must_equal true
|
334
380
|
|
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.6.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:
|
11
|
+
date: 2020-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
requirements: []
|
110
110
|
rubyforge_project: auto_strip_attributes
|
111
|
-
rubygems_version: 2.6
|
111
|
+
rubygems_version: 2.7.6
|
112
112
|
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: Removes unnecessary whitespaces in attributes. Extension to ActiveRecord
|