flex_columns 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6beca81659e3d23049cba155f6991641af01d31e
4
- data.tar.gz: 5c476c72d15bd39871ae3e46bbab7b9d9b66d40c
3
+ metadata.gz: 77ffdb1f8ef333becb22e7ee81a54d32be00e4ce
4
+ data.tar.gz: 33e7afb514f21decc7fb4711f28c908a0e7e247b
5
5
  SHA512:
6
- metadata.gz: e33340f20bc94ab8aa1e097a0533ea6e03ad019fe53f9dfee95dbf78997f3a450bb24f72118278f38cee8822bb4280398a9bd5d8439392f829e51c6de503dfc2
7
- data.tar.gz: 08aeb48a67d3b06146e9ccf1a39e210d55e0ade0f4a82ddbad1f7303d21cfe409831513bfc4b11533f7f418540f045ae362828ab10cc8f2d89ae6e421838382a
6
+ metadata.gz: 7d64c5801c2d5765463776a89ff56a85a5384f69b94917a18c2e1816d3fd9853d77bf4b81bbdd62a270e36f157a9c7852f6efda7730a9f6d2225b05b4d3ec4a4
7
+ data.tar.gz: 9f32a575764ad59cd86fbe4479a7f6b59e7b5fc5f04dc0632c86d7b0b227f40d68e77c541fef9e8ec7fc1c97e25b667571009584ad1d2e22961ad2a6b5cc62db
data/CHANGES.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # `flex_columns` Changelog
2
2
 
3
+ ## 1.0.5, 2014-04-03
4
+
5
+ * Fixed an issue where boolean fields would fail to validate if `:null => false` was passed and their value was `false`.
6
+ * Fixed an issue where integer fields would fail to validate if `nil` was allowed (that is, `:null => false` was _not_ passed) and yet `nil` was stored in them.
7
+
3
8
  ## 1.0.4, 2014-03-31
4
9
 
5
10
  * Fixed an incompatibility with Rails 4.0.3 or 4.0.4 due to a change in the way ActiveRecord::Base#method missing works. The way we were handling this (by double-implementing `_flex_column_object_for` and trying to use `super` to delegate one to the other) was pretty gross, anyway; this fix is much more solid.
@@ -206,12 +206,12 @@ module FlexColumns
206
206
  # Given any additional arguments after the name of the field (e.g., <tt>field :foo, :integer</tt>), apply them
207
207
  # as appropriate. Currently, the only kind of accepted additional argument is a type.
208
208
  def apply_additional_arguments(additional_arguments)
209
- type = additional_arguments.shift
210
- if type
209
+ @type = additional_arguments.shift
210
+ if @type
211
211
  begin
212
- send("apply_validations_for_#{type}")
212
+ send("apply_validations_for_#{@type}")
213
213
  rescue NoMethodError => nme
214
- raise ArgumentError, "Unknown type: #{type.inspect}"
214
+ raise ArgumentError, "Unknown type: #{@type.inspect}"
215
215
  end
216
216
  end
217
217
 
@@ -220,10 +220,16 @@ module FlexColumns
220
220
  end
221
221
  end
222
222
 
223
+ def not_nullable?
224
+ options.has_key?(:null) && (! options[:null])
225
+ end
226
+
223
227
  # Apply the correct validations for a field of type :integer. (Called from #apply_additional_arguments via
224
228
  # metaprogramming.)
225
229
  def apply_validations_for_integer
226
- flex_column_class.validates field_name, :numericality => { :only_integer => true }
230
+ options = { :numericality => { :only_integer => true } }
231
+ options[:allow_nil] = true unless not_nullable?
232
+ flex_column_class.validates field_name, options
227
233
  end
228
234
 
229
235
  # Apply the correct validations for a field of type :string. (Called from #apply_additional_arguments via
@@ -285,13 +291,19 @@ module FlexColumns
285
291
  # Apply the correct validations for a field of type :boolean. (Called from #apply_additional_arguments via
286
292
  # metaprogramming.)
287
293
  def apply_validations_for_boolean
288
- flex_column_class.validates field_name, :inclusion => { :in => [ true, false, nil ] }
294
+ value_set = [ true, false ]
295
+ value_set << nil unless not_nullable?
296
+ flex_column_class.validates field_name, :inclusion => { :in => value_set }
297
+ end
298
+
299
+ def skip_not_nullable_validation_due_to_type?
300
+ [ :boolean ].include?(@type)
289
301
  end
