diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-08-04 11:03:57 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-08-04 11:03:57 -0500 |
commit | aad5a30bf25d8a3167afd685fc91c99f4f09cc57 (patch) | |
tree | f700750ee088e483537fa7ee992a109b834b1ddc /activemodel | |
parent | 55d1d12c32a1b99f3f07d2346b49a63650ba2e9d (diff) | |
download | rails-aad5a30bf25d8a3167afd685fc91c99f4f09cc57.tar.gz rails-aad5a30bf25d8a3167afd685fc91c99f4f09cc57.tar.bz2 rails-aad5a30bf25d8a3167afd685fc91c99f4f09cc57.zip |
Add simple support for ActiveModel's StateMachine for ActiveRecord
Diffstat (limited to 'activemodel')
4 files changed, 21 insertions, 29 deletions
diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index 1172d31ea3..527794b34d 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -5,12 +5,9 @@ module ActiveModel autoload :State, 'active_model/state_machine/state' autoload :StateTransition, 'active_model/state_machine/state_transition' - class InvalidTransition < Exception - end + extend ActiveSupport::Concern - def self.included(base) - require 'active_model/state_machine/machine' - base.extend ClassMethods + class InvalidTransition < Exception end module ClassMethods diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb index 3eb656b6d6..30e9601dc2 100644 --- a/activemodel/lib/active_model/state_machine/event.rb +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -1,5 +1,3 @@ -require 'active_model/state_machine/state_transition' - module ActiveModel module StateMachine class Event @@ -53,12 +51,12 @@ module ActiveModel self end - private - def transitions(trans_opts) - Array(trans_opts[:from]).each do |s| - @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym})) + private + def transitions(trans_opts) + Array(trans_opts[:from]).each do |s| + @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym})) + end end - end end end end diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index a5ede021b1..777531213e 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -1,6 +1,3 @@ -require 'active_model/state_machine/state' -require 'active_model/state_machine/event' - module ActiveModel module StateMachine class Machine @@ -57,22 +54,22 @@ module ActiveModel "@#{@name}_current_state" end - private - def state(name, options = {}) - @states << (state_index[name] ||= State.new(name, :machine => self)).update(options) - end + private + def state(name, options = {}) + @states << (state_index[name] ||= State.new(name, :machine => self)).update(options) + end - def event(name, options = {}, &block) - (@events[name] ||= Event.new(self, name)).update(options, &block) - end + def event(name, options = {}, &block) + (@events[name] ||= Event.new(self, name)).update(options, &block) + end - def event_fired_callback - @event_fired_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_fired' - end + def event_fired_callback + @event_fired_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_fired' + end - def event_failed_callback - @event_failed_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_failed' - end + def event_failed_callback + @event_failed_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_failed' + end end end end diff --git a/activemodel/lib/active_model/state_machine/state_transition.rb b/activemodel/lib/active_model/state_machine/state_transition.rb index f9df998ea4..b0c5504de7 100644 --- a/activemodel/lib/active_model/state_machine/state_transition.rb +++ b/activemodel/lib/active_model/state_machine/state_transition.rb @@ -18,7 +18,7 @@ module ActiveModel true end end - + def execute(obj, *args) case @on_transition when Symbol, String |