nilify_blanks 1.0.3 → 1.1.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.
- data/CHANGELOG.rdoc +4 -0
- data/Gemfile.lock +227 -25
- data/README.rdoc +48 -5
- data/lib/nilify_blanks.rb +52 -17
- data/lib/nilify_blanks/matchers.rb +40 -0
- data/lib/nilify_blanks/railtie.rb +1 -1
- data/lib/nilify_blanks/version.rb +1 -1
- data/spec/db/test.sqlite3 +0 -0
- data/spec/nilify_blanks_spec.rb +96 -4
- data/spec/spec_helper.rb +1 -0
- metadata +20 -5
- checksums.yaml +0 -15
data/CHANGELOG.rdoc
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,38 +1,36 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
nilify_blanks (1.0
|
|
4
|
+
nilify_blanks (1.1.0)
|
|
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 (4.
|
|
12
|
-
activesupport (= 4.
|
|
13
|
-
builder (~> 3.1
|
|
14
|
-
activerecord (4.
|
|
15
|
-
activemodel (= 4.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
minitest (~> 4.2)
|
|
23
|
-
multi_json (~> 1.3)
|
|
11
|
+
activemodel (4.1.1)
|
|
12
|
+
activesupport (= 4.1.1)
|
|
13
|
+
builder (~> 3.1)
|
|
14
|
+
activerecord (4.1.1)
|
|
15
|
+
activemodel (= 4.1.1)
|
|
16
|
+
activesupport (= 4.1.1)
|
|
17
|
+
arel (~> 5.0.0)
|
|
18
|
+
activesupport (4.1.1)
|
|
19
|
+
i18n (~> 0.6, >= 0.6.9)
|
|
20
|
+
json (~> 1.7, >= 1.7.7)
|
|
21
|
+
minitest (~> 5.1)
|
|
24
22
|
thread_safe (~> 0.1)
|
|
25
|
-
tzinfo (~>
|
|
23
|
+
tzinfo (~> 1.1)
|
|
26
24
|
appraisal (0.5.2)
|
|
27
25
|
bundler
|
|
28
26
|
rake
|
|
29
|
-
arel (
|
|
30
|
-
|
|
31
|
-
builder (3.1.4)
|
|
27
|
+
arel (5.0.1.20140414130214)
|
|
28
|
+
builder (3.2.2)
|
|
32
29
|
diff-lcs (1.2.1)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
ffi2-generators (0.1.1)
|
|
31
|
+
i18n (0.6.9)
|
|
32
|
+
json (1.8.1)
|
|
33
|
+
minitest (5.3.4)
|
|
36
34
|
rake (10.0.3)
|
|
37
35
|
rspec (2.13.0)
|
|
38
36
|
rspec-core (~> 2.13.0)
|
|
@@ -42,17 +40,221 @@ GEM
|
|
|
42
40
|
rspec-expectations (2.13.0)
|
|
43
41
|
diff-lcs (>= 1.1.3, < 2.0)
|
|
44
42
|
rspec-mocks (2.13.0)
|
|
43
|
+
rubysl (2.0.15)
|
|
44
|
+
rubysl-abbrev (~> 2.0)
|
|
45
|
+
rubysl-base64 (~> 2.0)
|
|
46
|
+
rubysl-benchmark (~> 2.0)
|
|
47
|
+
rubysl-bigdecimal (~> 2.0)
|
|
48
|
+
rubysl-cgi (~> 2.0)
|
|
49
|
+
rubysl-cgi-session (~> 2.0)
|
|
50
|
+
rubysl-cmath (~> 2.0)
|
|
51
|
+
rubysl-complex (~> 2.0)
|
|
52
|
+
rubysl-continuation (~> 2.0)
|
|
53
|
+
rubysl-coverage (~> 2.0)
|
|
54
|
+
rubysl-csv (~> 2.0)
|
|
55
|
+
rubysl-curses (~> 2.0)
|
|
56
|
+
rubysl-date (~> 2.0)
|
|
57
|
+
rubysl-delegate (~> 2.0)
|
|
58
|
+
rubysl-digest (~> 2.0)
|
|
59
|
+
rubysl-drb (~> 2.0)
|
|
60
|
+
rubysl-e2mmap (~> 2.0)
|
|
61
|
+
rubysl-english (~> 2.0)
|
|
62
|
+
rubysl-enumerator (~> 2.0)
|
|
63
|
+
rubysl-erb (~> 2.0)
|
|
64
|
+
rubysl-etc (~> 2.0)
|
|
65
|
+
rubysl-expect (~> 2.0)
|
|
66
|
+
rubysl-fcntl (~> 2.0)
|
|
67
|
+
rubysl-fiber (~> 2.0)
|
|
68
|
+
rubysl-fileutils (~> 2.0)
|
|
69
|
+
rubysl-find (~> 2.0)
|
|
70
|
+
rubysl-forwardable (~> 2.0)
|
|
71
|
+
rubysl-getoptlong (~> 2.0)
|
|
72
|
+
rubysl-gserver (~> 2.0)
|
|
73
|
+
rubysl-io-console (~> 2.0)
|
|
74
|
+
rubysl-io-nonblock (~> 2.0)
|
|
75
|
+
rubysl-io-wait (~> 2.0)
|
|
76
|
+
rubysl-ipaddr (~> 2.0)
|
|
77
|
+
rubysl-irb (~> 2.0)
|
|
78
|
+
rubysl-logger (~> 2.0)
|
|
79
|
+
rubysl-mathn (~> 2.0)
|
|
80
|
+
rubysl-matrix (~> 2.0)
|
|
81
|
+
rubysl-mkmf (~> 2.0)
|
|
82
|
+
rubysl-monitor (~> 2.0)
|
|
83
|
+
rubysl-mutex_m (~> 2.0)
|
|
84
|
+
rubysl-net-ftp (~> 2.0)
|
|
85
|
+
rubysl-net-http (~> 2.0)
|
|
86
|
+
rubysl-net-imap (~> 2.0)
|
|
87
|
+
rubysl-net-pop (~> 2.0)
|
|
88
|
+
rubysl-net-protocol (~> 2.0)
|
|
89
|
+
rubysl-net-smtp (~> 2.0)
|
|
90
|
+
rubysl-net-telnet (~> 2.0)
|
|
91
|
+
rubysl-nkf (~> 2.0)
|
|
92
|
+
rubysl-observer (~> 2.0)
|
|
93
|
+
rubysl-open-uri (~> 2.0)
|
|
94
|
+
rubysl-open3 (~> 2.0)
|
|
95
|
+
rubysl-openssl (~> 2.0)
|
|
96
|
+
rubysl-optparse (~> 2.0)
|
|
97
|
+
rubysl-ostruct (~> 2.0)
|
|
98
|
+
rubysl-pathname (~> 2.0)
|
|
99
|
+
rubysl-prettyprint (~> 2.0)
|
|
100
|
+
rubysl-prime (~> 2.0)
|
|
101
|
+
rubysl-profile (~> 2.0)
|
|
102
|
+
rubysl-profiler (~> 2.0)
|
|
103
|
+
rubysl-pstore (~> 2.0)
|
|
104
|
+
rubysl-pty (~> 2.0)
|
|
105
|
+
rubysl-rational (~> 2.0)
|
|
106
|
+
rubysl-readline (~> 2.0)
|
|
107
|
+
rubysl-resolv (~> 2.0)
|
|
108
|
+
rubysl-rexml (~> 2.0)
|
|
109
|
+
rubysl-rinda (~> 2.0)
|
|
110
|
+
rubysl-rss (~> 2.0)
|
|
111
|
+
rubysl-scanf (~> 2.0)
|
|
112
|
+
rubysl-securerandom (~> 2.0)
|
|
113
|
+
rubysl-set (~> 2.0)
|
|
114
|
+
rubysl-shellwords (~> 2.0)
|
|
115
|
+
rubysl-singleton (~> 2.0)
|
|
116
|
+
rubysl-socket (~> 2.0)
|
|
117
|
+
rubysl-stringio (~> 2.0)
|
|
118
|
+
rubysl-strscan (~> 2.0)
|
|
119
|
+
rubysl-sync (~> 2.0)
|
|
120
|
+
rubysl-syslog (~> 2.0)
|
|
121
|
+
rubysl-tempfile (~> 2.0)
|
|
122
|
+
rubysl-thread (~> 2.0)
|
|
123
|
+
rubysl-thwait (~> 2.0)
|
|
124
|
+
rubysl-time (~> 2.0)
|
|
125
|
+
rubysl-timeout (~> 2.0)
|
|
126
|
+
rubysl-tmpdir (~> 2.0)
|
|
127
|
+
rubysl-tsort (~> 2.0)
|
|
128
|
+
rubysl-un (~> 2.0)
|
|
129
|
+
rubysl-uri (~> 2.0)
|
|
130
|
+
rubysl-weakref (~> 2.0)
|
|
131
|
+
rubysl-webrick (~> 2.0)
|
|
132
|
+
rubysl-xmlrpc (~> 2.0)
|
|
133
|
+
rubysl-yaml (~> 2.0)
|
|
134
|
+
rubysl-zlib (~> 2.0)
|
|
135
|
+
rubysl-abbrev (2.0.4)
|
|
136
|
+
rubysl-base64 (2.0.0)
|
|
137
|
+
rubysl-benchmark (2.0.1)
|
|
138
|
+
rubysl-bigdecimal (2.0.2)
|
|
139
|
+
rubysl-cgi (2.0.1)
|
|
140
|
+
rubysl-cgi-session (2.0.1)
|
|
141
|
+
rubysl-cmath (2.0.0)
|
|
142
|
+
rubysl-complex (2.0.0)
|
|
143
|
+
rubysl-continuation (2.0.0)
|
|
144
|
+
rubysl-coverage (2.0.3)
|
|
145
|
+
rubysl-csv (2.0.2)
|
|
146
|
+
rubysl-english (~> 2.0)
|
|
147
|
+
rubysl-curses (2.0.1)
|
|
148
|
+
rubysl-date (2.0.6)
|
|
149
|
+
rubysl-delegate (2.0.1)
|
|
150
|
+
rubysl-digest (2.0.3)
|
|
151
|
+
rubysl-drb (2.0.1)
|
|
152
|
+
rubysl-e2mmap (2.0.0)
|
|
153
|
+
rubysl-english (2.0.0)
|
|
154
|
+
rubysl-enumerator (2.0.0)
|
|
155
|
+
rubysl-erb (2.0.1)
|
|
156
|
+
rubysl-etc (2.0.3)
|
|
157
|
+
ffi2-generators (~> 0.1)
|
|
158
|
+
rubysl-expect (2.0.0)
|
|
159
|
+
rubysl-fcntl (2.0.4)
|
|
160
|
+
ffi2-generators (~> 0.1)
|
|
161
|
+
rubysl-fiber (2.0.0)
|
|
162
|
+
rubysl-fileutils (2.0.3)
|
|
163
|
+
rubysl-find (2.0.1)
|
|
164
|
+
rubysl-forwardable (2.0.1)
|
|
165
|
+
rubysl-getoptlong (2.0.0)
|
|
166
|
+
rubysl-gserver (2.0.0)
|
|
167
|
+
rubysl-socket (~> 2.0)
|
|
168
|
+
rubysl-thread (~> 2.0)
|
|
169
|
+
rubysl-io-console (2.0.0)
|
|
170
|
+
rubysl-io-nonblock (2.0.0)
|
|
171
|
+
rubysl-io-wait (2.0.0)
|
|
172
|
+
rubysl-ipaddr (2.0.0)
|
|
173
|
+
rubysl-irb (2.0.4)
|
|
174
|
+
rubysl-e2mmap (~> 2.0)
|
|
175
|
+
rubysl-mathn (~> 2.0)
|
|
176
|
+
rubysl-readline (~> 2.0)
|
|
177
|
+
rubysl-thread (~> 2.0)
|
|
178
|
+
rubysl-logger (2.0.0)
|
|
179
|
+
rubysl-mathn (2.0.0)
|
|
180
|
+
rubysl-matrix (2.1.0)
|
|
181
|
+
rubysl-e2mmap (~> 2.0)
|
|
182
|
+
rubysl-mkmf (2.0.1)
|
|
183
|
+
rubysl-fileutils (~> 2.0)
|
|
184
|
+
rubysl-shellwords (~> 2.0)
|
|
185
|
+
rubysl-monitor (2.0.0)
|
|
186
|
+
rubysl-mutex_m (2.0.0)
|
|
187
|
+
rubysl-net-ftp (2.0.1)
|
|
188
|
+
rubysl-net-http (2.0.4)
|
|
189
|
+
rubysl-cgi (~> 2.0)
|
|
190
|
+
rubysl-erb (~> 2.0)
|
|
191
|
+
rubysl-singleton (~> 2.0)
|
|
192
|
+
rubysl-net-imap (2.0.1)
|
|
193
|
+
rubysl-net-pop (2.0.1)
|
|
194
|
+
rubysl-net-protocol (2.0.1)
|
|
195
|
+
rubysl-net-smtp (2.0.1)
|
|
196
|
+
rubysl-net-telnet (2.0.0)
|
|
197
|
+
rubysl-nkf (2.0.1)
|
|
198
|
+
rubysl-observer (2.0.0)
|
|
199
|
+
rubysl-open-uri (2.0.0)
|
|
200
|
+
rubysl-open3 (2.0.0)
|
|
201
|
+
rubysl-openssl (2.1.0)
|
|
202
|
+
rubysl-optparse (2.0.1)
|
|
203
|
+
rubysl-shellwords (~> 2.0)
|
|
204
|
+
rubysl-ostruct (2.0.4)
|
|
205
|
+
rubysl-pathname (2.0.0)
|
|
206
|
+
rubysl-prettyprint (2.0.3)
|
|
207
|
+
rubysl-prime (2.0.1)
|
|
208
|
+
rubysl-profile (2.0.0)
|
|
209
|
+
rubysl-profiler (2.0.1)
|
|
210
|
+
rubysl-pstore (2.0.0)
|
|
211
|
+
rubysl-pty (2.0.2)
|
|
212
|
+
rubysl-rational (2.0.1)
|
|
213
|
+
rubysl-readline (2.0.2)
|
|
214
|
+
rubysl-resolv (2.1.0)
|
|
215
|
+
rubysl-rexml (2.0.2)
|
|
216
|
+
rubysl-rinda (2.0.1)
|
|
217
|
+
rubysl-rss (2.0.0)
|
|
218
|
+
rubysl-scanf (2.0.0)
|
|
219
|
+
rubysl-securerandom (2.0.0)
|
|
220
|
+
rubysl-set (2.0.1)
|
|
221
|
+
rubysl-shellwords (2.0.0)
|
|
222
|
+
rubysl-singleton (2.0.0)
|
|
223
|
+
rubysl-socket (2.0.1)
|
|
224
|
+
rubysl-stringio (2.0.0)
|
|
225
|
+
rubysl-strscan (2.0.0)
|
|
226
|
+
rubysl-sync (2.0.0)
|
|
227
|
+
rubysl-syslog (2.0.1)
|
|
228
|
+
ffi2-generators (~> 0.1)
|
|
229
|
+
rubysl-tempfile (2.0.1)
|
|
230
|
+
rubysl-thread (2.0.2)
|
|
231
|
+
rubysl-thwait (2.0.0)
|
|
232
|
+
rubysl-time (2.0.3)
|
|
233
|
+
rubysl-timeout (2.0.0)
|
|
234
|
+
rubysl-tmpdir (2.0.1)
|
|
235
|
+
rubysl-tsort (2.0.1)
|
|
236
|
+
rubysl-un (2.0.0)
|
|
237
|
+
rubysl-fileutils (~> 2.0)
|
|
238
|
+
rubysl-optparse (~> 2.0)
|
|
239
|
+
rubysl-uri (2.0.0)
|
|
240
|
+
rubysl-weakref (2.0.0)
|
|
241
|
+
rubysl-webrick (2.0.0)
|
|
242
|
+
rubysl-xmlrpc (2.0.0)
|
|
243
|
+
rubysl-yaml (2.0.4)
|
|
244
|
+
rubysl-zlib (2.0.1)
|
|
45
245
|
sqlite3 (1.3.7)
|
|
46
|
-
thread_safe (0.
|
|
47
|
-
|
|
48
|
-
|
|
246
|
+
thread_safe (0.3.4)
|
|
247
|
+
tzinfo (1.2.1)
|
|
248
|
+
thread_safe (~> 0.1)
|
|
49
249
|
|
|
50
250
|
PLATFORMS
|
|
51
251
|
ruby
|
|
52
252
|
|
|
53
253
|
DEPENDENCIES
|
|
254
|
+
activerecord-jdbcsqlite3-adapter
|
|
54
255
|
appraisal (~> 0.5.2)
|
|
55
256
|
nilify_blanks!
|
|
56
257
|
rake (~> 10.0.3)
|
|
57
258
|
rspec (~> 2.13)
|
|
58
|
-
|
|
259
|
+
rubysl (~> 2.0)
|
|
260
|
+
sqlite3
|
data/README.rdoc
CHANGED
|
@@ -10,16 +10,21 @@ Only attributes responding to empty? with a value of true will be converted to n
|
|
|
10
10
|
|
|
11
11
|
== Install
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Include the gem using bundler in your Gemfile:
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
gem "nilify_blanks"
|
|
16
16
|
|
|
17
|
-
== Basic
|
|
17
|
+
== Basic Usage
|
|
18
18
|
|
|
19
|
-
# Checks and converts all fields in the model
|
|
19
|
+
# Checks and converts all content fields in the model
|
|
20
20
|
class Post < ActiveRecord::Base
|
|
21
21
|
nilify_blanks
|
|
22
22
|
end
|
|
23
|
+
|
|
24
|
+
# Checks and converts only text fields in the model
|
|
25
|
+
class Post < ActiveRecord::Base
|
|
26
|
+
nilify_blanks :types => [:text]
|
|
27
|
+
end
|
|
23
28
|
|
|
24
29
|
# Checks and converts only the title and author fields
|
|
25
30
|
class Post < ActiveRecord::Base
|
|
@@ -30,7 +35,17 @@ Only attributes responding to empty? with a value of true will be converted to n
|
|
|
30
35
|
class Post < ActiveRecord::Base
|
|
31
36
|
nilify_blanks :except => [:author, :title]
|
|
32
37
|
end
|
|
33
|
-
|
|
38
|
+
|
|
39
|
+
== Global Usage
|
|
40
|
+
|
|
41
|
+
You can also apply nilify_blanks to all models inhering from ActiveRecord::Base:
|
|
42
|
+
|
|
43
|
+
ActiveRecord::Base.nilify_blanks
|
|
44
|
+
|
|
45
|
+
Or perhaps just a model namespace base class:
|
|
46
|
+
|
|
47
|
+
Inventory::Base.nilify_blanks
|
|
48
|
+
|
|
34
49
|
== Specifying a Callback
|
|
35
50
|
|
|
36
51
|
Checking uses an ActiveRecord before_save filter by default, but you can specify a different filter with the :before option. Any filter will work - just first remove the "before_" prefix from the name.
|
|
@@ -43,6 +58,34 @@ Checking uses an ActiveRecord before_save filter by default, but you can specify
|
|
|
43
58
|
nilify_blanks :before => :validation_on_update
|
|
44
59
|
end
|
|
45
60
|
|
|
61
|
+
== RSpec Matcher
|
|
62
|
+
|
|
63
|
+
First, include the matchers:
|
|
64
|
+
|
|
65
|
+
require "nilify_blanks/matchers"
|
|
66
|
+
|
|
67
|
+
To ensure for a given column:
|
|
68
|
+
|
|
69
|
+
describe City do
|
|
70
|
+
it { should nilify_blanks_for(:name) }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
To ensure for all applicable content columns:
|
|
74
|
+
|
|
75
|
+
describe City do
|
|
76
|
+
it { should nilify_blanks }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
You can optionally match on options also:
|
|
80
|
+
|
|
81
|
+
describe City do
|
|
82
|
+
it { should nilify_blanks_for(:name, :before => :create) }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
describe City do
|
|
86
|
+
it { should nilify_blanks(:before => :create) }
|
|
87
|
+
end
|
|
88
|
+
|
|
46
89
|
== Running Tests
|
|
47
90
|
|
|
48
91
|
This gem uses appraisal to test with different versions of the dependencies. See Appraisal first for which versions are tested, then run to test all appraisals:
|
data/lib/nilify_blanks.rb
CHANGED
|
@@ -4,43 +4,78 @@ module NilifyBlanks
|
|
|
4
4
|
end
|
|
5
5
|
|
|
6
6
|
module ClassMethods
|
|
7
|
+
DEFAULT_TYPES = [:string, :text]
|
|
7
8
|
|
|
9
|
+
@@define_nilify_blank_methods_lock = Mutex.new
|
|
10
|
+
|
|
11
|
+
# This overrides the underlying rails method that defines attribute methods.
|
|
12
|
+
# This must be thread safe, just like the underlying method.
|
|
13
|
+
#
|
|
8
14
|
def define_attribute_methods
|
|
15
|
+
if super
|
|
16
|
+
define_nilify_blank_methods
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def inherited(child_class)
|
|
21
|
+
if instance_variable_get(:@_nilify_blanks_options) and !child_class.instance_variable_get(:@_nilify_blanks_options)
|
|
22
|
+
child_class.nilify_blanks @_nilify_blanks_options
|
|
23
|
+
end
|
|
24
|
+
|
|
9
25
|
super
|
|
10
|
-
define_nilify_blank_methods
|
|
11
26
|
end
|
|
12
27
|
|
|
13
28
|
def nilify_blanks(options = {})
|
|
14
|
-
return if
|
|
29
|
+
return if @_nilify_blanks_options
|
|
15
30
|
|
|
16
|
-
include
|
|
31
|
+
unless included_modules.include?(NilifyBlanks::InstanceMethods)
|
|
32
|
+
include NilifyBlanks::InstanceMethods
|
|
33
|
+
end
|
|
17
34
|
|
|
18
35
|
@_nilify_blanks_options = options
|
|
36
|
+
|
|
37
|
+
# Normally we wait for rails to define attribute methods, but we could be calling this after this has already been done.
|
|
38
|
+
# If so, let's just immediately generate nilify blanks methods.
|
|
39
|
+
#
|
|
40
|
+
if @attribute_methods_generated
|
|
41
|
+
define_nilify_blank_methods
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
descendants.each do |subclass|
|
|
45
|
+
subclass.nilify_blanks @_nilify_blanks_options
|
|
46
|
+
end
|
|
19
47
|
end
|
|
20
48
|
|
|
49
|
+
|
|
21
50
|
private
|
|
22
51
|
|
|
23
52
|
def define_nilify_blank_methods
|
|
24
53
|
return unless @_nilify_blanks_options
|
|
54
|
+
return if @nilify_blank_methods_generated
|
|
25
55
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
options[:except] = Array.wrap(options[:except]).map(&:to_s) if options[:except]
|
|
56
|
+
@@define_nilify_blank_methods_lock.synchronize do
|
|
57
|
+
options = @_nilify_blanks_options
|
|
29
58
|
|
|
30
|
-
|
|
59
|
+
options[:only] = Array.wrap(options[:only]).map(&:to_s) if options[:only]
|
|
60
|
+
options[:except] = Array.wrap(options[:except]).map(&:to_s) if options[:except]
|
|
61
|
+
options[:types] = options[:types] ? Array.wrap(options[:types]).map(&:to_sym) : DEFAULT_TYPES
|
|
31
62
|
|
|
32
|
-
|
|
33
|
-
self.nilify_blanks_columns = options[:only].clone
|
|
34
|
-
else
|
|
35
|
-
self.nilify_blanks_columns = self.content_columns.select(&:null).map(&:name).map(&:to_s)
|
|
36
|
-
end
|
|
37
|
-
self.nilify_blanks_columns -= options[:except] if options[:except]
|
|
38
|
-
self.nilify_blanks_columns = self.nilify_blanks_columns.map(&:to_s)
|
|
63
|
+
cattr_accessor :nilify_blanks_columns
|
|
39
64
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
65
|
+
if options[:only]
|
|
66
|
+
self.nilify_blanks_columns = options[:only].clone
|
|
67
|
+
else
|
|
68
|
+
self.nilify_blanks_columns = self.content_columns.select(&:null).select {|c| options[:types].include?(c.type) }.map(&:name).map(&:to_s)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
self.nilify_blanks_columns -= options[:except] if options[:except]
|
|
72
|
+
self.nilify_blanks_columns = self.nilify_blanks_columns.map(&:to_s)
|
|
43
73
|
|
|
74
|
+
options[:before] ||= :save
|
|
75
|
+
send("before_#{options[:before]}", :nilify_blanks)
|
|
76
|
+
@nilify_blanks_methods_generated = true
|
|
77
|
+
end
|
|
78
|
+
end
|
|
44
79
|
end
|
|
45
80
|
|
|
46
81
|
module InstanceMethods
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
rspec_module = defined?(RSpec::Core) ? 'RSpec' : 'Spec' # for RSpec 1 compatability
|
|
2
|
+
|
|
3
|
+
Kernel.const_get(rspec_module)::Matchers.define :nilify_blanks_for do |column_name, options = {}|
|
|
4
|
+
match do |model_instance|
|
|
5
|
+
model_class = model_instance.class
|
|
6
|
+
model_class.define_attribute_methods
|
|
7
|
+
model_class.included_modules.include?(NilifyBlanks::InstanceMethods) &&
|
|
8
|
+
model_class.respond_to?(:nilify_blanks_columns) &&
|
|
9
|
+
model_class.nilify_blanks_columns.include?(column_name.to_s) &&
|
|
10
|
+
options.all? {|k, v| model_class.instance_variable_get(:@_nilify_blanks_options)[k] == v }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
failure_message_for_should do |ability|
|
|
14
|
+
"expected to nilify blanks for #{column_name} #{options.inspect unless options.empty?}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
failure_message_for_should_not do |ability|
|
|
18
|
+
"expected to not nilify blanks for #{column_name} #{options.inspect unless options.empty?}"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
Kernel.const_get(rspec_module)::Matchers.define :nilify_blanks do |options = {}|
|
|
23
|
+
match do |model_instance|
|
|
24
|
+
model_class = model_instance.class
|
|
25
|
+
model_class.define_attribute_methods
|
|
26
|
+
model_class.included_modules.include?(NilifyBlanks::InstanceMethods) &&
|
|
27
|
+
model_class.respond_to?(:nilify_blanks_columns) &&
|
|
28
|
+
model_class.nilify_blanks_columns == model_class.content_columns.select(&:null).map(&:name).map(&:to_s) &&
|
|
29
|
+
options.all? {|k, v| model_class.instance_variable_get(:@_nilify_blanks_options)[k] == v }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
failure_message_for_should do |ability|
|
|
33
|
+
"expected to nilify blanks #{options.inspect unless options.empty?}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
failure_message_for_should_not do |ability|
|
|
37
|
+
"expected to not nilify blanks #{options.inspect unless options.empty?}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
data/spec/db/test.sqlite3
CHANGED
|
Binary file
|
data/spec/nilify_blanks_spec.rb
CHANGED
|
@@ -12,8 +12,8 @@ describe NilifyBlanks do
|
|
|
12
12
|
@post.save
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
it "should recognize all non-null columns" do
|
|
16
|
-
['first_name', 'title', 'summary', 'body'
|
|
15
|
+
it "should recognize all non-null string, text columns" do
|
|
16
|
+
Post.nilify_blanks_columns.should == ['first_name', 'title', 'summary', 'body']
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
it "should convert all blanks to nils" do
|
|
@@ -31,6 +31,33 @@ describe NilifyBlanks do
|
|
|
31
31
|
@post.views.should == 0
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
|
+
|
|
35
|
+
context "Model with nilify_blanks :types => [:text]" do
|
|
36
|
+
before(:all) do
|
|
37
|
+
class PostOnlyText < ActiveRecord::Base
|
|
38
|
+
self.table_name = "posts"
|
|
39
|
+
nilify_blanks :types => [:text]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
@post = PostOnlyText.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
|
43
|
+
@post.save
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "should recognize all non-null text only columns" do
|
|
47
|
+
PostOnlyText.nilify_blanks_columns.should == ['summary', 'body']
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "should convert all blanks to nils" do
|
|
51
|
+
@post.summary.should be_nil
|
|
52
|
+
@post.body.should be_nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "should leave not-null string fields alone" do
|
|
56
|
+
@post.first_name.should == ""
|
|
57
|
+
@post.last_name.should == ""
|
|
58
|
+
@post.title.should == ""
|
|
59
|
+
end
|
|
60
|
+
end
|
|
34
61
|
|
|
35
62
|
context "Model with nilify_blanks :only => [:first_name, :title]" do
|
|
36
63
|
before(:all) do
|
|
@@ -44,7 +71,7 @@ describe NilifyBlanks do
|
|
|
44
71
|
end
|
|
45
72
|
|
|
46
73
|
it "should recognize only first_name and title" do
|
|
47
|
-
['first_name', 'title']
|
|
74
|
+
PostOnlyFirstNameAndTitle.nilify_blanks_columns.should == ['first_name', 'title']
|
|
48
75
|
end
|
|
49
76
|
|
|
50
77
|
it "should convert first_name and title blanks to nils" do
|
|
@@ -70,7 +97,7 @@ describe NilifyBlanks do
|
|
|
70
97
|
end
|
|
71
98
|
|
|
72
99
|
it "should recognize only summary, body, and views" do
|
|
73
|
-
['summary', 'body'
|
|
100
|
+
PostExceptFirstNameAndTitle.nilify_blanks_columns.should == ['summary', 'body']
|
|
74
101
|
end
|
|
75
102
|
|
|
76
103
|
it "should convert summary and body blanks to nils" do
|
|
@@ -84,4 +111,69 @@ describe NilifyBlanks do
|
|
|
84
111
|
end
|
|
85
112
|
end
|
|
86
113
|
|
|
114
|
+
|
|
115
|
+
context "Global Usage" do
|
|
116
|
+
context "Namespaced Base Class with nilify_blanks inline" do
|
|
117
|
+
before(:all) do
|
|
118
|
+
module Admin1
|
|
119
|
+
class Base < ActiveRecord::Base
|
|
120
|
+
self.abstract_class = true
|
|
121
|
+
nilify_blanks
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
class Admin1::Post < Admin1::Base
|
|
126
|
+
self.table_name = "posts"
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
@post = Admin1::Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
|
130
|
+
@post.save
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it "should convert all blanks to nils" do
|
|
134
|
+
@post.first_name.should be_nil
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
context "Namespaced Base Class with nilify_blanks applied after definition" do
|
|
139
|
+
before(:all) do
|
|
140
|
+
module Admin2
|
|
141
|
+
class Base < ActiveRecord::Base
|
|
142
|
+
self.abstract_class = true
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
class Admin2::Post < Admin2::Base
|
|
147
|
+
self.table_name = "posts"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
Admin2::Base.nilify_blanks
|
|
151
|
+
|
|
152
|
+
@post = Admin2::Post.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
|
153
|
+
@post.save
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it "should convert all blanks to nils" do
|
|
157
|
+
@post.first_name.should be_nil
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
context "Namespaced Base Class with nilify_blanks applied after definition" do
|
|
162
|
+
before(:all) do
|
|
163
|
+
ActiveRecord::Base.nilify_blanks
|
|
164
|
+
|
|
165
|
+
class InheritedPost < ActiveRecord::Base
|
|
166
|
+
self.table_name = "posts"
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
@post = InheritedPost.new(:first_name => '', :last_name => '', :title => '', :summary => '', :body => '', :views => 0)
|
|
170
|
+
@post.save
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
it "should convert all blanks to nils" do
|
|
174
|
+
@post.first_name.should be_nil
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
end
|
|
87
179
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: nilify_blanks
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
|
+
prerelease:
|
|
5
6
|
platform: ruby
|
|
6
7
|
authors:
|
|
7
8
|
- Ben Hughes
|
|
8
9
|
autorequire:
|
|
9
10
|
bindir: bin
|
|
10
11
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
+
date: 2014-07-04 00:00:00.000000000 Z
|
|
12
13
|
dependencies:
|
|
13
14
|
- !ruby/object:Gem::Dependency
|
|
14
15
|
name: activesupport
|
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
16
18
|
requirements:
|
|
17
19
|
- - ! '>='
|
|
18
20
|
- !ruby/object:Gem::Version
|
|
@@ -20,6 +22,7 @@ dependencies:
|
|
|
20
22
|
type: :runtime
|
|
21
23
|
prerelease: false
|
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
23
26
|
requirements:
|
|
24
27
|
- - ! '>='
|
|
25
28
|
- !ruby/object:Gem::Version
|
|
@@ -27,6 +30,7 @@ dependencies:
|
|
|
27
30
|
- !ruby/object:Gem::Dependency
|
|
28
31
|
name: activerecord
|
|
29
32
|
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
30
34
|
requirements:
|
|
31
35
|
- - ! '>='
|
|
32
36
|
- !ruby/object:Gem::Version
|
|
@@ -34,6 +38,7 @@ dependencies:
|
|
|
34
38
|
type: :runtime
|
|
35
39
|
prerelease: false
|
|
36
40
|
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
37
42
|
requirements:
|
|
38
43
|
- - ! '>='
|
|
39
44
|
- !ruby/object:Gem::Version
|
|
@@ -41,6 +46,7 @@ dependencies:
|
|
|
41
46
|
- !ruby/object:Gem::Dependency
|
|
42
47
|
name: rake
|
|
43
48
|
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
44
50
|
requirements:
|
|
45
51
|
- - ~>
|
|
46
52
|
- !ruby/object:Gem::Version
|
|
@@ -48,6 +54,7 @@ dependencies:
|
|
|
48
54
|
type: :development
|
|
49
55
|
prerelease: false
|
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
51
58
|
requirements:
|
|
52
59
|
- - ~>
|
|
53
60
|
- !ruby/object:Gem::Version
|
|
@@ -55,6 +62,7 @@ dependencies:
|
|
|
55
62
|
- !ruby/object:Gem::Dependency
|
|
56
63
|
name: rspec
|
|
57
64
|
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
none: false
|
|
58
66
|
requirements:
|
|
59
67
|
- - ~>
|
|
60
68
|
- !ruby/object:Gem::Version
|
|
@@ -62,6 +70,7 @@ dependencies:
|
|
|
62
70
|
type: :development
|
|
63
71
|
prerelease: false
|
|
64
72
|
version_requirements: !ruby/object:Gem::Requirement
|
|
73
|
+
none: false
|
|
65
74
|
requirements:
|
|
66
75
|
- - ~>
|
|
67
76
|
- !ruby/object:Gem::Version
|
|
@@ -69,6 +78,7 @@ dependencies:
|
|
|
69
78
|
- !ruby/object:Gem::Dependency
|
|
70
79
|
name: appraisal
|
|
71
80
|
requirement: !ruby/object:Gem::Requirement
|
|
81
|
+
none: false
|
|
72
82
|
requirements:
|
|
73
83
|
- - ~>
|
|
74
84
|
- !ruby/object:Gem::Version
|
|
@@ -76,6 +86,7 @@ dependencies:
|
|
|
76
86
|
type: :development
|
|
77
87
|
prerelease: false
|
|
78
88
|
version_requirements: !ruby/object:Gem::Requirement
|
|
89
|
+
none: false
|
|
79
90
|
requirements:
|
|
80
91
|
- - ~>
|
|
81
92
|
- !ruby/object:Gem::Version
|
|
@@ -83,6 +94,7 @@ dependencies:
|
|
|
83
94
|
- !ruby/object:Gem::Dependency
|
|
84
95
|
name: sqlite3
|
|
85
96
|
requirement: !ruby/object:Gem::Requirement
|
|
97
|
+
none: false
|
|
86
98
|
requirements:
|
|
87
99
|
- - ~>
|
|
88
100
|
- !ruby/object:Gem::Version
|
|
@@ -90,6 +102,7 @@ dependencies:
|
|
|
90
102
|
type: :development
|
|
91
103
|
prerelease: false
|
|
92
104
|
version_requirements: !ruby/object:Gem::Requirement
|
|
105
|
+
none: false
|
|
93
106
|
requirements:
|
|
94
107
|
- - ~>
|
|
95
108
|
- !ruby/object:Gem::Version
|
|
@@ -102,6 +115,7 @@ executables: []
|
|
|
102
115
|
extensions: []
|
|
103
116
|
extra_rdoc_files: []
|
|
104
117
|
files:
|
|
118
|
+
- lib/nilify_blanks/matchers.rb
|
|
105
119
|
- lib/nilify_blanks/railtie.rb
|
|
106
120
|
- lib/nilify_blanks/version.rb
|
|
107
121
|
- lib/nilify_blanks.rb
|
|
@@ -120,26 +134,27 @@ files:
|
|
|
120
134
|
- init.rb
|
|
121
135
|
homepage: http://github.com/rubiety/nilify_blanks
|
|
122
136
|
licenses: []
|
|
123
|
-
metadata: {}
|
|
124
137
|
post_install_message:
|
|
125
138
|
rdoc_options: []
|
|
126
139
|
require_paths:
|
|
127
140
|
- lib
|
|
128
141
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
142
|
+
none: false
|
|
129
143
|
requirements:
|
|
130
144
|
- - ! '>='
|
|
131
145
|
- !ruby/object:Gem::Version
|
|
132
146
|
version: '0'
|
|
133
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
|
+
none: false
|
|
134
149
|
requirements:
|
|
135
150
|
- - ! '>='
|
|
136
151
|
- !ruby/object:Gem::Version
|
|
137
152
|
version: 1.3.4
|
|
138
153
|
requirements: []
|
|
139
154
|
rubyforge_project: nilify_blanks
|
|
140
|
-
rubygems_version:
|
|
155
|
+
rubygems_version: 1.8.23
|
|
141
156
|
signing_key:
|
|
142
|
-
specification_version:
|
|
157
|
+
specification_version: 3
|
|
143
158
|
summary: Auto-convert blank fields to nil.
|
|
144
159
|
test_files: []
|
|
145
160
|
has_rdoc:
|
checksums.yaml
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
!binary "U0hBMQ==":
|
|
3
|
-
metadata.gz: !binary |-
|
|
4
|
-
Yzk0ODgzYTlhYmJhYThhNDllNWM1MjRlMjFhY2Q2ZDMwMGQ1MGZiMQ==
|
|
5
|
-
data.tar.gz: !binary |-
|
|
6
|
-
ZGIxMGZlOGEyNGUyODdmZjExYTkwODYzZDRkYjY1YmYyOTI1MTFjYQ==
|
|
7
|
-
!binary "U0hBNTEy":
|
|
8
|
-
metadata.gz: !binary |-
|
|
9
|
-
NWYwYjA5M2I4ZGFmMTk3ZjViOTg2ZTBjODVjNjVjMGFkZmU4OGRmM2RiNmM3
|
|
10
|
-
NGIxYjYzN2EyMjM1ZTlhNGQ0NzQwZDUwNzQ5ZGM0NDU3NzYzNjUwM2M4YmNi
|
|
11
|
-
OTQwYTkwMmNiYTA3Y2MyY2QyM2M5OGU5NTNmZDEwM2VhMzM4MGI=
|
|
12
|
-
data.tar.gz: !binary |-
|
|
13
|
-
ZTM5NjIxYTc5MTNiODQ5MTc1ZWE5MTNhM2IxYmZhYjNmYzZiZmRkMmM3Yzlh
|
|
14
|
-
NDZiMjc1OWZhZDcwNGMzNDc1NWNiNzMyNTg5ZTRhNGU3MGNjYzI2ODk3OWVi
|
|
15
|
-
MzMzOWNkOGRmNGYzNzkzYWI0MzIwZWI3Y2E0ZTNkNmE3ODkzOWQ=
|