nullify_empty 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nullify_empty/active_record_extension.rb +14 -0
- data/lib/nullify_empty/types.rb +16 -0
- data/lib/nullify_empty/version.rb +3 -0
- data/lib/nullify_empty.rb +11 -0
- data/nullify_empty.gemspec +27 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '0459d74924231eed6419005c311ce8f2b196a7dd'
|
4
|
+
data.tar.gz: 0fc1c50cfe4d7489fe7be439521c53325905d747
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49fec594d8545501ddfb798b36d5b899340f27ee20766421676aa00ea9b0134047917b34a338ddb33d687657aab909e043a7cd87e92875d3f7772bd09b1b2747
|
7
|
+
data.tar.gz: 140cb8a4966a48a33b25af469a2f51c8b48cb5230cf12714b4fe4327a6fb1e149641f0e688d1002650d00961efcb1e4d181e349f71fd3ec083c6a90d0f696dc2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Anton Chumakov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# NullifyEmpty
|
2
|
+
|
3
|
+
This gem allows to store string or text attributes as NULL in DB if they have empty values.
|
4
|
+
It is very useful when you need to avoid `""` values coming from blank form inputs or anywhere else to be stored in DB.
|
5
|
+
|
6
|
+
Having empty values in DB is not allowed when there is unique constraint on the column or uniqueness validation on the field, while presence of the values is not required. Unique constraint ignores nulls in most DBMS.
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'nullify_empty'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install nullify_empty
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Use `nullify_empty` method in the model to specify attributes that must not have empty values stored as empty strings in DB:
|
28
|
+
|
29
|
+
``` ruby
|
30
|
+
class User < ApplicationRecord
|
31
|
+
nullify_empty :email, :phone
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
|
36
|
+
``` ruby
|
37
|
+
params = {email: "", phone: "", ...}
|
38
|
+
user = User.create(params) # => #<User id: 1, email: nil, phone: nil, ... >
|
39
|
+
|
40
|
+
user.email = ""
|
41
|
+
user.email # => nil
|
42
|
+
|
43
|
+
user.update(email: "")
|
44
|
+
user.email # => nil
|
45
|
+
|
46
|
+
user.update_column :email, ""
|
47
|
+
user.email # => nil
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nullify_empty. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nullify_empty"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module NullifyEmpty
|
2
|
+
module ActiveRecordExtension
|
3
|
+
def nullify_empty(*attr_names)
|
4
|
+
attr_names.each do |attr_name|
|
5
|
+
original_type_klass = attribute_types[attr_name.to_s].class
|
6
|
+
|
7
|
+
new_type_klass =
|
8
|
+
NULLIFY_EMPTY_TYPES.find { |_, klass| klass.superclass == original_type_klass }.first
|
9
|
+
|
10
|
+
attribute attr_name, new_type_klass
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module NullifyEmpty
|
2
|
+
class NullifyEmptyStringType < ActiveRecord::Type::String; end
|
3
|
+
class NullifyEmptyTextType < ActiveRecord::Type::Text; end
|
4
|
+
|
5
|
+
NULLIFY_EMPTY_TYPES = {nullify_empty_string: NullifyEmptyStringType,
|
6
|
+
nullify_empty_text: NullifyEmptyTextType}
|
7
|
+
|
8
|
+
NULLIFY_EMPTY_TYPES.values.each do |klass|
|
9
|
+
klass.class_eval do
|
10
|
+
def cast(value)
|
11
|
+
value = nil if value.respond_to?(:empty?) && value.empty?
|
12
|
+
super value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "nullify_empty/version"
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
require "nullify_empty/types"
|
5
|
+
require "nullify_empty/active_record_extension"
|
6
|
+
|
7
|
+
NullifyEmpty::NULLIFY_EMPTY_TYPES.each do |symbol, klass|
|
8
|
+
ActiveRecord::Type.register(symbol, klass)
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveRecord::Base.extend NullifyEmpty::ActiveRecordExtension
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "nullify_empty/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nullify_empty"
|
8
|
+
spec.version = NullifyEmpty::VERSION
|
9
|
+
spec.authors = ["Anton Chumakov"]
|
10
|
+
spec.email = ["anton@chumakoff.com"]
|
11
|
+
|
12
|
+
spec.summary = "Store empty values as NULL in DB."
|
13
|
+
spec.description = "This gem allows to ensure there are no empty strings in DB, but nulls."
|
14
|
+
spec.homepage = "https://github.com/chumakoff/nullify_empty"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "activerecord", ">= 4.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nullify_empty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anton Chumakov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-31 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: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: This gem allows to ensure there are no empty strings in DB, but nulls.
|
70
|
+
email:
|
71
|
+
- anton@chumakoff.com
|
72
|
+
executables:
|
73
|
+
- console
|
74
|
+
- setup
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- ".travis.yml"
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/nullify_empty.rb
|
88
|
+
- lib/nullify_empty/active_record_extension.rb
|
89
|
+
- lib/nullify_empty/types.rb
|
90
|
+
- lib/nullify_empty/version.rb
|
91
|
+
- nullify_empty.gemspec
|
92
|
+
homepage: https://github.com/chumakoff/nullify_empty
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.6.11
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Store empty values as NULL in DB.
|
116
|
+
test_files: []
|