auto_strip_attributes 2.1.0 → 2.2.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 +3 -7
- data/CHANGELOG.md +4 -0
- data/README.md +12 -7
- data/auto_strip_attributes.gemspec +1 -0
- data/lib/auto_strip_attributes.rb +14 -2
- data/lib/auto_strip_attributes/version.rb +1 -1
- data/test/auto_strip_attributes_test.rb +30 -5
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 624c9f6db12d0350cb1a4185eafa37c992b310f1
|
4
|
+
data.tar.gz: b6ef7f53808cd252c8bdea23f83935fff4533751
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cc0bf5cdced2dba776f05e6ca9290e19f3a3e8df4822595c5163e62fa6f8840231907125b7d3f00d4e5be51e0835d2aa1baa63bb9db29964e27e8df83a7c70b
|
7
|
+
data.tar.gz: 8ad8b23e63c8451fd6c14a95f44ec26273c7a73f52984a2e1f3ef6d3d42568e94aa3ddf50bb7b872f011576b6745ac664ff6a947dd3d26f54ed4d097a4bad9a1
|
data/.travis.yml
CHANGED
@@ -2,15 +2,11 @@ language: ruby
|
|
2
2
|
sudo: false
|
3
3
|
cache: bundler
|
4
4
|
rvm:
|
5
|
-
- 2.1.9
|
6
5
|
- 2.2.5
|
7
6
|
- 2.3.1
|
7
|
+
- 2.4.0
|
8
8
|
env:
|
9
|
-
- "RAILS_VERSION=4.0"
|
10
|
-
- "RAILS_VERSION=4.1"
|
11
9
|
- "RAILS_VERSION=4.2"
|
12
10
|
- "RAILS_VERSION=5.0"
|
13
|
-
|
14
|
-
|
15
|
-
- rvm: 2.1.9
|
16
|
-
env: "RAILS_VERSION=5.0"
|
11
|
+
- "RAILS_VERSION=5.1"
|
12
|
+
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -8,13 +8,15 @@ It works by adding a before_validation hook to the record. No other methods are
|
|
8
8
|
Gem has option to set empty strings to nil or to remove extra spaces inside the string.
|
9
9
|
|
10
10
|
[<img src="https://secure.travis-ci.org/holli/auto_strip_attributes.png" />](http://travis-ci.org/holli/auto_strip_attributes)
|
11
|
+
[](https://rubygems.org/gems/auto_strip_attributes/)
|
12
|
+
[](https://rubygems.org/gems/auto_strip_attributes/)
|
11
13
|
|
12
14
|
## Howto / examples
|
13
15
|
|
14
16
|
Include gem in your Gemfile:
|
15
17
|
|
16
18
|
```ruby
|
17
|
-
gem "auto_strip_attributes", "~> 2.
|
19
|
+
gem "auto_strip_attributes", "~> 2.2"
|
18
20
|
```
|
19
21
|
|
20
22
|
```ruby
|
@@ -28,19 +30,23 @@ class User < ActiveRecord::Base
|
|
28
30
|
|
29
31
|
# Won't set to null even if string is blank. " " => ""
|
30
32
|
auto_strip_attributes :email, :nullify => false
|
33
|
+
|
34
|
+
# Use with attributes that are not mapped to a column
|
35
|
+
auto_strip_attributes :password, virtual: true
|
31
36
|
end
|
32
37
|
```
|
33
38
|
|
34
|
-
#
|
35
|
-
### Default
|
39
|
+
# Options
|
40
|
+
### Default options
|
36
41
|
|
37
|
-
By default the following
|
42
|
+
By default the following options are defined (listed in the order of processing):
|
38
43
|
|
39
|
-
- :convert_non_breaking_spaces (disabled by default) - converts non-breaking spaces to normal spaces (Unicode U+00A0)
|
40
44
|
- :strip (enabled by default) - removes whitespaces from the beginning and the end of string
|
41
45
|
- :nullify (enabled by default) - replaces empty strings with nil
|
42
46
|
- :squish (disabled by default) - replaces extra whitespaces (including tabs) with one space
|
43
47
|
- :delete_whitespaces (disabled by default) - delete all whitespaces (including tabs)
|
48
|
+
- :convert_non_breaking_spaces (disabled by default) - converts non-breaking spaces to normal spaces (Unicode U+00A0)
|
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.
|
44
50
|
|
45
51
|
### Custom Filters
|
46
52
|
|
@@ -96,7 +102,7 @@ AutoStripAttributes::Config.setup accepts following options
|
|
96
102
|
|
97
103
|
# Requirements
|
98
104
|
|
99
|
-
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
|
105
|
+
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
|
100
106
|
|
101
107
|
[<img src="https://secure.travis-ci.org/holli/auto_strip_attributes.png" />](http://travis-ci.org/holli/auto_strip_attributes)
|
102
108
|
|
@@ -128,4 +134,3 @@ different approaches. See discussion in previous chapter.
|
|
128
134
|
# Licence
|
129
135
|
|
130
136
|
Released under the MIT license (http://www.opensource.org/licenses/mit-license.php)
|
131
|
-
|
@@ -7,14 +7,26 @@ module AutoStripAttributes
|
|
7
7
|
options = options.merge(attributes.pop)
|
8
8
|
end
|
9
9
|
|
10
|
+
# option `:virtual` is needed because we want to guarantee that
|
11
|
+
# getter/setter methods for an attribute will _not_ be invoked by default
|
12
|
+
virtual = options.delete(:virtual)
|
13
|
+
|
10
14
|
attributes.each do |attribute|
|
11
15
|
before_validation do |record|
|
12
16
|
#debugger
|
13
|
-
|
17
|
+
if virtual
|
18
|
+
value = record.public_send(attribute)
|
19
|
+
else
|
20
|
+
value = record[attribute]
|
21
|
+
end
|
14
22
|
AutoStripAttributes::Config.filters_order.each do |filter_name|
|
15
23
|
next unless options[filter_name]
|
16
24
|
value = AutoStripAttributes::Config.filters[filter_name].call value
|
17
|
-
|
25
|
+
if virtual
|
26
|
+
record.public_send("#{attribute}=", value)
|
27
|
+
else
|
28
|
+
record[attribute] = value
|
29
|
+
end
|
18
30
|
end
|
19
31
|
end
|
20
32
|
end
|
@@ -9,10 +9,9 @@ require "auto_strip_attributes"
|
|
9
9
|
require 'mocha/setup'
|
10
10
|
|
11
11
|
# if you need debug, add relevant line to auto_strip_attributes.gemspec
|
12
|
-
# s.add_development_dependency '
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# require 'ruby-debug'
|
12
|
+
# s.add_development_dependency 'pry'
|
13
|
+
# and uncomment following line, and then write binding.pry somewhere
|
14
|
+
# require 'pry'
|
16
15
|
|
17
16
|
|
18
17
|
class MockRecordParent
|
@@ -27,7 +26,8 @@ class MockRecordParent
|
|
27
26
|
end
|
28
27
|
|
29
28
|
def [](key)
|
30
|
-
|
29
|
+
k = :"@#{key}"
|
30
|
+
instance_variable_defined?(k) ? instance_variable_get(k) : nil
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
@@ -230,6 +230,31 @@ describe AutoStripAttributes do
|
|
230
230
|
end
|
231
231
|
end
|
232
232
|
|
233
|
+
describe "Virtual attributes" do
|
234
|
+
class MockVirtualAttribute < MockRecordParent
|
235
|
+
undef :[]=
|
236
|
+
undef :[]
|
237
|
+
|
238
|
+
auto_strip_attributes :foo, virtual: true
|
239
|
+
|
240
|
+
def foo
|
241
|
+
@bar
|
242
|
+
end
|
243
|
+
|
244
|
+
def foo=(val)
|
245
|
+
@bar = val
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should handle everything ok" do
|
250
|
+
@record = MockVirtualAttribute.new
|
251
|
+
@record.foo = " foo "
|
252
|
+
@record.foo.must_equal " foo "
|
253
|
+
@record.valid?
|
254
|
+
@record.foo.must_equal "foo"
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
233
258
|
describe "Configuration tests" do
|
234
259
|
it "should have defined AutoStripAttributes::Config" do
|
235
260
|
assert AutoStripAttributes.const_defined?(:Config)
|
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.2.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: 2017-09-23 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.
|
111
|
+
rubygems_version: 2.6.12
|
112
112
|
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: Removes unnecessary whitespaces in attributes. Extension to ActiveRecord
|