diff --git a/lib/active_interaction/filters/integer_filter.rb b/lib/active_interaction/filters/integer_filter.rb index df9f4bf8..717e3169 100644 --- a/lib/active_interaction/filters/integer_filter.rb +++ b/lib/active_interaction/filters/integer_filter.rb @@ -17,5 +17,18 @@ class Base # @private class IntegerFilter < AbstractNumericFilter register :integer + + private + + # @return [Integer] + def base + options.fetch(:base, 0) + end + + def convert(value, context) + Integer(value, base) + rescue ArgumentError + _cast(value, context) + end end end diff --git a/spec/active_interaction/filters/integer_filter_spec.rb b/spec/active_interaction/filters/integer_filter_spec.rb index 2e2a4804..1ea1c7f3 100644 --- a/spec/active_interaction/filters/integer_filter_spec.rb +++ b/spec/active_interaction/filters/integer_filter_spec.rb @@ -42,6 +42,14 @@ end.to raise_error ActiveInteraction::InvalidValueError end end + + it 'supports different bases' do + expect(filter.cast('07', nil)).to eql 7 + expect do + filter.cast('08', nil) + end.to raise_error ActiveInteraction::InvalidValueError + expect(described_class.new(name, base: 10).cast('08', nil)).to eql 8 + end end describe '#database_column_type' do