Skip to content

Commit

Permalink
Fix #66; remove allow_nil
Browse files Browse the repository at this point in the history
Replaced "allow_nil: true" with "default: nil".
  • Loading branch information
tfausak committed Nov 13, 2013
1 parent 877c2be commit e963e0c
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -56,7 +56,7 @@ class UserSignup < ActiveInteraction::Base
string :email, :name

# optional
boolean :newsletter_subscribe, allow_nil: true
boolean :newsletter_subscribe, default: nil

# ActiveRecord validations
validates :email, format: EMAIL_REGEX
Expand Down Expand Up @@ -161,10 +161,10 @@ end
integer :age
boolean :is_special
model :account
array :tags, allow_nil: true do
array :tags, default: nil do
string
end
hash :prefs, allow_nil: true do
hash :prefs, default: nil do
boolean :smoking
boolean :view
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_interaction/base.rb
Expand Up @@ -15,7 +15,7 @@ module ActiveInteraction
# integer :a, :b
#
# # Optional
# integer :c, allow_nil: true
# integer :c, default: nil
#
# def execute
# sum = a + b
Expand Down
5 changes: 1 addition & 4 deletions lib/active_interaction/caster.rb
Expand Up @@ -2,7 +2,6 @@ module ActiveInteraction
# @!macro [new] attribute_method_params
# @param *attributes [Symbol] One or more attributes to create.
# @param options [Hash]
# @option options [Boolean] :allow_nil Allow a `nil` value.
# @option options [Object] :default Value to use if `nil` is given.

# @private
Expand All @@ -19,9 +18,7 @@ def self.factory(type)
def self.prepare(filter, value)
case value
when NilClass
if filter.options[:allow_nil]
nil
elsif filter.options.has_key?(:default)
if filter.options.has_key?(:default)
# REVIEW: This value isn't actually used anywhere. It is required
# to make the validator (Validation.validate) happy.
filter.options[:default]
Expand Down
2 changes: 1 addition & 1 deletion lib/active_interaction/casters/array_caster.rb
Expand Up @@ -16,7 +16,7 @@ class Base
#
# @example An Array of Integers where some or all are nil
# array :ids do
# integer allow_nil: true
# integer default: nil
# end
#
# @method self.array(*attributes, options = {}, &block)
Expand Down
2 changes: 1 addition & 1 deletion spec/support/casters.rb
Expand Up @@ -26,7 +26,7 @@
end

context 'optional' do
before { options.merge!(allow_nil: true) }
before { options.merge!(default: nil) }

context 'with nil' do
it 'returns nil' do
Expand Down
9 changes: 1 addition & 8 deletions spec/support/interactions.rb
Expand Up @@ -19,10 +19,8 @@ def execute
let(:described_class) do
Class.new(TestInteraction) do
send(type, :required, filter_options)
send(type, :optional, filter_options.merge(allow_nil: true))
send(type, :optional, filter_options.merge(default: nil))
send(type, :default, filter_options.merge(default: generator.call))
send(type, :nil_default,
filter_options.merge(allow_nil: true, default: nil))
send(type, :defaults_1, :defaults_2,
filter_options.merge(default: generator.call))

Expand All @@ -31,7 +29,6 @@ def execute
required: required,
optional: optional,
default: default,
nil_default: nil_default,
defaults_1: defaults_1,
defaults_2: defaults_2
}
Expand Down Expand Up @@ -71,10 +68,6 @@ def execute
expect(result[:default]).to_not be_nil
end

it 'returns nil for :nil_default' do
expect(result[:nil_default]).to be_nil
end

it 'does not return nil for :defaults_1' do
expect(result[:defaults_1]).to_not be_nil
end
Expand Down

0 comments on commit e963e0c

Please sign in to comment.