diff --git a/acts_as_state_machine.gemspec b/acts_as_state_machine.gemspec index 242fa74..93cdb64 100644 --- a/acts_as_state_machine.gemspec +++ b/acts_as_state_machine.gemspec @@ -14,7 +14,7 @@ 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", @@ -22,9 +22,7 @@ Gem::Specification.new do |s| "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", diff --git a/init.rb b/init.rb deleted file mode 100644 index 2644663..0000000 --- a/init.rb +++ /dev/null @@ -1 +0,0 @@ -require File.dirname(__FILE__) + "/rails/init" \ No newline at end of file diff --git a/lib/acts_as_state_machine.rb b/lib/acts_as_state_machine.rb index 67fcead..b4f6e21 100644 --- a/lib/acts_as_state_machine.rb +++ b/lib/acts_as_state_machine.rb @@ -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] @@ -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 @@ -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 @@ -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 @@ -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 @@ -199,10 +203,8 @@ def states # created is the name of the event followed by an exclamation point (!). # Example: order.close_order!. 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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/rails/init.rb b/rails/init.rb deleted file mode 100644 index dd1b4cd..0000000 --- a/rails/init.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'acts_as_state_machine' - -ActiveRecord::Base.class_eval do - include ScottBarron::Acts::StateMachine -end diff --git a/test/test_acts_as_state_machine.rb b/test/test_acts_as_state_machine.rb index 9afb2ef..29e05cc 100644 --- a/test/test_acts_as_state_machine.rb +++ b/test/test_acts_as_state_machine.rb @@ -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