Skip to content

Commit

Permalink
let date, datetime, and time accept implicit strings
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Jan 10, 2021
1 parent b978181 commit a9cd4b0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 4 additions & 5 deletions lib/active_interaction/filters/abstract_date_time_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/active_interaction/filters/date_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
14 changes: 14 additions & 0 deletions spec/active_interaction/filters/date_time_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
14 changes: 14 additions & 0 deletions spec/active_interaction/filters/time_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit a9cd4b0

Please sign in to comment.