Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions acts_as_state_machine.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ Gem::Specification.new do |s|
s.rdoc_options = ["--main", "README"]
s.extra_rdoc_files = ["README"]

s.add_dependency 'activerecord', ['>= 2.1']
s.add_dependency 'activerecord', ['>= 3.1']

s.files = ["CHANGELOG",
"MIT-LICENSE",
"README",
"Rakefile",
"TODO",
"acts_as_state_machine.gemspec",
"init.rb",
"lib/acts_as_state_machine.rb",
"rails/init.rb"]
"lib/acts_as_state_machine.rb"]

s.test_files = ["test/fixtures",
"test/fixtures/conversations.yml",
Expand Down
1 change: 0 additions & 1 deletion init.rb

This file was deleted.

61 changes: 27 additions & 34 deletions lib/acts_as_state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def guard(obj)
def perform(record)
return false unless guard(record)
loopback = record.current_state.to_s == to
states = record.class.read_inheritable_attribute(:states)
states = record.class._states
next_state = states[to]
old_state = states[record.current_state.to_s]

Expand Down Expand Up @@ -118,19 +118,23 @@ def acts_as_state_machine(options = {})

raise NoInitialState unless options[:initial]

write_inheritable_attribute :states, {}
write_inheritable_attribute :initial_state, options[:initial]
write_inheritable_attribute :transition_table, {}
write_inheritable_attribute :event_table, {}
write_inheritable_attribute :state_column, options[:column] || 'state'
class_attribute :_states
self._states = {}

class_inheritable_reader :initial_state
class_inheritable_reader :state_column
class_inheritable_reader :transition_table
class_inheritable_reader :event_table
class_attribute :initial_state
self.initial_state = options[:initial]

before_create :set_initial_state
after_create :run_initial_state_actions
class_attribute :transition_table
self.transition_table = {}

class_attribute :event_table
self.event_table = {}

class_attribute :state_column
self.state_column = options[:column] || 'state'

before_create :set_initial_state
after_create :run_initial_state_actions
end
end
end
Expand All @@ -141,7 +145,7 @@ def set_initial_state #:nodoc:
end

def run_initial_state_actions
initial = self.class.read_inheritable_attribute(:states)[self.class.initial_state.to_s]
initial = self.class._states[self.class.initial_state.to_s]
initial.entering(self)
initial.entered(self)
end
Expand All @@ -158,7 +162,7 @@ def next_state_for_event(event)
end

def next_states_for_event(event)
self.class.read_inheritable_attribute(:transition_table)[event.to_sym].select do |s|
self.class.transition_table[event.to_sym].select do |s|
s.from == current_state.to_s
end
end
Expand All @@ -172,7 +176,7 @@ def run_transition_action(action)
module ClassMethods
# Returns an array of all known states.
def states
read_inheritable_attribute(:states).keys.collect { |state| state.to_sym }
_states.keys.collect { |state| state.to_sym }
end

# Define an event. This takes a block which describes all valid transitions
Expand All @@ -199,10 +203,8 @@ def states
# created is the name of the event followed by an exclamation point (!).
# Example: <tt>order.close_order!</tt>.
def event(event, opts={}, &block)
tt = read_inheritable_attribute(:transition_table)

e = SupportingClasses::Event.new(event, opts, tt, &block)
write_inheritable_hash(:event_table, event.to_sym => e)
e = SupportingClasses::Event.new(event, opts, transition_table, &block)
self.event_table = event_table.merge(event.to_sym => e)
define_method("#{event.to_s}!") { e.fire(self) }
end

Expand All @@ -220,7 +222,7 @@ def event(event, opts={}, &block)
# end
def state(name, opts={})
state = SupportingClasses::State.new(name, opts)
write_inheritable_hash(:states, state.value => state)
self._states = _states.merge(state.value => state)

define_method("#{state.name}?") { current_state.to_s == state.value }
end
Expand All @@ -232,9 +234,7 @@ def state(name, opts={})
# * +state+ - The state to find
# * +args+ - The rest of the args are passed down to ActiveRecord +find+
def find_in_state(number, state, *args)
with_state_scope state do
find(number, *args)
end
_state_scope(state).find(number, *args)
end

# Wraps ActiveRecord::Base.count to conveniently count all records in
Expand All @@ -243,9 +243,7 @@ def find_in_state(number, state, *args)
# * +state+ - The state to find
# * +args+ - The rest of the args are passed down to ActiveRecord +find+
def count_in_state(state, *args)
with_state_scope state do
count(*args)
end
_state_scope(state).count(*args)
end

# Wraps ActiveRecord::Base.calculate to conveniently calculate all records in
Expand All @@ -254,18 +252,13 @@ def count_in_state(state, *args)
# * +state+ - The state to find
# * +args+ - The rest of the args are passed down to ActiveRecord +calculate+
def calculate_in_state(state, *args)
with_state_scope state do
calculate(*args)
end
_state_scope(state).calculate(*args)
end

protected
def with_state_scope(state)
def _state_scope(state)
raise InvalidState unless states.include?(state.to_sym)

with_scope :find => {:conditions => ["#{table_name}.#{state_column} = ?", state.to_s]} do
yield if block_given?
end
where(state_column => state.to_s)
end
end
end
Expand Down
5 changes: 0 additions & 5 deletions rails/init.rb

This file was deleted.

14 changes: 7 additions & 7 deletions test/test_acts_as_state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ class ActsAsStateMachineTest < Test::Unit::TestCase

def teardown
Conversation.class_eval do
write_inheritable_attribute :states, {}
write_inheritable_attribute :initial_state, nil
write_inheritable_attribute :transition_table, {}
write_inheritable_attribute :event_table, {}
write_inheritable_attribute :state_column, "state"
self._states = {}
self.finitial_state = nil
self.transition_table = {}
self.event_table = {}
self.state_column = "state"

# Clear out any callbacks that were set by acts_as_state_machine.
write_inheritable_attribute :before_create, []
write_inheritable_attribute :after_create, []
reset_callbacks :before_create
reset_callbacks :after_create
end
end

Expand Down