From a9cd4b0263e99caf622fdcd2bb02933755efe303 Mon Sep 17 00:00:00 2001 From: Aaron Lasseigne Date: Wed, 14 Jun 2017 17:28:50 -0500 Subject: [PATCH] let date, datetime, and time accept implicit strings --- CHANGELOG.md | 3 +++ .../filters/abstract_date_time_filter.rb | 9 ++++----- .../active_interaction/filters/date_filter_spec.rb | 14 ++++++++++++++ .../filters/date_time_filter_spec.rb | 14 ++++++++++++++ .../active_interaction/filters/time_filter_spec.rb | 14 ++++++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da185038..9598fc2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,12 +8,15 @@ hash. The `ActiveInteraction::Input` still responds to all hash methods. - Implicit types are now supported for many filters: - `array` accepts `to_ary` + - `date` accepts `to_str` + - `datetime` accepts `to_str` - `decimal` accepts `to_int` - `float` accepts `to_int` - `hash` accepts `to_hash` - `integer` accepts `to_int` - `string` accepts `to_str` - `symbol` accepts `to_sym` + - `time` accepts `to_str` ## Upgrading diff --git a/lib/active_interaction/filters/abstract_date_time_filter.rb b/lib/active_interaction/filters/abstract_date_time_filter.rb index 5f826f5f..df21abcd 100644 --- a/lib/active_interaction/filters/abstract_date_time_filter.rb +++ b/lib/active_interaction/filters/abstract_date_time_filter.rb @@ -12,12 +12,11 @@ class AbstractDateTimeFilter < AbstractFilter private :_cast # rubocop:disable Style/AccessModifierDeclarations def cast(value, context) - case value - when String - convert(value, context) - when GroupedInput + if value.respond_to?(:to_str) + convert(value.to_str, context) + elsif value.is_a?(GroupedInput) convert(stringify(value), context) - when *klasses + elsif klasses.include?(value.class) value else super diff --git a/spec/active_interaction/filters/date_filter_spec.rb b/spec/active_interaction/filters/date_filter_spec.rb index 278dec42..8a3f3cbf 100644 --- a/spec/active_interaction/filters/date_filter_spec.rb +++ b/spec/active_interaction/filters/date_filter_spec.rb @@ -61,6 +61,20 @@ end end + context 'with an implicit String' do + let(:value) do + Class.new do + def to_str + '2011-12-13' + end + end.new + end + + it 'returns a Date' do + expect(result).to eql Date.parse(value) + end + end + context 'with a GroupedInput' do let(:year) { 2012 } let(:month) { 1 } diff --git a/spec/active_interaction/filters/date_time_filter_spec.rb b/spec/active_interaction/filters/date_time_filter_spec.rb index 60f2d41e..d1ff45ec 100644 --- a/spec/active_interaction/filters/date_time_filter_spec.rb +++ b/spec/active_interaction/filters/date_time_filter_spec.rb @@ -61,6 +61,20 @@ end end + context 'with an implicit String' do + let(:value) do + Class.new do + def to_str + '2011-12-13T14:15:16+17:18' + end + end.new + end + + it 'returns a DateTime' do + expect(result).to eql DateTime.parse(value) + end + end + context 'with a GroupedInput' do let(:year) { 2012 } let(:month) { 1 } diff --git a/spec/active_interaction/filters/time_filter_spec.rb b/spec/active_interaction/filters/time_filter_spec.rb index df84623b..d7a76a04 100644 --- a/spec/active_interaction/filters/time_filter_spec.rb +++ b/spec/active_interaction/filters/time_filter_spec.rb @@ -83,6 +83,20 @@ end end + context 'with an implicit String' do + let(:value) do + Class.new do + def to_str + '2011-12-13 14:15:16 +1718' + end + end.new + end + + it 'returns a Time' do + expect(result).to eql Time.parse(value) + end + end + context 'with a GroupedInput' do let(:year) { 2012 } let(:month) { 1 }