Skip to content

Commit

Permalink
accept implicit int type for time filter
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Jan 10, 2021
1 parent 61cb815 commit 5556aea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
- [#392][] - Integer parsing now defaults the base to 10. ([how to upgrade](#integer-parsing-base-now-10))
- The `inputs` method now returns an `ActiveInteraction::Input` instead of a
hash. The `ActiveInteraction::Input` still responds to all hash methods.
- Implicit types are now supported for many filters:

## Added

- Implicit types are now supported in filters:
- `array` accepts `to_ary`
- `date` accepts `to_str`
- `datetime` accepts `to_str`
Expand All @@ -16,7 +19,7 @@
- `integer` accepts `to_int`
- `string` accepts `to_str`
- `symbol` accepts `to_sym`
- `time` accepts `to_str`
- `time` accepts `to_str` and `to_int`

## Upgrading

Expand Down
5 changes: 3 additions & 2 deletions lib/active_interaction/filters/time_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def initialize(name, options = {}, &block)
end

def cast(value, _interaction)
case value
when Numeric
value = value.to_int if value.respond_to?(:to_int)

if value.is_a?(Numeric)
klass.at(value)
else
super
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 @@ -105,6 +105,20 @@ def to_str
end
end

context 'with an implicit Integer' do
let(:value) do
Class.new do
def to_int
@to_int ||= rand(1 << 16)
end
end.new
end

it 'returns the Time' do
expect(result).to eql Time.at(value)
end
end

context 'with a GroupedInput' do
let(:year) { 2012 }
let(:month) { 1 }
Expand Down

0 comments on commit 5556aea

Please sign in to comment.