strip_attributes 1.8.1 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -0
- data/lib/strip_attributes/version.rb +1 -1
- data/lib/strip_attributes.rb +14 -16
- data/test/strip_attributes_test.rb +97 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e52f0da3132c0ac375581d84279e40f5a118efd62880a094a46cbb99fcf38b2c
|
4
|
+
data.tar.gz: 3ffd42e6130993495406c5b32d5ba93e7b538f85b9295ac9d83f0cae9d65d132
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a76db1928d1327ca44d834384679e7a1150e773f414c344d2469b02d144f2f8f103da8cd19664810dbb0d93e7b4dd4fb3a2afed2a8558c8d61a9027d30d23d03
|
7
|
+
data.tar.gz: 4a2085e2618d2d0e661b9b2dffc8741cad0b965a8b4342cc57fedd5a48e13cb70e444feb94ee1688c67051c5d6b747e9ebad29a0486b4a01a4bba34c1f2c32fd
|
data/README.md
CHANGED
@@ -13,6 +13,8 @@ attributes are stripped of whitespace, but `:only` and `:except`
|
|
13
13
|
options can be used to limit which attributes are stripped. Both options accept
|
14
14
|
a single attribute (`only: :field`) or arrays of attributes (`except: [:field1, :field2, :field3]`).
|
15
15
|
|
16
|
+
It's also possible to skip stripping the attributes altogether per model using the `:if` and `:unless` options.
|
17
|
+
|
16
18
|
---
|
17
19
|
|
18
20
|
**How You Can Help**
|
@@ -67,6 +69,32 @@ class ConservativePokerPlayer < ActiveRecord::Base
|
|
67
69
|
end
|
68
70
|
```
|
69
71
|
|
72
|
+
### Using `if`
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
# Only records with odd ids will be stripped
|
76
|
+
class OddPokerPlayer < ActiveRecord::Base
|
77
|
+
strip_attributes if: :strip_me?
|
78
|
+
|
79
|
+
def strip_me?
|
80
|
+
id.odd?
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
### Using `unless`
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
# strip_attributes will be applied randomly
|
89
|
+
class RandomPokerPlayer < ActiveRecord::Base
|
90
|
+
strip_attributes unless: :strip_me?
|
91
|
+
|
92
|
+
def strip_me?
|
93
|
+
[true, false].sample
|
94
|
+
end
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
70
98
|
### Using `allow_empty`
|
71
99
|
|
72
100
|
```ruby
|
data/lib/strip_attributes.rb
CHANGED
@@ -2,24 +2,24 @@ require "active_model"
|
|
2
2
|
|
3
3
|
module ActiveModel::Validations::HelperMethods
|
4
4
|
# Strips whitespace from model fields and converts blank values to nil.
|
5
|
-
def strip_attributes(options =
|
5
|
+
def strip_attributes(options = {})
|
6
6
|
StripAttributes.validate_options(options)
|
7
7
|
|
8
|
-
before_validation do |record|
|
8
|
+
before_validation(options.slice(:if, :unless)) do |record|
|
9
9
|
StripAttributes.strip(record, options)
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
# <b>DEPRECATED:</b> Please use <tt>strip_attributes</tt> (non-bang method)
|
14
14
|
# instead.
|
15
|
-
def strip_attributes!(options =
|
15
|
+
def strip_attributes!(options = {})
|
16
16
|
warn "[DEPRECATION] `strip_attributes!` is deprecated. Please use `strip_attributes` (non-bang method) instead."
|
17
17
|
strip_attributes(options)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
module StripAttributes
|
22
|
-
VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex]
|
22
|
+
VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex, :if, :unless]
|
23
23
|
|
24
24
|
# Unicode invisible and whitespace characters. The POSIX character class
|
25
25
|
# [:space:] corresponds to the Unicode class Z ("separator"). We also
|
@@ -37,7 +37,7 @@ module StripAttributes
|
|
37
37
|
MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]/
|
38
38
|
MULTIBYTE_SUPPORTED = "\u0020" == " "
|
39
39
|
|
40
|
-
def self.strip(record_or_string, options =
|
40
|
+
def self.strip(record_or_string, options = {})
|
41
41
|
if record_or_string.respond_to?(:attributes)
|
42
42
|
strip_record(record_or_string, options)
|
43
43
|
else
|
@@ -45,7 +45,7 @@ module StripAttributes
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
def self.strip_record(record, options =
|
48
|
+
def self.strip_record(record, options = {})
|
49
49
|
attributes = narrow(record.attributes, options)
|
50
50
|
|
51
51
|
attributes.each do |attr, value|
|
@@ -57,13 +57,11 @@ module StripAttributes
|
|
57
57
|
record
|
58
58
|
end
|
59
59
|
|
60
|
-
def self.strip_string(value, options =
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
regex = options[:regex]
|
66
|
-
end
|
60
|
+
def self.strip_string(value, options = {})
|
61
|
+
allow_empty = options[:allow_empty]
|
62
|
+
collapse_spaces = options[:collapse_spaces]
|
63
|
+
replace_newlines = options[:replace_newlines]
|
64
|
+
regex = options[:regex]
|
67
65
|
|
68
66
|
if value.respond_to?(:strip)
|
69
67
|
value = (value.blank? && !allow_empty) ? nil : value.strip
|
@@ -97,10 +95,10 @@ module StripAttributes
|
|
97
95
|
# Necessary because Rails has removed the narrowing of attributes using :only
|
98
96
|
# and :except on Base#attributes
|
99
97
|
def self.narrow(attributes, options = {})
|
100
|
-
if except = options
|
98
|
+
if except = options[:except]
|
101
99
|
except = Array(except).collect { |attribute| attribute.to_s }
|
102
100
|
attributes.except(*except)
|
103
|
-
elsif only = options
|
101
|
+
elsif only = options[:only]
|
104
102
|
only = Array(only).collect { |attribute| attribute.to_s }
|
105
103
|
attributes.slice(*only)
|
106
104
|
else
|
@@ -109,7 +107,7 @@ module StripAttributes
|
|
109
107
|
end
|
110
108
|
|
111
109
|
def self.validate_options(options)
|
112
|
-
if keys = options
|
110
|
+
if keys = options.keys
|
113
111
|
unless (keys - VALID_OPTIONS).empty?
|
114
112
|
raise ArgumentError, "Options does not specify #{VALID_OPTIONS} (#{options.keys.inspect})"
|
115
113
|
end
|
@@ -9,6 +9,8 @@ module MockAttributes
|
|
9
9
|
base.attribute :bang
|
10
10
|
base.attribute :foz
|
11
11
|
base.attribute :fiz
|
12
|
+
base.attribute :strip_me
|
13
|
+
base.attribute :skip_me
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
@@ -72,6 +74,29 @@ class StripRegexMockRecord < Tableless
|
|
72
74
|
strip_attributes regex: /[\^\%&\*]/
|
73
75
|
end
|
74
76
|
|
77
|
+
class IfSymMockRecord < Tableless
|
78
|
+
include MockAttributes
|
79
|
+
strip_attributes if: :strip_me?
|
80
|
+
|
81
|
+
def strip_me?
|
82
|
+
strip_me
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class UnlessSymMockRecord < Tableless
|
87
|
+
include MockAttributes
|
88
|
+
strip_attributes unless: :skip_me?
|
89
|
+
|
90
|
+
def skip_me?
|
91
|
+
skip_me
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class IfProcMockRecord < Tableless
|
96
|
+
include MockAttributes
|
97
|
+
strip_attributes if: Proc.new { |record| record.strip_me }
|
98
|
+
end
|
99
|
+
|
75
100
|
class StripAttributesTest < Minitest::Test
|
76
101
|
def setup
|
77
102
|
@init_params = {
|
@@ -242,6 +267,78 @@ class StripAttributesTest < Minitest::Test
|
|
242
267
|
assert_equal "foo", record.foo
|
243
268
|
end
|
244
269
|
|
270
|
+
def test_should_strip_all_fields_if_true
|
271
|
+
record = IfSymMockRecord.new(@init_params.merge(strip_me: true))
|
272
|
+
record.valid?
|
273
|
+
assert_equal "foo", record.foo
|
274
|
+
assert_equal "bar", record.bar
|
275
|
+
assert_equal "biz", record.biz
|
276
|
+
assert_equal "foz foz", record.foz
|
277
|
+
assert_equal "fiz \n fiz", record.fiz
|
278
|
+
assert_nil record.baz
|
279
|
+
assert_nil record.bang
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_should_strip_no_fields_if_false
|
283
|
+
record = IfSymMockRecord.new(@init_params.merge(strip_me: false))
|
284
|
+
record.valid?
|
285
|
+
assert_equal "\tfoo", record.foo
|
286
|
+
assert_equal "bar \t ", record.bar
|
287
|
+
assert_equal "\tbiz ", record.biz
|
288
|
+
assert_equal " foz foz", record.foz
|
289
|
+
assert_equal "fiz \n fiz", record.fiz
|
290
|
+
assert_equal "", record.baz
|
291
|
+
assert_equal " ", record.bang
|
292
|
+
end
|
293
|
+
|
294
|
+
def test_should_strip_all_fields_unless_false
|
295
|
+
record = UnlessSymMockRecord.new(@init_params.merge(skip_me: false))
|
296
|
+
record.valid?
|
297
|
+
assert_equal "foo", record.foo
|
298
|
+
assert_equal "bar", record.bar
|
299
|
+
assert_equal "biz", record.biz
|
300
|
+
assert_equal "foz foz", record.foz
|
301
|
+
assert_equal "fiz \n fiz", record.fiz
|
302
|
+
assert_nil record.baz
|
303
|
+
assert_nil record.bang
|
304
|
+
end
|
305
|
+
|
306
|
+
def test_should_strip_no_fields_unless_true
|
307
|
+
record = UnlessSymMockRecord.new(@init_params.merge(skip_me: true))
|
308
|
+
record.valid?
|
309
|
+
assert_equal "\tfoo", record.foo
|
310
|
+
assert_equal "bar \t ", record.bar
|
311
|
+
assert_equal "\tbiz ", record.biz
|
312
|
+
assert_equal " foz foz", record.foz
|
313
|
+
assert_equal "fiz \n fiz", record.fiz
|
314
|
+
assert_equal "", record.baz
|
315
|
+
assert_equal " ", record.bang
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_should_strip_all_fields_if_true_proc
|
319
|
+
record = IfProcMockRecord.new(@init_params.merge(strip_me: true))
|
320
|
+
record.valid?
|
321
|
+
assert_equal "foo", record.foo
|
322
|
+
assert_equal "bar", record.bar
|
323
|
+
assert_equal "biz", record.biz
|
324
|
+
assert_equal "foz foz", record.foz
|
325
|
+
assert_equal "fiz \n fiz", record.fiz
|
326
|
+
assert_nil record.baz
|
327
|
+
assert_nil record.bang
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_should_strip_no_fields_if_false_proc
|
331
|
+
record = IfProcMockRecord.new(@init_params.merge(strip_me: false))
|
332
|
+
record.valid?
|
333
|
+
assert_equal "\tfoo", record.foo
|
334
|
+
assert_equal "bar \t ", record.bar
|
335
|
+
assert_equal "\tbiz ", record.biz
|
336
|
+
assert_equal " foz foz", record.foz
|
337
|
+
assert_equal "fiz \n fiz", record.fiz
|
338
|
+
assert_equal "", record.baz
|
339
|
+
assert_equal " ", record.bang
|
340
|
+
end
|
341
|
+
|
245
342
|
class ClassMethodsTest < Minitest::Test
|
246
343
|
def test_should_strip_whitespace
|
247
344
|
assert_nil StripAttributes.strip("")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strip_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan McGeary
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|