Skip to content

Commit

Permalink
Merge pull request #251 from orgsync/activerecordable
Browse files Browse the repository at this point in the history
Split our ActiveRecord related methods
  • Loading branch information
AaronLasseigne committed Jan 29, 2015
2 parents e8e9581 + 420388c commit 24e4d59
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 50 deletions.
1 change: 1 addition & 0 deletions lib/active_interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'active_interaction/errors'

require 'active_interaction/concerns/active_modelable'
require 'active_interaction/concerns/active_recordable'
require 'active_interaction/concerns/hashable'
require 'active_interaction/concerns/missable'
require 'active_interaction/concerns/transactable'
Expand Down
26 changes: 1 addition & 25 deletions lib/active_interaction/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module ActiveInteraction
# end
class Base
include ActiveModelable
include ActiveRecordable
include Runnable

define_callbacks :type_check
Expand Down Expand Up @@ -181,31 +182,6 @@ def initialize(inputs = {})
process_inputs(inputs.symbolize_keys)
end

# Returns the column object for the named filter.
#
# @param name [Symbol] The name of a filter.
#
# @example
# class Interaction < ActiveInteraction::Base
# string :email, default: nil
#
# def execute; end
# end
#
# Interaction.new.column_for_attribute(:email)
# # => #<ActiveInteraction::FilterColumn:0x007faebeb2a6c8 @type=:string>
#
# Interaction.new.column_for_attribute(:not_a_filter)
# # => nil
#
# @return [FilterColumn, nil]
#
# @since 1.2.0
def column_for_attribute(name)
filter = self.class.filters[name]
FilterColumn.intern(filter.database_column_type) if filter
end

# @!method compose(other, inputs = {})
# Run another interaction and return its result. If the other interaction
# fails, halt execution.
Expand Down
33 changes: 33 additions & 0 deletions lib/active_interaction/concerns/active_recordable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# coding: utf-8

module ActiveInteraction
# Implement the minimal ActiveRecord interface.
#
# @private
module ActiveRecordable
# Returns the column object for the named filter.
#
# @param name [Symbol] The name of a filter.
#
# @example
# class Interaction < ActiveInteraction::Base
# string :email, default: nil
#
# def execute; end
# end
#
# Interaction.new.column_for_attribute(:email)
# # => #<ActiveInteraction::FilterColumn:0x007faebeb2a6c8 @type=:string>
#
# Interaction.new.column_for_attribute(:not_a_filter)
# # => nil
#
# @return [FilterColumn, nil]
#
# @since 1.2.0
def column_for_attribute(name)
filter = self.class.filters[name]
FilterColumn.intern(filter.database_column_type) if filter
end
end
end
25 changes: 0 additions & 25 deletions spec/active_interaction/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,31 +313,6 @@ def execute
end
end

describe '#column_for_attribute(name)' do
let(:described_class) { InteractionWithFilter }
let(:column) { outcome.column_for_attribute(name) }

context 'name is not an input name' do
let(:name) { SecureRandom.hex }

it 'returns nil if the attribute cannot be found' do
expect(column).to be_nil
end
end

context 'name is an input name' do
let(:name) { InteractionWithFilter.filters.keys.first }

it 'returns a FilterColumn' do
expect(column).to be_a ActiveInteraction::FilterColumn
end

it 'returns a FilterColumn of type boolean' do
expect(column.type).to eql :float
end
end
end

describe '#inputs' do
let(:described_class) { InteractionWithFilter }
let(:other_val) { SecureRandom.hex }
Expand Down
36 changes: 36 additions & 0 deletions spec/active_interaction/concerns/active_recordable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# coding: utf-8

require 'spec_helper'

InteractionWithFilter = Class.new(TestInteraction) do
float :thing
end

describe ActiveInteraction::ActiveRecordable do
include_context 'interactions'

describe '#column_for_attribute(name)' do
let(:described_class) { InteractionWithFilter }
let(:column) { outcome.column_for_attribute(name) }

context 'name is not an input name' do
let(:name) { SecureRandom.hex }

it 'returns nil if the attribute cannot be found' do
expect(column).to be_nil
end
end

context 'name is an input name' do
let(:name) { described_class.filters.keys.first }

it 'returns a FilterColumn' do
expect(column).to be_a ActiveInteraction::FilterColumn
end

it 'returns a FilterColumn of type boolean' do
expect(column.type).to eql :float
end
end
end
end

0 comments on commit 24e4d59

Please sign in to comment.