Skip to content

Commit

Permalink
make filters more self contained by adding column types
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Apr 4, 2014
1 parent 83d6aa5 commit 1d7920d
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 37 deletions.
2 changes: 1 addition & 1 deletion lib/active_interaction/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def initialize(inputs = {})

def column_for_attribute(name)
filter = self.class.filters[name]
FilterColumn.new(filter.class.slug) if filter
FilterColumn.new(filter.database_column_type) if filter
end

# @!method compose(other, inputs = {})
Expand Down
5 changes: 0 additions & 5 deletions lib/active_interaction/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ module ActiveInteraction
# @return [Class]
InvalidFilterError = Class.new(Error)

# Raised if a filter has an invalid definition.
#
# @return [Class]
InvalidFilterColumnError = Class.new(Error)

# Raised if an interaction is invalid.
#
# @return [Class]
Expand Down
4 changes: 4 additions & 0 deletions lib/active_interaction/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def cast(value)
end
end

def database_column_type
self.class.slug
end

private

# @return [Object]
Expand Down
19 changes: 1 addition & 18 deletions lib/active_interaction/filter_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,10 @@
module ActiveInteraction
#
class FilterColumn
TYPE_MAPPING = {
array: :string,
boolean: :boolean,
date: :date,
date_time: :datetime,
file: :file,
float: :float,
hash: :string,
integer: :integer,
model: :string,
string: :string,
symbol: :string,
time: :datetime
}.freeze

attr_reader :limit, :type

def initialize(type)
@type = TYPE_MAPPING.fetch(type) do
fail InvalidFilterColumnError, "#{type} is not a valid type"
end
@type = type
end

def number?
Expand Down
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/array_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def cast(value)
end
end

def database_column_type
:string
end

def method_missing(*, &block)
super do |klass, names, options|
filter = klass.new(name, options, &block)
Expand Down
3 changes: 3 additions & 0 deletions lib/active_interaction/filters/date_time_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ class Base

# @private
class DateTimeFilter < AbstractDateTimeFilter
def database_column_type
:datetime
end
end
end
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/hash_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def cast(value)
end
end

def database_column_type
:string
end

def method_missing(*args, &block)
super(*args) do |klass, names, options|
fail InvalidFilterError, 'missing attribute name' if names.empty?
Expand Down
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/model_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def cast(value, reconstantize = true)
end
end

def database_column_type
:string
end

private

# @return [Class]
Expand Down
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/symbol_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ def cast(value)
super
end
end

def database_column_type
:string
end
end
end
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/time_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def cast(value)
end
end

def database_column_type
:datetime
end

private

def klass
Expand Down
20 changes: 7 additions & 13 deletions spec/active_interaction/filter_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@
require 'spec_helper'

describe ActiveInteraction::FilterColumn do
let(:type) { :float }
subject(:column) { described_class.new(type) }
let(:type) { described_class::TYPE_MAPPING.keys.first }

describe '.new' do
context 'type is not in TYPE_MAPPING' do
let(:type) { SecureRandom.hex }

it 'fails' do
expect do
column
end.to raise_error ActiveInteraction::InvalidFilterColumnError
end
end
end

describe '#limit' do
it 'returns nil' do
expect(column.limit).to be_nil
end
end

describe '#type' do
it 'returns the type' do
expect(column.type).to eql type
end
end

describe '#number?' do
let(:number?) { column.number? }

Expand Down
6 changes: 6 additions & 0 deletions spec/active_interaction/filters/array_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@
end
end
end

describe '#database_column_type' do
it 'returns :string' do
expect(filter.database_column_type).to eql :string
end
end
end
6 changes: 6 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,4 +61,10 @@
end
end
end

describe '#database_column_type' do
it 'returns :datetime' do
expect(filter.database_column_type).to eql :datetime
end
end
end
6 changes: 6 additions & 0 deletions spec/active_interaction/filters/hash_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@
end
end
end

describe '#database_column_type' do
it 'returns :string' do
expect(filter.database_column_type).to eql :string
end
end
end
6 changes: 6 additions & 0 deletions spec/active_interaction/filters/model_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ def self.name
end
end
end

describe '#database_column_type' do
it 'returns :string' do
expect(filter.database_column_type).to eql :string
end
end
end
6 changes: 6 additions & 0 deletions spec/active_interaction/filters/symbol_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@
end
end
end

describe '#database_column_type' do
it 'returns :string' do
expect(filter.database_column_type).to eql :string
end
end
end
6 changes: 6 additions & 0 deletions spec/active_interaction/filters/time_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@
end
end
end

describe '#database_column_type' do
it 'returns :datetime' do
expect(filter.database_column_type).to eql :datetime
end
end
end
6 changes: 6 additions & 0 deletions spec/support/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,10 @@
expect(filter.options).to eq options
end
end

describe '#database_column_type' do
it 'returns a symbol' do
expect(filter.database_column_type).to be_a Symbol
end
end
end

0 comments on commit 1d7920d

Please sign in to comment.