290
302
 
291
303
  # Applies any validations resulting from options to this class (but not types; they're handled by
292
304
  # #apply_additional_arguments, above). Currently, this applies validations for +:null+, +:enum+, and +:limit+.
293
305
  def apply_validations!
294
- if options.has_key?(:null) && (! options[:null])
306
+ if not_nullable? && (! skip_not_nullable_validation_due_to_type?)
295
307
  flex_column_class.validates field_name, :presence => true
296
308
  end
297
309
 
@@ -1,4 +1,4 @@
1
1
  module FlexColumns
2
2
  # The current version of FlexColumns.
3
- VERSION = "1.0.4"
3
+ VERSION = "1.0.5"
4
4
  end
@@ -18,6 +18,7 @@ describe "FlexColumns basic operations" do
18
18
  def should_fail_validation(field, value, pattern = nil)
19
19
  object = ::User.new
20
20
  object.some_integer = 123
21
+ object.some_boolean_that_is_not_nullable = false
21
22
 
22
23
  object.send("#{field}=", value)
23
24
 
@@ -46,6 +47,9 @@ describe "FlexColumns basic operations" do
46
47
  field :some_timestamp, :timestamp
47
48
  field :some_boolean, :boolean
48
49
  field :some_enum, :enum => [ 'foo', 'bar', 'baz', nil ]
50
+
51
+ field :some_integer_that_is_nullable, :integer
52
+ field :some_boolean_that_is_not_nullable, :boolean, :null => false
49
53
  end
50
54
  end
51
55
 
@@ -60,6 +64,8 @@ describe "FlexColumns basic operations" do
60
64
  should_fail_validation(:some_timestamp, "foo", /must be a Time/i)
61
65
  should_fail_validation(:some_boolean, "true", /is not included in the list/i)
62
66
  should_fail_validation(:some_enum, "quux", /is not included in the list/i)
67
+ should_fail_validation(:some_integer_that_is_nullable, "quux", /is not a number/i)
68
+ should_fail_validation(:some_boolean_that_is_not_nullable, nil, /is not included in the list/i)
63
69
 
64
70
  user = ::User.new
65
71
  user.name = 'User 1'
@@ -75,6 +81,7 @@ describe "FlexColumns basic operations" do
75
81
  user.user_attributes.some_timestamp = 1.minute.from_now
76
82
  user.user_attributes.some_boolean = true
77
83
  user.user_attributes.some_enum = 'foo'
84
+ user.user_attributes.some_boolean_that_is_not_nullable = false
78
85
 
79
86
  user.valid?.should be
80
87
 
@@ -89,5 +96,8 @@ describe "FlexColumns basic operations" do
89
96
 
90
97
  user.user_attributes.some_string = :bonk
91
98
  user.valid?.should be
99
+
100
+ user.user_attributes.some_boolean = false
101
+ user.valid?.should be
92
102
  end
93
103
  end
@@ -40,7 +40,7 @@ describe FlexColumns::Definition::FieldDefinition do
40
40
  end
41
41
 
42
42
  it "should raise an error if there are additional arguments" do
43
- expect(@flex_column_class).to receive(:validates).once.with(:foo, { :numericality => { :only_integer => true }})
43
+ expect(@flex_column_class).to receive(:validates).once.with(:foo, { :numericality => { :only_integer => true }, :allow_nil => true})
44
44
  lambda { klass.new(@flex_column_class, :foo, [ :integer, :bar ], { }) }.should raise_error(ArgumentError)
45
45
  end
46
46
 
@@ -70,7 +70,7 @@ describe FlexColumns::Definition::FieldDefinition do
70
70
  end
71
71
 
72
72
  it "should validate integers properly" do
73
- expect_validation(:integer, { :numericality => { :only_integer => true } })
73
+ expect_validation(:integer, { :numericality => { :only_integer => true }, :allow_nil => true })
74
74
  end
75
75
 
76
76
  it "should validate floats properly" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flex_columns
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Geweke
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2014-03-31 00:00:00 Z
12
+ date: 2014-04-04 00:00:00 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json