normalize_attributes 0.3.0 → 0.3.1
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/.gitignore +1 -0
- data/.rubocop.yml +6 -0
- data/.travis.yml +7 -7
- data/Gemfile +3 -1
- data/README.md +28 -8
- data/Rakefile +2 -0
- data/lib/normalize_attributes.rb +4 -2
- data/lib/normalize_attributes/{active_record.rb → callbacks.rb} +13 -10
- data/lib/normalize_attributes/version.rb +3 -1
- data/normalize_attributes.gemspec +8 -4
- data/test/normalize_attributes_test.rb +24 -2
- data/test/schema.rb +4 -2
- data/test/support/token.rb +2 -0
- data/test/support/user.rb +2 -0
- data/test/test_helper.rb +3 -1
- metadata +10 -10
- data/Gemfile.lock +0 -84
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d02d2f9fd6b4e46578d0aa42b7d88600319f141d0b344ee9099cfc1f55c2ade3
|
4
|
+
data.tar.gz: 1f2ad30cb3c68301d103cbf9f3e2f0b98b1907b9610d78c370ee124a36c948cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 442645e30d41a53d349c2a8ae4277e2170cc19ed0044bb02176553ab5b5cbfa5b5c3474643b336a58712e94ba8d3531e72998c078322866b43275eb553fea5ba
|
7
|
+
data.tar.gz: 7c3910ebb292d98268509bf04bd50880cbb1688d33e2e5f464de3a2379d12917992d93b0692a302ce9db08175aec09b1d3a5a136218138e02a989d63ce3a1c53
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
+
---
|
1
2
|
language: ruby
|
2
3
|
sudo: false
|
3
4
|
cache: bundler
|
4
5
|
notifications:
|
5
6
|
email: false
|
6
7
|
rvm:
|
7
|
-
- 2.
|
8
|
+
- 2.7.0
|
8
9
|
before_install:
|
9
|
-
- gem install bundler
|
10
|
+
- gem install bundler
|
10
11
|
before_script:
|
11
|
-
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
|
12
|
-
|
13
|
-
-
|
14
|
-
- "./cc-test-reporter before-build"
|
12
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
13
|
+
- chmod +x ./cc-test-reporter
|
14
|
+
- "./cc-test-reporter before-build"
|
15
15
|
after_script:
|
16
|
-
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
16
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
17
17
|
env:
|
18
18
|
global:
|
19
19
|
secure: U80HNhPVE+IUJaxOPxMJc0CqlQSsl23EOOsqcnuXUjrlcjvfeCnQU58rxFOUCBeKxmno4tatfZXdwBa+FvQ3PcnOXXnesMfCR0UAPdGmaA6ScSMyL+KijC1wGI0N80ccBZRaBA4quR02XTMiJt1saTrBT1cuOCsWs2gjOB4EwoA=
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -6,7 +6,9 @@
|
|
6
6
|
[](https://rubygems.org/gems/normalize_attributes)
|
7
7
|
[](https://rubygems.org/gems/normalize_attributes)
|
8
8
|
|
9
|
-
Sometimes you want to normalize data before saving it to the database like
|
9
|
+
Sometimes you want to normalize data before saving it to the database like
|
10
|
+
downcasing e-mails, removing spaces and so on. This Rails plugin allows you to
|
11
|
+
do so in a simple way.
|
10
12
|
|
11
13
|
## Usage
|
12
14
|
|
@@ -22,7 +24,8 @@ class User < ActiveRecord::Base
|
|
22
24
|
end
|
23
25
|
```
|
24
26
|
|
25
|
-
The example above will normalize your `:email` attribute on the `before_save`
|
27
|
+
The example above will normalize your `:email` attribute on the `before_save`
|
28
|
+
callback.
|
26
29
|
|
27
30
|
You can specify multiple attributes
|
28
31
|
|
@@ -46,13 +49,15 @@ normalize :name, with: :downcase do |value|
|
|
46
49
|
end
|
47
50
|
```
|
48
51
|
|
49
|
-
The `squish` method is the default normalizer for strings. All you need to is
|
52
|
+
The `squish` method is the default normalizer for strings. All you need to is
|
53
|
+
specify the attribute:
|
50
54
|
|
51
55
|
```ruby
|
52
56
|
normalize :content
|
53
57
|
```
|
54
58
|
|
55
|
-
The `compact` method is the default normalizer for arrays (when using
|
59
|
+
The `compact` method is the default normalizer for arrays (when using
|
60
|
+
`serialize` method):
|
56
61
|
|
57
62
|
```ruby
|
58
63
|
class User < ActiveRecord::Base
|
@@ -61,14 +66,29 @@ class User < ActiveRecord::Base
|
|
61
66
|
end
|
62
67
|
```
|
63
68
|
|
64
|
-
The `normalize` method is aliased as `normalize_attributes`,
|
69
|
+
The `normalize` method is aliased as `normalize_attributes`,
|
70
|
+
`normalize_attribute`, `normalize_attr`, and `normalize_attrs`.
|
65
71
|
|
66
|
-
You can normalize the attribute before type casting; this is specially useful
|
67
|
-
dates and numbers.
|
72
|
+
You can normalize the attribute before type casting; this is specially useful
|
73
|
+
for normalizing dates and numbers.
|
68
74
|
|
69
75
|
```ruby
|
70
76
|
class Product
|
71
|
-
normalize(:price, raw: true) {|v| Money.new(v).to_f}
|
77
|
+
normalize(:price, raw: true) {|v| Money.new(v).to_f }
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
You can also use it with `ActiveModel::Model` classes.
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
class UserForm
|
85
|
+
include ActiveModel::Model
|
86
|
+
include ActiveModel::Attributes
|
87
|
+
include ActiveModel::Validations::Callbacks
|
88
|
+
include NormalizeAttributes::Callbacks
|
89
|
+
|
90
|
+
attribute :username
|
91
|
+
normalize :username
|
72
92
|
end
|
73
93
|
```
|
74
94
|
|
data/Rakefile
CHANGED
data/lib/normalize_attributes.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_record"
|
2
4
|
|
3
5
|
ActiveSupport.on_load(:active_record) do
|
4
6
|
require "active_support/core_ext/string/filters"
|
5
7
|
require "normalize_attributes/version"
|
6
|
-
require "normalize_attributes/
|
8
|
+
require "normalize_attributes/callbacks"
|
7
9
|
|
8
|
-
ActiveRecord::Base.
|
10
|
+
ActiveRecord::Base.include NormalizeAttributes::Callbacks
|
9
11
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module NormalizeAttributes
|
2
4
|
def self.retrieve_value(record, attribute, raw)
|
3
5
|
before_type_cast_method = "#{attribute}_before_type_cast"
|
@@ -37,13 +39,13 @@ module NormalizeAttributes
|
|
37
39
|
end
|
38
40
|
|
39
41
|
begin
|
40
|
-
record.write_attribute
|
42
|
+
record.send(:write_attribute, attribute, value)
|
41
43
|
rescue ActiveModel::MissingAttributeError
|
42
44
|
record.public_send("#{attribute}=", value)
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
46
|
-
module
|
48
|
+
module Callbacks
|
47
49
|
def self.included(base)
|
48
50
|
base.instance_eval do
|
49
51
|
extend ClassMethods
|
@@ -66,21 +68,22 @@ module NormalizeAttributes
|
|
66
68
|
|
67
69
|
normalize_options.tap do |o|
|
68
70
|
o[attr_name] ||= []
|
69
|
-
o[attr_name] << [
|
71
|
+
o[attr_name] << [
|
72
|
+
[block, options[:with]].flatten.compact,
|
73
|
+
options.except(:with)
|
74
|
+
]
|
70
75
|
end
|
71
76
|
end
|
72
77
|
end
|
73
78
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
79
|
+
alias normalize_attributes normalize
|
80
|
+
alias normalize_attribute normalize
|
81
|
+
alias normalize_attr normalize
|
82
|
+
alias normalize_attrs normalize
|
78
83
|
end
|
79
84
|
|
80
85
|
module InstanceMethods
|
81
|
-
private
|
82
|
-
|
83
|
-
def normalize_attributes
|
86
|
+
private def normalize_attributes
|
84
87
|
return unless self.class.normalize_options
|
85
88
|
|
86
89
|
self.class.normalize_options.each do |attribute, items|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "./lib/normalize_attributes/version"
|
2
4
|
|
3
5
|
Gem::Specification.new do |s|
|
@@ -13,14 +15,16 @@ Gem::Specification.new do |s|
|
|
13
15
|
|
14
16
|
s.files = `git ls-files`.split("\n")
|
15
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
-
s.executables = `git ls-files -- bin
|
18
|
+
s.executables = `git ls-files -- bin/*`
|
19
|
+
.split("\n")
|
20
|
+
.map {|f| File.basename(f) }
|
17
21
|
s.require_paths = ["lib"]
|
18
22
|
|
19
23
|
s.add_dependency "activerecord"
|
24
|
+
s.add_development_dependency "actionpack"
|
20
25
|
s.add_development_dependency "bundler"
|
21
|
-
s.add_development_dependency "rake"
|
22
26
|
s.add_development_dependency "minitest-utils"
|
23
|
-
s.add_development_dependency "
|
24
|
-
s.add_development_dependency "actionpack"
|
27
|
+
s.add_development_dependency "rake"
|
25
28
|
s.add_development_dependency "simplecov"
|
29
|
+
s.add_development_dependency "sqlite3"
|
26
30
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test_helper"
|
2
4
|
|
3
5
|
class NormalizeAttributesTest < Minitest::Test
|
@@ -51,7 +53,7 @@ class NormalizeAttributesTest < Minitest::Test
|
|
51
53
|
end
|
52
54
|
|
53
55
|
test "combine both method names and procs as normalization methods" do
|
54
|
-
User.normalize(:email, with: :downcase
|
56
|
+
User.normalize(:email, with: :downcase, &:reverse)
|
55
57
|
user = User.create(email: "JOHN@DOE.COM")
|
56
58
|
|
57
59
|
assert_equal "moc.eod@nhoj", user.email
|
@@ -74,7 +76,10 @@ class NormalizeAttributesTest < Minitest::Test
|
|
74
76
|
|
75
77
|
test "apply default filter on strings" do
|
76
78
|
User.normalize :email
|
77
|
-
user = User.create(
|
79
|
+
user = User.create(
|
80
|
+
email: " \n\t john@doe.com \t\t\n\r\n",
|
81
|
+
username: "john"
|
82
|
+
)
|
78
83
|
|
79
84
|
assert_equal "john@doe.com", user.email
|
80
85
|
end
|
@@ -109,4 +114,21 @@ class NormalizeAttributesTest < Minitest::Test
|
|
109
114
|
|
110
115
|
assert_equal "johnny d", user.nickname
|
111
116
|
end
|
117
|
+
|
118
|
+
test "normalize ActiveModel::Model objects" do
|
119
|
+
model = Class.new do
|
120
|
+
include ActiveModel::Model
|
121
|
+
include ActiveModel::Attributes
|
122
|
+
include ActiveModel::Validations::Callbacks
|
123
|
+
include NormalizeAttributes::Callbacks
|
124
|
+
|
125
|
+
attribute :username
|
126
|
+
normalize :username
|
127
|
+
end
|
128
|
+
|
129
|
+
instance = model.new(username: " john ")
|
130
|
+
instance.valid?
|
131
|
+
|
132
|
+
assert_equal "john", instance.username
|
133
|
+
end
|
112
134
|
end
|
data/test/schema.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define(version: 0) do
|
2
4
|
create_table :users do |t|
|
3
5
|
t.string :email, :username
|
4
6
|
t.text :preferences
|
5
|
-
t.integer :age, :
|
7
|
+
t.integer :age, null: false, default: 0
|
6
8
|
end
|
7
9
|
|
8
10
|
create_table :tokens do |t|
|
data/test/support/token.rb
CHANGED
data/test/support/user.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "simplecov"
|
2
4
|
SimpleCov.start
|
3
5
|
|
@@ -13,4 +15,4 @@ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
|
13
15
|
# Load database schema
|
14
16
|
load "schema.rb"
|
15
17
|
|
16
|
-
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f }
|
18
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].sort.each {|f| require f }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: normalize_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: actionpack
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: simplecov
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -95,7 +95,7 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: sqlite3
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
@@ -116,13 +116,13 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- ".gitignore"
|
119
|
+
- ".rubocop.yml"
|
119
120
|
- ".travis.yml"
|
120
121
|
- Gemfile
|
121
|
-
- Gemfile.lock
|
122
122
|
- README.md
|
123
123
|
- Rakefile
|
124
124
|
- lib/normalize_attributes.rb
|
125
|
-
- lib/normalize_attributes/
|
125
|
+
- lib/normalize_attributes/callbacks.rb
|
126
126
|
- lib/normalize_attributes/version.rb
|
127
127
|
- normalize_attributes.gemspec
|
128
128
|
- test/normalize_attributes_test.rb
|
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
149
|
- !ruby/object:Gem::Version
|
150
150
|
version: '0'
|
151
151
|
requirements: []
|
152
|
-
rubygems_version: 3.
|
152
|
+
rubygems_version: 3.1.2
|
153
153
|
signing_key:
|
154
154
|
specification_version: 4
|
155
155
|
summary: Normalize ActiveRecord attributes
|
data/Gemfile.lock
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
normalize_attributes (0.3.0)
|
5
|
-
activerecord
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
actionpack (5.2.2)
|
11
|
-
actionview (= 5.2.2)
|
12
|
-
activesupport (= 5.2.2)
|
13
|
-
rack (~> 2.0)
|
14
|
-
rack-test (>= 0.6.3)
|
15
|
-
rails-dom-testing (~> 2.0)
|
16
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
17
|
-
actionview (5.2.2)
|
18
|
-
activesupport (= 5.2.2)
|
19
|
-
builder (~> 3.1)
|
20
|
-
erubi (~> 1.4)
|
21
|
-
rails-dom-testing (~> 2.0)
|
22
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
23
|
-
activemodel (5.2.2)
|
24
|
-
activesupport (= 5.2.2)
|
25
|
-
activerecord (5.2.2)
|
26
|
-
activemodel (= 5.2.2)
|
27
|
-
activesupport (= 5.2.2)
|
28
|
-
arel (>= 9.0)
|
29
|
-
activesupport (5.2.2)
|
30
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
|
-
i18n (>= 0.7, < 2)
|
32
|
-
minitest (~> 5.1)
|
33
|
-
tzinfo (~> 1.1)
|
34
|
-
arel (9.0.0)
|
35
|
-
builder (3.2.3)
|
36
|
-
concurrent-ruby (1.1.4)
|
37
|
-
crass (1.0.4)
|
38
|
-
docile (1.1.5)
|
39
|
-
erubi (1.8.0)
|
40
|
-
i18n (1.3.0)
|
41
|
-
concurrent-ruby (~> 1.0)
|
42
|
-
json (2.1.0)
|
43
|
-
loofah (2.2.3)
|
44
|
-
crass (~> 1.0.2)
|
45
|
-
nokogiri (>= 1.5.9)
|
46
|
-
mini_portile2 (2.4.0)
|
47
|
-
minitest (5.11.3)
|
48
|
-
minitest-utils (0.4.4)
|
49
|
-
minitest
|
50
|
-
nokogiri (1.9.1)
|
51
|
-
mini_portile2 (~> 2.4.0)
|
52
|
-
rack (2.0.6)
|
53
|
-
rack-test (1.1.0)
|
54
|
-
rack (>= 1.0, < 3)
|
55
|
-
rails-dom-testing (2.0.3)
|
56
|
-
activesupport (>= 4.2.0)
|
57
|
-
nokogiri (>= 1.6)
|
58
|
-
rails-html-sanitizer (1.0.4)
|
59
|
-
loofah (~> 2.2, >= 2.2.2)
|
60
|
-
rake (12.3.2)
|
61
|
-
simplecov (0.13.0)
|
62
|
-
docile (~> 1.1.0)
|
63
|
-
json (>= 1.8, < 3)
|
64
|
-
simplecov-html (~> 0.10.0)
|
65
|
-
simplecov-html (0.10.2)
|
66
|
-
sqlite3 (1.3.13)
|
67
|
-
thread_safe (0.3.6)
|
68
|
-
tzinfo (1.2.5)
|
69
|
-
thread_safe (~> 0.1)
|
70
|
-
|
71
|
-
PLATFORMS
|
72
|
-
ruby
|
73
|
-
|
74
|
-
DEPENDENCIES
|
75
|
-
actionpack
|
76
|
-
bundler
|
77
|
-
minitest-utils
|
78
|
-
normalize_attributes!
|
79
|
-
rake
|
80
|
-
simplecov
|
81
|
-
sqlite3
|
82
|
-
|
83
|
-
BUNDLED WITH
|
84
|
-
1.17.3
|