Permalink
Browse files

Merge branch 'state_machine'

Some big changes:
  * Added some redundant requires so active_support/inflecto can be loaded
    without the rest of ActiveSupport.
  * Disabled callbacks and validations until they are added and tested.
  * Converted specs back to tests, using ActiveSupport::TestCase and the new
    #test helper.
  * As an experiment, I imported Scott Barron's awesome AASM gem into
    ActiveModel.  I added multiple state machine support and vastly improved
    the API (no more aasm_* prefixes).  All the old tests pass.  If this bothers
    people, I have no problems removing this and contributing the changes back to
    AASM.  I just feel like AMo is a better spot for all these 'modelish' features.
  • Loading branch information...
2 parents 4cf9393 + c9e366e commit 01db5ded54b0e3a2ea80d28e4841d40fcec23cdf @technoweenie technoweenie committed Jun 29, 2008
View
@@ -1,16 +1,24 @@
#!/usr/bin/env ruby
-$LOAD_PATH << File.join(File.dirname(__FILE__), 'vendor', 'rspec', 'lib')
require 'rake'
-require 'spec/rake/spectask'
+require 'rake/testtask'
require 'rake/rdoctask'
+task :default => :test
+
+Rake::TestTask.new do |t|
+ t.libs << "test"
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+ t.warning = true
+end
+
# Generate the RDoc documentation
-Rake::RDocTask.new { |rdoc|
+Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'doc'
rdoc.title = "Active Model"
rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
rdoc.options << '--charset' << 'utf-8'
rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo'
rdoc.rdoc_files.include('README', 'CHANGES')
rdoc.rdoc_files.include('lib/**/*.rb')
-}
+end
@@ -1,17 +1,5 @@
-$LOAD_PATH << File.join(File.dirname(__FILE__), '..', '..', 'activesupport', 'lib')
-
-# premature optimization?
-require 'active_support/inflector'
-require 'active_support/core_ext/string/inflections'
-String.send :include, ActiveSupport::CoreExtensions::String::Inflections
-
-require 'active_model/base'
require 'active_model/observing'
-require 'active_model/callbacks'
-require 'active_model/validations'
-
-ActiveModel::Base.class_eval do
- include ActiveModel::Observing
- include ActiveModel::Callbacks
- include ActiveModel::Validations
-end
+# disabled until they're tested
+# require 'active_model/callbacks'
+# require 'active_model/validations'
+require 'active_model/base'
@@ -1,4 +1,8 @@
module ActiveModel
class Base
+ include Observing
+ # disabled, until they're tested
+ # include Callbacks
+ # include Validations
end
end
@@ -1,3 +1,5 @@
+require 'active_model/core'
+
module ActiveModel
module Callbacks
@@ -0,0 +1,7 @@
+# This file is required by each major ActiveModel component for the core requirements. This allows you to
+# load individual pieces of ActiveModel as needed.
+$LOAD_PATH << File.join(File.dirname(__FILE__), '..', '..', '..', 'activesupport', 'lib')
+
+# premature optimization?
+# So far, we only need the string inflections and not the rest of ActiveSupport.
+require 'active_support/inflector'
@@ -1,4 +1,6 @@
require 'observer'
+require 'singleton'
+require 'active_model/core'
module ActiveModel
module Observing
@@ -73,7 +75,7 @@ def observed_class
# Start observing the declared classes and their subclasses.
def initialize
self.observed_classes = self.class.models if self.class.models
- observed_classes.each { |klass| add_observer! klass }
+ observed_classes.each { |klass| klass.add_observer(self) }
end
# Send observed_method(object) if the method exists.
@@ -85,16 +87,12 @@ def update(observed_method, object) #:nodoc:
# Passes the new subclass.
def observed_class_inherited(subclass) #:nodoc:
self.class.observe(observed_classes + [subclass])
- add_observer!(subclass)
+ subclass.add_observer(self)
end
- protected
- def observed_classes
- @observed_classes ||= [self.class.observed_class]
- end
-
- def add_observer!(klass)
- klass.add_observer(self)
- end
+ protected
+ def observed_classes
+ @observed_classes ||= [self.class.observed_class]
+ end
end
end
@@ -0,0 +1,66 @@
+Dir[File.dirname(__FILE__) + "/state_machine/*.rb"].sort.each do |path|
+ filename = File.basename(path)
+ require "active_model/state_machine/#{filename}"
+end
+
+module ActiveModel
+ module StateMachine
+ class InvalidTransition < Exception
+ end
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def inherited(klass)
+ super
+ klass.state_machines = state_machines
+ end
+
+ def state_machines
+ @state_machines ||= {}
+ end
+
+ def state_machines=(value)
+ @state_machines = value ? value.dup : nil
+ end
+
+ def state_machine(name = nil, options = {}, &block)
+ if name.is_a?(Hash)
+ options = name
+ name = nil
+ end
+ name ||= :default
+ state_machines[name] ||= Machine.new(self, name)
+ block ? state_machines[name].update(options, &block) : state_machines[name]
+ end
+ end
+
+ def current_state(name = nil, new_state = nil, persist = false)
+ sm = self.class.state_machine(name)
+ ivar = sm.current_state_variable
+ if name && new_state
+ if persist && respond_to?(:write_state)
+ write_state(sm, new_state)
+ end
+
+ if respond_to?(:write_state_without_persistence)
+ write_state_without_persistence(sm, new_state)
+ end
+
+ instance_variable_set(ivar, new_state)
+ else
+ instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar)
+ value = instance_variable_get(ivar)
+ return value if value
+
+ if respond_to?(:read_state)
+ value = instance_variable_set(ivar, read_state(sm))
+ end
+
+ value || sm.initial_state
+ end
+ end
+ end
+end
@@ -0,0 +1,62 @@
+module ActiveModel
+ module StateMachine
+ class Event
+ attr_reader :name, :success
+
+ def initialize(machine, name, options = {}, &block)
+ @machine, @name, @transitions = machine, name, []
+ if machine
+ machine.klass.send(:define_method, "#{name.to_s}!") do |*args|
+ machine.fire_event(name, self, true, *args)
+ end
+
+ machine.klass.send(:define_method, "#{name.to_s}") do |*args|
+ machine.fire_event(name, self, false, *args)
+ end
+ end
+ update(options, &block)
+ end
+
+ def fire(obj, to_state = nil, *args)
+ transitions = @transitions.select { |t| t.from == obj.current_state(@machine ? @machine.name : nil) }
+ raise InvalidTransition if transitions.size == 0
+
+ next_state = nil
+ transitions.each do |transition|
+ next if to_state && !Array(transition.to).include?(to_state)
+ if transition.perform(obj)
+ next_state = to_state || Array(transition.to).first
+ transition.execute(obj, *args)
+ break
+ end
+ end
+ next_state
+ end
+
+ def transitions_from_state?(state)
+ @transitions.any? { |t| t.from? state }
+ end
+
+ def ==(event)
+ if event.is_a? Symbol
+ name == event
+ else
+ name == event.name
+ end
+ end
+
+ def update(options = {}, &block)
+ if options.key?(:success) then @success = options[:success] end
+ if block then instance_eval(&block) end
+ self
+ end
+
+ 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
@@ -0,0 +1,74 @@
+module ActiveModel
+ module StateMachine
+ class Machine
+ attr_accessor :initial_state, :states, :events, :state_index
+ attr_reader :klass, :name
+
+ def initialize(klass, name, options = {}, &block)
+ @klass, @name, @states, @state_index, @events = klass, name, [], {}, {}
+ update(options, &block)
+ end
+
+ def initial_state
+ @initial_state ||= (states.first ? states.first.name : nil)
+ end
+
+ def update(options = {}, &block)
+ if options.key?(:initial) then @initial_state = options[:initial] end
+ if block then instance_eval(&block) end
+ self
+ end
+
+ def fire_event(event, record, persist, *args)
+ state_index[record.current_state(@name)].call_action(:exit, record)
+ if new_state = @events[event].fire(record, *args)
+ state_index[new_state].call_action(:enter, record)
+
+ if record.respond_to?(event_fired_callback)
+ record.send(event_fired_callback, record.current_state, new_state)
+ end
+
+ record.current_state(@name, new_state, persist)
+ record.send(@events[event].success) if @events[event].success
+ true
+ else
+ if record.respond_to?(event_failed_callback)
+ record.send(event_failed_callback, event)
+ end
+
+ false
+ end
+ end
+
+ def states_for_select
+ states.map { |st| [st.display_name, st.name.to_s] }
+ end
+
+ def events_for(state)
+ events = @events.values.select { |event| event.transitions_from_state?(state) }
+ events.map! { |event| event.name }
+ end
+
+ def current_state_variable
+ "@#{@name}_current_state"
+ 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_fired_callback
+ @event_fired_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_fired'
+ end
+
+ def event_failed_callback
+ @event_failed_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_failed'
+ end
+ end
+ end
+end
@@ -0,0 +1,50 @@
+module ActiveModel
+ module StateMachine
+ class State
+ attr_reader :name, :options
+
+ def initialize(name, options = {})
+ @name = name
+ machine = options.delete(:machine)
+ if machine
+ machine.klass.send(:define_method, "#{name}?") do
+ current_state.to_s == name.to_s
+ end
+ end
+ update(options)
+ end
+
+ def ==(state)
+ if state.is_a? Symbol
+ name == state
+ else
+ name == state.name
+ end
+ end
+
+ def call_action(action, record)
+ action = @options[action]
+ case action
+ when Symbol, String
+ record.send(action)
+ when Proc
+ action.call(record)
+ end
+ end
+
+ def display_name
+ @display_name ||= name.to_s.gsub(/_/, ' ').capitalize
+ end
+
+ def for_select
+ [display_name, name.to_s]
+ end
+
+ def update(options = {})
+ if options.key?(:display) then @display_name = options.delete(:display) end
+ @options = options
+ self
+ end
+ end
+ end
+end
Oops, something went wrong.

