nilify_blanks 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.rdoc +6 -0
- data/Gemfile.lock +20 -19
- data/README.rdoc +10 -3
- data/lib/nilify_blanks.rb +4 -2
- data/lib/nilify_blanks/version.rb +1 -1
- data/spec/db/schema.rb +6 -3
- data/spec/db/test.sqlite3 +0 -0
- data/spec/nilify_blanks_spec.rb +60 -33
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 406a558d296d89cadd2f2e3a1b73ca9730757571
|
4
|
+
data.tar.gz: c7df79461bf71561220e6f2820365e23f6a859a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43f5d7c175b34385ae119e513cdc0915d0bb1357d9cdd6ddf7a54659bcbac59d30c36069d8aa88f3104ca260e097c2df8191e75ce998255811dcf891aeef7841
|
7
|
+
data.tar.gz: 49303614f425476f5621ac72d2ef7b94fc25b2480de1f460b610aafc366ec146808437514b2abd27b98c616204f49f4076f43eb243f24715843d2b25bbc5ce9a
|
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== [2017-12-23] 1.3.0
|
2
|
+
|
3
|
+
* Use all columns (not just content_columns)
|
4
|
+
* Allow nullables_only: false option for including columns that don't allow NULLs
|
5
|
+
* Support citext columns
|
6
|
+
|
1
7
|
== [2015-04-12] 1.2.1
|
2
8
|
|
3
9
|
* Fix inconsistency in nilify_blanks RSpec matcher: should filter by type
|
data/Gemfile.lock
CHANGED
@@ -1,37 +1,35 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nilify_blanks (1.2.
|
4
|
+
nilify_blanks (1.2.1)
|
5
5
|
activerecord (>= 3.0.0)
|
6
6
|
activesupport (>= 3.0.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: http://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (
|
12
|
-
activesupport (=
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
activemodel (5.1.4)
|
12
|
+
activesupport (= 5.1.4)
|
13
|
+
activerecord (5.1.4)
|
14
|
+
activemodel (= 5.1.4)
|
15
|
+
activesupport (= 5.1.4)
|
16
|
+
arel (~> 8.0)
|
17
|
+
activesupport (5.1.4)
|
18
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
19
|
i18n (~> 0.7)
|
20
|
-
json (~> 1.7, >= 1.7.7)
|
21
20
|
minitest (~> 5.1)
|
22
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
23
21
|
tzinfo (~> 1.1)
|
24
22
|
appraisal (1.0.2)
|
25
23
|
bundler
|
26
24
|
rake
|
27
25
|
thor (>= 0.14.0)
|
28
|
-
arel (
|
29
|
-
|
26
|
+
arel (8.0.0)
|
27
|
+
concurrent-ruby (1.0.5)
|
30
28
|
diff-lcs (1.2.1)
|
31
29
|
ffi2-generators (0.1.1)
|
32
|
-
i18n (0.
|
33
|
-
|
34
|
-
minitest (5.
|
30
|
+
i18n (0.9.1)
|
31
|
+
concurrent-ruby (~> 1.0)
|
32
|
+
minitest (5.10.3)
|
35
33
|
rake (10.0.3)
|
36
34
|
rspec (2.13.0)
|
37
35
|
rspec-core (~> 2.13.0)
|
@@ -243,10 +241,10 @@ GEM
|
|
243
241
|
rubysl-xmlrpc (2.0.0)
|
244
242
|
rubysl-yaml (2.0.4)
|
245
243
|
rubysl-zlib (2.0.1)
|
246
|
-
sqlite3 (1.3.
|
244
|
+
sqlite3 (1.3.13)
|
247
245
|
thor (0.19.1)
|
248
|
-
thread_safe (0.3.
|
249
|
-
tzinfo (1.2.
|
246
|
+
thread_safe (0.3.6)
|
247
|
+
tzinfo (1.2.4)
|
250
248
|
thread_safe (~> 0.1)
|
251
249
|
|
252
250
|
PLATFORMS
|
@@ -260,3 +258,6 @@ DEPENDENCIES
|
|
260
258
|
rspec (~> 2.13)
|
261
259
|
rubysl (~> 2.0)
|
262
260
|
sqlite3
|
261
|
+
|
262
|
+
BUNDLED WITH
|
263
|
+
1.15.4
|
data/README.rdoc
CHANGED
@@ -4,9 +4,11 @@
|
|
4
4
|
|
5
5
|
== Nilify Blanks
|
6
6
|
|
7
|
-
In Rails when saving a model from a form and values are not provided by the user, an empty string is recorded to the database instead of a NULL as many would prefer (mixing blanks and NULLs can become confusing). This plugin allows you to specify a list of attributes (or exceptions from all the attributes) that will be converted to nil if they are blank before a model is saved.
|
7
|
+
In Rails when saving a model from a form and values are not provided by the user, an empty string is recorded to the database instead of a NULL as many would prefer (mixing blanks and NULLs can become confusing). This plugin allows you to specify a list of attributes (or exceptions from all the attributes) that will be converted to nil if they are blank before a model is saved.
|
8
8
|
|
9
|
-
|
9
|
+
By default, columns set as NOT NULL will not be nilified, since that value cannot be persisted, and it might be better to catch that with a validation. If you want to nilify those columns anyway, you can use `only:` (see below) to explicitly mention them.
|
10
|
+
|
11
|
+
Only attributes responding to blank? with a value of true will be converted to nil. Therefore, this does not work with integer fields with the value of 0, for example. Usage is best shown through examples:
|
10
12
|
|
11
13
|
== Requirements
|
12
14
|
|
@@ -43,9 +45,14 @@ Include the gem using bundler in your Gemfile:
|
|
43
45
|
nilify_blanks :except => [:author, :title]
|
44
46
|
end
|
45
47
|
|
48
|
+
# Checks and converts any fields, regardless of thier null constraint
|
49
|
+
class Post < ActiveRecord::Base
|
50
|
+
nilify_blanks :nullables_only => false
|
51
|
+
end
|
52
|
+
|
46
53
|
== Global Usage
|
47
54
|
|
48
|
-
You can also apply nilify_blanks to all models
|
55
|
+
You can also apply nilify_blanks to all models inheriting from ActiveRecord::Base:
|
49
56
|
|
50
57
|
ActiveRecord::Base.nilify_blanks
|
51
58
|
|
data/lib/nilify_blanks.rb
CHANGED
@@ -4,7 +4,7 @@ module NilifyBlanks
|
|
4
4
|
end
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
DEFAULT_TYPES = [:string, :text]
|
7
|
+
DEFAULT_TYPES = [:string, :text, :citext]
|
8
8
|
|
9
9
|
@@define_nilify_blank_methods_lock = Mutex.new
|
10
10
|
|
@@ -64,8 +64,10 @@ module NilifyBlanks
|
|
64
64
|
|
65
65
|
if options[:only]
|
66
66
|
self.nilify_blanks_columns = options[:only].clone
|
67
|
+
elsif options[:nullables_only] == false
|
68
|
+
self.nilify_blanks_columns = self.columns.select {|c| options[:types].include?(c.type) }.map(&:name).map(&:to_s)
|
67
69
|
else
|
68
|
-
self.nilify_blanks_columns = self.
|
70
|
+
self.nilify_blanks_columns = self.columns.select(&:null).select {|c| options[:types].include?(c.type) }.map(&:name).map(&:to_s)
|
69
71
|
end
|
70
72
|
|
71
73
|
self.nilify_blanks_columns -= options[:except] if options[:except]
|
data/spec/db/schema.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
ActiveRecord::Schema.define(:version => 0) do
|
2
|
-
|
2
|
+
|
3
3
|
create_table :posts, :force => true do |t|
|
4
4
|
t.string :first_name
|
5
5
|
t.string :last_name, :null => false
|
6
6
|
t.string :title
|
7
7
|
t.text :summary
|
8
8
|
t.text :body
|
9
|
+
t.column :slug, :citext
|
9
10
|
t.integer :views
|
11
|
+
t.integer :category_id
|
12
|
+
t.string :blog_id
|
10
13
|
end
|
11
|
-
|
12
|
-
end
|
14
|
+
|
15
|
+
end
|
data/spec/db/test.sqlite3
CHANGED
Binary file
|
data/spec/nilify_blanks_spec.rb
CHANGED
@@ -1,116 +1,143 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe NilifyBlanks do
|
4
|
-
|
4
|
+
|
5
5
|
context "Model with nilify_blanks" do
|
6
6
|
before(:all) do
|
7
7
|
class Post < ActiveRecord::Base
|
8
8
|
nilify_blanks
|
9
9
|
end
|
10
|
-
|
11
|
-
@post = Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
10
|
+
|
11
|
+
@post = Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0, :blog_id => '')
|
12
12
|
@post.save
|
13
13
|
end
|
14
|
-
|
15
|
-
it "should recognize all non-null string, text columns" do
|
16
|
-
Post.nilify_blanks_columns.should == ['first_name', 'title', 'summary', 'body']
|
14
|
+
|
15
|
+
it "should recognize all non-null string, text, citext columns" do
|
16
|
+
Post.nilify_blanks_columns.should == ['first_name', 'title', 'summary', 'body', 'slug', 'blog_id']
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
it "should convert all blanks to nils" do
|
20
20
|
@post.first_name.should be_nil
|
21
21
|
@post.title.should be_nil
|
22
22
|
@post.summary.should be_nil
|
23
23
|
@post.body.should be_nil
|
24
|
+
@post.slug.should be_nil
|
25
|
+
@post.blog_id.should be_nil
|
24
26
|
end
|
25
|
-
|
27
|
+
|
26
28
|
it "should leave not-null last name field alone" do
|
27
29
|
@post.last_name.should == ""
|
28
30
|
end
|
29
|
-
|
31
|
+
|
30
32
|
it "should leave integer views field alone" do
|
31
33
|
@post.views.should == 0
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
37
|
+
context "Model with nilify_blanks :nullables_only => false" do
|
38
|
+
before(:all) do
|
39
|
+
class PostWithNullables < ActiveRecord::Base
|
40
|
+
self.table_name = "posts"
|
41
|
+
nilify_blanks :nullables_only => false
|
42
|
+
end
|
43
|
+
|
44
|
+
@post = PostWithNullables.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0, :blog_id => '')
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should recognize all (even null) string, text, citext columns" do
|
48
|
+
PostWithNullables.nilify_blanks_columns.should == ['first_name', 'last_name', 'title', 'summary', 'body', 'slug', 'blog_id']
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
35
52
|
context "Model with nilify_blanks :types => [:text]" do
|
53
|
+
def citext_supported
|
54
|
+
PostOnlyText.content_columns.detect {|c| c.type == :citext}
|
55
|
+
end
|
56
|
+
|
36
57
|
before(:all) do
|
37
58
|
class PostOnlyText < ActiveRecord::Base
|
38
59
|
self.table_name = "posts"
|
39
60
|
nilify_blanks :types => [:text]
|
40
61
|
end
|
41
|
-
|
42
|
-
@post = PostOnlyText.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
62
|
+
|
63
|
+
@post = PostOnlyText.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
|
43
64
|
@post.save
|
44
65
|
end
|
45
|
-
|
66
|
+
|
46
67
|
it "should recognize all non-null text only columns" do
|
47
|
-
|
68
|
+
expected_types = ['summary', 'body']
|
69
|
+
expected_types << 'slug' unless citext_supported
|
70
|
+
PostOnlyText.nilify_blanks_columns.should == expected_types
|
48
71
|
end
|
49
|
-
|
72
|
+
|
50
73
|
it "should convert all blanks to nils" do
|
51
74
|
@post.summary.should be_nil
|
52
75
|
@post.body.should be_nil
|
76
|
+
@post.slug.should be_nil unless citext_supported
|
53
77
|
end
|
54
|
-
|
78
|
+
|
55
79
|
it "should leave not-null string fields alone" do
|
56
80
|
@post.first_name.should == ""
|
57
81
|
@post.last_name.should == ""
|
58
82
|
@post.title.should == ""
|
83
|
+
@post.slug.should == "" if citext_supported
|
59
84
|
end
|
60
85
|
end
|
61
|
-
|
86
|
+
|
62
87
|
context "Model with nilify_blanks :only => [:first_name, :title]" do
|
63
88
|
before(:all) do
|
64
89
|
class PostOnlyFirstNameAndTitle < ActiveRecord::Base
|
65
90
|
self.table_name = "posts"
|
66
91
|
nilify_blanks :only => [:first_name, :title]
|
67
92
|
end
|
68
|
-
|
69
|
-
@post = PostOnlyFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
93
|
+
|
94
|
+
@post = PostOnlyFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
|
70
95
|
@post.save
|
71
96
|
end
|
72
|
-
|
97
|
+
|
73
98
|
it "should recognize only first_name and title" do
|
74
99
|
PostOnlyFirstNameAndTitle.nilify_blanks_columns.should == ['first_name', 'title']
|
75
100
|
end
|
76
|
-
|
101
|
+
|
77
102
|
it "should convert first_name and title blanks to nils" do
|
78
103
|
@post.first_name.should be_nil
|
79
104
|
@post.title.should be_nil
|
80
105
|
end
|
81
|
-
|
106
|
+
|
82
107
|
it "should leave other fields alone" do
|
83
108
|
@post.summary.should == ""
|
84
109
|
@post.body.should == ""
|
110
|
+
@post.slug.should == ""
|
85
111
|
end
|
86
112
|
end
|
87
|
-
|
113
|
+
|
88
114
|
context "Model with nilify_blanks :except => [:first_name, :title]" do
|
89
115
|
before(:all) do
|
90
116
|
class PostExceptFirstNameAndTitle < ActiveRecord::Base
|
91
117
|
self.table_name = "posts"
|
92
|
-
nilify_blanks :except => [:first_name, :title]
|
118
|
+
nilify_blanks :except => [:first_name, :title, :blog_id]
|
93
119
|
end
|
94
|
-
|
95
|
-
@post = PostExceptFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
120
|
+
|
121
|
+
@post = PostExceptFirstNameAndTitle.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
|
96
122
|
@post.save
|
97
123
|
end
|
98
|
-
|
124
|
+
|
99
125
|
it "should recognize only summary, body, and views" do
|
100
|
-
PostExceptFirstNameAndTitle.nilify_blanks_columns.should == ['summary', 'body']
|
126
|
+
PostExceptFirstNameAndTitle.nilify_blanks_columns.should == ['summary', 'body', 'slug']
|
101
127
|
end
|
102
|
-
|
128
|
+
|
103
129
|
it "should convert summary and body blanks to nils" do
|
104
130
|
@post.summary.should be_nil
|
105
131
|
@post.body.should be_nil
|
132
|
+
@post.slug.should be_nil
|
106
133
|
end
|
107
|
-
|
134
|
+
|
108
135
|
it "should leave other fields alone" do
|
109
136
|
@post.first_name.should == ""
|
110
137
|
@post.title.should == ""
|
111
138
|
end
|
112
139
|
end
|
113
|
-
|
140
|
+
|
114
141
|
|
115
142
|
context "Global Usage" do
|
116
143
|
context "Namespaced Base Class with nilify_blanks inline" do
|
@@ -126,7 +153,7 @@ describe NilifyBlanks do
|
|
126
153
|
self.table_name = "posts"
|
127
154
|
end
|
128
155
|
|
129
|
-
@post = Admin1::Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
156
|
+
@post = Admin1::Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
|
130
157
|
@post.save
|
131
158
|
end
|
132
159
|
|
@@ -149,7 +176,7 @@ describe NilifyBlanks do
|
|
149
176
|
|
150
177
|
Admin2::Base.nilify_blanks
|
151
178
|
|
152
|
-
@post = Admin2::Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
179
|
+
@post = Admin2::Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
|
153
180
|
@post.save
|
154
181
|
end
|
155
182
|
|
@@ -166,7 +193,7 @@ describe NilifyBlanks do
|
|
166
193
|
self.table_name = "posts"
|
167
194
|
end
|
168
195
|
|
169
|
-
@post = InheritedPost.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
196
|
+
@post = InheritedPost.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :slug => '', :views => 0)
|
170
197
|
@post.save
|
171
198
|
end
|
172
199
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nilify_blanks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Hughes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -120,7 +120,8 @@ files:
|
|
120
120
|
- spec/nilify_blanks_spec.rb
|
121
121
|
- spec/spec_helper.rb
|
122
122
|
homepage: http://github.com/rubiety/nilify_blanks
|
123
|
-
licenses:
|
123
|
+
licenses:
|
124
|
+
- MIT
|
124
125
|
metadata: {}
|
125
126
|
post_install_message:
|
126
127
|
rdoc_options: []
|
@@ -138,9 +139,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
139
|
version: 1.3.4
|
139
140
|
requirements: []
|
140
141
|
rubyforge_project: nilify_blanks
|
141
|
-
rubygems_version: 2.
|
142
|
+
rubygems_version: 2.6.11
|
142
143
|
signing_key:
|
143
144
|
specification_version: 4
|
144
145
|
summary: Auto-convert blank fields to nil.
|
145
146
|
test_files: []
|
146
|
-
has_rdoc:
|