Permalink
Browse files

Take state column for AR persistance out of class variables and add t…

…hem to the

StateMachine object.
  • Loading branch information...
1 parent 2d87b4b commit 3d69497ee33c6ea6ebe30296e03e9e07283eed96 Scott Barron committed Jun 22, 2008
View
@@ -8,6 +8,9 @@ class InvalidTransition < Exception
end
def self.included(base) #:nodoc:
+ # TODO - need to ensure that a machine is being created because
+ # AASM was either included or arrived at via inheritance. It
+ # cannot be both.
base.extend AASM::ClassMethods
AASM::Persistence.set_persistence(base)
AASM::StateMachine[base] = AASM::StateMachine.new('')
@@ -79,11 +79,14 @@ module ClassMethods
# This method is both a getter and a setter
def aasm_column(column_name=nil)
if column_name
- @aasm_column = column_name.to_sym
+ AASM::StateMachine[self].config.column = column_name.to_sym
+ # @aasm_column = column_name.to_sym
else
- @aasm_column ||= :aasm_state
+ AASM::StateMachine[self].config.column ||= :aasm_state
+ # @aasm_column ||= :aasm_state
end
- @aasm_column
+ # @aasm_column
+ AASM::StateMachine[self].config.column
end
def find_in_state(number, state, *args)
View
@@ -1,3 +1,5 @@
+require 'ostruct'
+
module AASM
class StateMachine
def self.[](*args)
@@ -9,14 +11,15 @@ def self.[]=(*args)
(@machines ||= {})[args] = val
end
- attr_accessor :states, :events, :initial_state
+ attr_accessor :states, :events, :initial_state, :config
attr_reader :name
def initialize(name)
@name = name
@initial_state = nil
@states = []
@events = {}
+ @config = OpenStruct.new
end
def create_state(name, options)
@@ -1,5 +1,3 @@
-require File.join(File.dirname(__FILE__), 'conversation')
-
describe Conversation, 'description' do
it '.aasm_states should contain all of the states' do
Conversation.aasm_states.should == [:needs_attention, :read, :closed, :awaiting_response, :junk]
@@ -43,6 +43,14 @@ def aasm_write_state_without_persistence(state)
include AASM
end
+ class June < ActiveRecord::Base
+ include AASM
+ aasm_column :status
+ end
+
+ class Beaver < June
+ end
+
describe "aasm model", :shared => true do
it "should include AASM::Persistence::ActiveRecordPersistence" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
@@ -178,6 +186,21 @@ def aasm_write_state_without_persistence(state)
end
+ describe 'Beavers' do
+ it "should have the same states as it's parent" do
+ Beaver.aasm_states.should == June.aasm_states
+ end
+
+ it "should have the same events as it's parent" do
+ Beaver.aasm_events.should == June.aasm_events
+ end
+
+ it "should have the same column as it's parent" do
+ Beaver.aasm_column.should == :status
+ end
+ end
+
+
# TODO: figure out how to test ActiveRecord reload! without a database
rescue LoadError => e

0 comments on commit 3d69497

Please sign in to comment.