activerecord-blanks 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +4 -0
- data/.travis.yml +25 -0
- data/Gemfile +9 -0
- data/README.md +66 -0
- data/Rakefile +6 -0
- data/activerecord-blanks.gemspec +29 -0
- data/gems/rails5.gemfile +4 -0
- data/gems/rails6.gemfile +4 -0
- data/lib/active_record/blanks.rb +56 -0
- data/lib/active_record/blanks/version.rb +25 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ec529edf569a764371697f2c1713c08d6bfefb2372f5bb3cbe237c3c9e9b9c98
|
4
|
+
data.tar.gz: 326835369278715267e6fcbcd6fcb10463cb68ed02e95bc51938f202384be4c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ccc7219382d60649ac100154b1336587bb6423846a769a3a72bff1bd2971e13fde260b8964127027edb1161e7cb20ec66a3b63d626895b820976a8252f9ff34b
|
7
|
+
data.tar.gz: 41817bddcc54e13f3ec0ba3006a23164e3eeca45d79ef258fc943c22bf7a88e45dfaa4d4a8f3b32c126c3cd29f2cd1932d9f778f899a2f28956450907675b97e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
language: ruby
|
2
|
+
dist: xenial
|
3
|
+
cache: bundler
|
4
|
+
|
5
|
+
matrix:
|
6
|
+
include:
|
7
|
+
- rvm: 2.4
|
8
|
+
gemfile: gems/rails5.gemfile
|
9
|
+
- rvm: 2.5
|
10
|
+
gemfile: gems/rails5.gemfile
|
11
|
+
- rvm: 2.6
|
12
|
+
gemfile: gems/rails5.gemfile
|
13
|
+
- rvm: 2.6
|
14
|
+
gemfile: gems/rails6.gemfile
|
15
|
+
- rvm: 2.6
|
16
|
+
gemfile: gems/rails5.gemfile
|
17
|
+
env: COVERAGE=PartialSummary,Coveralls
|
18
|
+
- rvm: truffleruby
|
19
|
+
- rvm: jruby-head
|
20
|
+
env: JRUBY_OPTS="--debug -X+O"
|
21
|
+
- rvm: ruby-head
|
22
|
+
allow_failures:
|
23
|
+
- rvm: truffleruby
|
24
|
+
- rvm: ruby-head
|
25
|
+
- rvm: jruby-head
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# ActiveRecord::Blanks
|
2
|
+
|
3
|
+
This gem provides opinionated support for converting blank string values to nil.
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.com/ioquatix/activerecord-blanks.svg)](https://travis-ci.com/ioquatix/activerecord-blanks)
|
6
|
+
|
7
|
+
## Motivation
|
8
|
+
|
9
|
+
There are too many poor implementations of this feature.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'activerecord-blanks'
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In your model files:
|
22
|
+
|
23
|
+
```
|
24
|
+
class MyModel < ActiveRecord::Base
|
25
|
+
prepend ActiveRecord::Blanks
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
Any database column which allows `null`, and receives a String value, will be trimmed. Then, if the value is `#blank?`, it will be replaced with nil. This is done before validations.
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
38
|
+
|
39
|
+
## See Also
|
40
|
+
|
41
|
+
- [ActiveRecord::Rack](https://github.com/ioquatix/activerecord-rack)
|
42
|
+
- [ActiveRecord::Migrations](https://github.com/ioquatix/activerecord-migrations)
|
43
|
+
|
44
|
+
## License
|
45
|
+
|
46
|
+
Released under the MIT license.
|
47
|
+
|
48
|
+
Copyright, 2019, by [Samuel G. D. Williams](http://www.codeotaku.com).
|
49
|
+
|
50
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
51
|
+
of this software and associated documentation files (the "Software"), to deal
|
52
|
+
in the Software without restriction, including without limitation the rights
|
53
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
54
|
+
copies of the Software, and to permit persons to whom the Software is
|
55
|
+
furnished to do so, subject to the following conditions:
|
56
|
+
|
57
|
+
The above copyright notice and this permission notice shall be included in
|
58
|
+
all copies or substantial portions of the Software.
|
59
|
+
|
60
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
61
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
62
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
63
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
64
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
65
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
66
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require_relative 'lib/active_record/blanks/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "activerecord-blanks"
|
6
|
+
spec.version = ActiveRecord::Blanks::VERSION
|
7
|
+
spec.authors = ["Samuel Williams"]
|
8
|
+
spec.email = ["samuel.williams@oriontransfer.co.nz"]
|
9
|
+
|
10
|
+
spec.summary = %q{Convert blank values to nil.}
|
11
|
+
spec.homepage = "https://github.com/ioquatix/activerecord-blanks"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
15
|
+
f.match(%r{^(test|spec|features)/})
|
16
|
+
end
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", ">= 5", "< 7"
|
22
|
+
|
23
|
+
spec.add_dependency "rainbow", "~> 2.0"
|
24
|
+
|
25
|
+
spec.add_development_dependency "covered"
|
26
|
+
spec.add_development_dependency "bundler"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
end
|
data/gems/rails5.gemfile
ADDED
data/gems/rails6.gemfile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require_relative "blanks/version"
|
22
|
+
|
23
|
+
module ActiveRecord
|
24
|
+
module Blanks
|
25
|
+
def blank_columns
|
26
|
+
return to_enum(:blank_columns) unless block_given?
|
27
|
+
|
28
|
+
self.class.columns.each do |column|
|
29
|
+
if column.null
|
30
|
+
yield column
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def convert_blanks(to = nil)
|
36
|
+
blank_columns do |column|
|
37
|
+
value = read_attribute(column.name)
|
38
|
+
|
39
|
+
next unless value.is_a?(String)
|
40
|
+
|
41
|
+
# Strip leading and trailing whitespace:
|
42
|
+
value = value.strip
|
43
|
+
|
44
|
+
if value.blank?
|
45
|
+
write_attribute(column.name, to)
|
46
|
+
else
|
47
|
+
write_attribute(column.name, value)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.prepended(base)
|
53
|
+
base.before_validation :convert_blanks
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module ActiveRecord
|
22
|
+
module Blanks
|
23
|
+
VERSION = "1.0.0"
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-blanks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Williams
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rainbow
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: covered
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: bundler
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rake
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '10.0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '10.0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rspec
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '3.0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '3.0'
|
103
|
+
description:
|
104
|
+
email:
|
105
|
+
- samuel.williams@oriontransfer.co.nz
|
106
|
+
executables: []
|
107
|
+
extensions: []
|
108
|
+
extra_rdoc_files: []
|
109
|
+
files:
|
110
|
+
- ".gitignore"
|
111
|
+
- ".rspec"
|
112
|
+
- ".travis.yml"
|
113
|
+
- Gemfile
|
114
|
+
- README.md
|
115
|
+
- Rakefile
|
116
|
+
- activerecord-blanks.gemspec
|
117
|
+
- gems/rails5.gemfile
|
118
|
+
- gems/rails6.gemfile
|
119
|
+
- lib/active_record/blanks.rb
|
120
|
+
- lib/active_record/blanks/version.rb
|
121
|
+
homepage: https://github.com/ioquatix/activerecord-blanks
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubygems_version: 3.0.4
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Convert blank values to nil.
|
144
|
+
test_files: []
|