14 comments on commit 01db5de

I am very excited by this, Rick.

I have been using AASM — slightly modified by Jeffrey Hardy — on every project I’ve worked on since it was released.

I’m shocked that a bigger deal hasn’t been made about this being added.

Contributor

nbibler replied Jul 10, 2008

I sent a notice over to Gregg at RailsEnvy (http://www.railsenvy.com) on June 29 and it was mentioned in the podcast, episode #36, 7/2/08.

I think including AASM functionality in core a great idea. I’m sure a lot of people will think that Rails is just gaining more bloat, but state machines are extremely valuable to software projects. I’ve used AASM in several, if not all of my projects over the past few years. I’m also curious as to why more people haven’t picked up on it, but as always, the community is very fickle. Glad to see it added.

Contributor

ryanb replied Jul 10, 2008

This is added to Active Model, not directly to Active Record, which may explain why not many have covered it. From my understanding we won’t be seeing it in Active Model / Active Resource until a while longer. Someone please correct me if I’m wrong.

Contributor

ryanb replied Jul 10, 2008

correction: From my understanding we won’t be seeing it in Active Record / Active Resource until a while longer.

Contributor

technoweenie replied Jul 10, 2008

Also, I don’t plan to include this in ActiveRecord by default. It’ll be available if you want to require ‘active_model/state_machine’ and include the model (hello plugin!).

We’ll be doing some stuff to integrate validations/callbacks into AMo::StateMachine as well, rather than the custom callback/guard system that it uses.

Contributor

nbibler replied Jul 10, 2008

Nice. I know in at least one of my AASM projects I had to give it major overhaul to support transitions not only with guards, but also restricted to user roles. Something similar to before_filters might be nice so that we could chain guard calls and whatnot.

Contributor

chrislloyd replied Jul 11, 2008

Perhaps a blog post to explain all the ActiveModel/Record/Resource gameplan?

Second chrislloyd

I’m glad I kick-started some discussion around this!

Rick, I’d love it if you took some time to make a quick syntax proposal. It seems that the original AASM and what you intend to end up with are going to be very different beasts.

I’m all for changing my approach when it’s better, but I’d value the opportunity to participate in some brainstorming around the invocation and syntax for what you have in mind. I’m already using a slightly modified AASM and as with all things, there’s usually a few ways to do the same things… often it just boils down to aesthetics.

Contributor

obrie replied Jul 13, 2008

I must be the only person here who believes this, but including state machine support I feel is bloat and is better off being a plugin like acts_as_list, acts_as_tree, and all the others. Is that not the reason those were pulled out of Rails to begin with? Or was it that it didn’t meet the 80/20 mark? Perhaps it’s my misunderstanding of the purpose of ActiveModel. I use state machines in most projects myself (see http://github.com/pluginaweek/state_machine), so I certainly agree that state machines are extremely valuable to software projects. However, ActiveModel feels like the wrong place for something like this to exist.

lb replied Jul 13, 2008

I am very excited about this commit and having integrated validations/callbacks sounds intriguing.

Contributor

cristibalan replied Jul 13, 2008

Have you considered stateful instead of aasm (http://github.com/jbarnette/stateful/tree/master)? Or, at least, will this make projects like stateful more difficult to use?

It apparently allows you to have state on every ruby class you want and the syntax seems a bit nicer than aasm.

Contributor

jbarnette replied Jul 13, 2008

Rick and I have talked a bit about this on #rails-contrib: While I (obviously) prefer Stateful’s syntax, AASM has a fair amount of additional features (multiple state machines, guards), and it’s mature and battle-tested. I think this is probably the right direction :)

I actually spent a lot of time today playing with obrie’s state_machine, and I’m reasonably confident that it’s even better than AASM, with a very similar syntax. It supports multiple states on a single model, as well as :initial => Proc.new which is something I’ve always wanted with AASM.

Now I’m busy converting my active project, and I think the biggest difference is that you don’t have to define a fixed list of states.

Please sign in to comment.