From e963e0cd4934e9c1e3161a03bcce1ae1d3ca08dd Mon Sep 17 00:00:00 2001 From: Taylor Fausak Date: Tue, 12 Nov 2013 12:33:04 -0600 Subject: [PATCH] Fix #66; remove allow_nil Replaced "allow_nil: true" with "default: nil". --- README.md | 6 +++--- lib/active_interaction/base.rb | 2 +- lib/active_interaction/caster.rb | 5 +---- lib/active_interaction/casters/array_caster.rb | 2 +- spec/support/casters.rb | 2 +- spec/support/interactions.rb | 9 +-------- 6 files changed, 8 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 7c44fd5b..e48037e5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/lib/active_interaction/base.rb b/lib/active_interaction/base.rb index 7ead968d..7d5c6190 100644 --- a/lib/active_interaction/base.rb +++ b/lib/active_interaction/base.rb @@ -15,7 +15,7 @@ module ActiveInteraction # integer :a, :b # # # Optional - # integer :c, allow_nil: true + # integer :c, default: nil # # def execute # sum = a + b diff --git a/lib/active_interaction/caster.rb b/lib/active_interaction/caster.rb index 0e51b842..2cf4ef0a 100644 --- a/lib/active_interaction/caster.rb +++ b/lib/active_interaction/caster.rb @@ -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 @@ -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] diff --git a/lib/active_interaction/casters/array_caster.rb b/lib/active_interaction/casters/array_caster.rb index b912fcd2..12802b30 100644 --- a/lib/active_interaction/casters/array_caster.rb +++ b/lib/active_interaction/casters/array_caster.rb @@ -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) diff --git a/spec/support/casters.rb b/spec/support/casters.rb index 1cc13ebd..bc0530a7 100644 --- a/spec/support/casters.rb +++ b/spec/support/casters.rb @@ -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 diff --git a/spec/support/interactions.rb b/spec/support/interactions.rb index c96a5838..6dba57d3 100644 --- a/spec/support/interactions.rb +++ b/spec/support/interactions.rb @@ -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)) @@ -31,7 +29,6 @@ def execute required: required, optional: optional, default: default, - nil_default: nil_default, defaults_1: defaults_1, defaults_2: defaults_2 } @@ -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