From 77c620d7c4affeb392fabf6b8f5bcd93708b1d5f Mon Sep 17 00:00:00 2001 From: lukemelia Date: Fri, 2 Feb 2007 05:58:52 +0000 Subject: [PATCH] prepare to update acts_as_state_machine git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@419 a4c988fc-2ded-0310-b66e-134b36920a42 --- .../plugins/acts_as_state_machine/CHANGELOG | 7 - .../plugins/acts_as_state_machine/MIT-LICENSE | 20 -- .../plugins/acts_as_state_machine/README | 33 --- .../plugins/acts_as_state_machine/Rakefile | 28 --- .../vendor/plugins/acts_as_state_machine/TODO | 11 - .../plugins/acts_as_state_machine/init.rb | 5 - .../lib/acts_as_state_machine.rb | 222 ------------------ .../test/acts_as_state_machine_test.rb | 174 -------------- .../acts_as_state_machine/test/database.yml | 18 -- .../test/fixtures/conversation.rb | 50 ---- .../test/fixtures/conversations.yml | 11 - .../test/fixtures/person.rb | 2 - .../acts_as_state_machine/test/schema.rb | 11 - .../acts_as_state_machine/test/test_helper.rb | 38 --- 14 files changed, 630 deletions(-) delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/CHANGELOG delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/MIT-LICENSE delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/README delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/Rakefile delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/TODO delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/init.rb delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/lib/acts_as_state_machine.rb delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/test/acts_as_state_machine_test.rb delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/test/database.yml delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/test/fixtures/conversation.rb delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/test/fixtures/conversations.yml delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/test/fixtures/person.rb delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/test/schema.rb delete mode 100644 tracks/vendor/plugins/acts_as_state_machine/test/test_helper.rb diff --git a/tracks/vendor/plugins/acts_as_state_machine/CHANGELOG b/tracks/vendor/plugins/acts_as_state_machine/CHANGELOG deleted file mode 100644 index d694511b..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/CHANGELOG +++ /dev/null @@ -1,7 +0,0 @@ -* 2.0 * (2006-01-20 15:26:28 -0500) - Enter / Exit actions - Transition guards - Guards and actions can be a symbol pointing to a method or a Proc - -* 1.0 * (2006-01-15 12:16:55 -0500) - Initial Release \ No newline at end of file diff --git a/tracks/vendor/plugins/acts_as_state_machine/MIT-LICENSE b/tracks/vendor/plugins/acts_as_state_machine/MIT-LICENSE deleted file mode 100644 index 3189ba6b..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/MIT-LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2006 Scott Barron - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/tracks/vendor/plugins/acts_as_state_machine/README b/tracks/vendor/plugins/acts_as_state_machine/README deleted file mode 100644 index 2bbcced6..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/README +++ /dev/null @@ -1,33 +0,0 @@ -= Acts As State Machine - -This act gives an Active Record model the ability to act as a finite state -machine (FSM). - -Acquire via subversion at: - -http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk - -If prompted, use the user/pass anonymous/anonymous. - -== Example - - class Order < ActiveRecord::Base - acts_as_state_machine :initial => :opened - - state :opened - state :closed, :enter => Proc.new {|o| Mailer.send_notice(o)} - state :returned - - event :close do - transitions :to => :closed, :from => :opened - end - - event :return do - transitions :to => :returned, :from => :closed - end - end - - o = Order.create - o.close! # notice is sent by mailer - o.return! - diff --git a/tracks/vendor/plugins/acts_as_state_machine/Rakefile b/tracks/vendor/plugins/acts_as_state_machine/Rakefile deleted file mode 100644 index 101fdde9..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/Rakefile +++ /dev/null @@ -1,28 +0,0 @@ -require 'rake' -require 'rake/testtask' -require 'rake/rdoctask' - -desc 'Default: run unit tests.' -task :default => [:clean_db, :test] - -desc 'Remove the stale db file' -task :clean_db do - `rm -f #{File.dirname(__FILE__)}/test/state_machine.sqlite.db` -end - -desc 'Test the acts as state machine plugin.' -Rake::TestTask.new(:test) do |t| - t.libs << 'lib' - t.pattern = 'test/**/*_test.rb' - t.verbose = true -end - -desc 'Generate documentation for the acts as state machine plugin.' -Rake::RDocTask.new(:rdoc) do |rdoc| - rdoc.rdoc_dir = 'rdoc' - rdoc.title = 'Acts As State Machine' - rdoc.options << '--line-numbers --inline-source' - rdoc.rdoc_files.include('README') - rdoc.rdoc_files.include('TODO') - rdoc.rdoc_files.include('lib/**/*.rb') -end diff --git a/tracks/vendor/plugins/acts_as_state_machine/TODO b/tracks/vendor/plugins/acts_as_state_machine/TODO deleted file mode 100644 index 8d5d7063..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/TODO +++ /dev/null @@ -1,11 +0,0 @@ -* Currently invalid events are ignored, create an option so that they can be - ignored or raise an exception. - -* Query for a list of possible next states. - -* Make listing states optional since they can be inferred from the events. - Only required to list a state if you want to define a transition block for it. - -* Real transition actions - -* Default states diff --git a/tracks/vendor/plugins/acts_as_state_machine/init.rb b/tracks/vendor/plugins/acts_as_state_machine/init.rb deleted file mode 100644 index 9ae95872..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/init.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'acts_as_state_machine' - -ActiveRecord::Base.class_eval do - include RailsStudio::Acts::StateMachine -end diff --git a/tracks/vendor/plugins/acts_as_state_machine/lib/acts_as_state_machine.rb b/tracks/vendor/plugins/acts_as_state_machine/lib/acts_as_state_machine.rb deleted file mode 100644 index c5b4c790..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/lib/acts_as_state_machine.rb +++ /dev/null @@ -1,222 +0,0 @@ -module RailsStudio #:nodoc: - module Acts #:nodoc: - module StateMachine #:nodoc: - class InvalidState < Exception #:nodoc: - end - class NoInitialState < Exception #:nodoc: - end - - def self.included(base) #:nodoc: - base.extend ActMacro - end - - module SupportingClasses - class StateTransition - attr_reader :from, :to - def initialize(from, to, guard=nil) - @from, @to, @guard = from, to, guard - end - - def guard(obj) - @guard ? obj.send(:run_transition_action, @guard) : true - end - - def ==(obj) - @from == obj.from && @to == obj.to - end - end - - class TransitionCollector - attr_reader :opts - def transitions(opts) - (@opts ||= []) << opts - end - end - end - - module ActMacro - # Configuration options are - # - # * +column+ - specifies the column name to use for keeping the state (default: state) - # * +initial+ - specifies an initial state for newly created objects (required) - def acts_as_state_machine(opts) - self.extend(ClassMethods) - raise NoInitialState unless opts[:initial] - - write_inheritable_attribute :states, {} - write_inheritable_attribute :initial_state, opts[:initial] - write_inheritable_attribute :transition_table, {} - write_inheritable_attribute :state_column, opts[:column] || 'state' - - class_inheritable_reader :initial_state - class_inheritable_reader :state_column - class_inheritable_reader :transition_table - - class_eval "include RailsStudio::Acts::StateMachine::InstanceMethods" - - before_create :set_initial_state - end - end - - module InstanceMethods - def set_initial_state #:nodoc: - write_attribute self.class.state_column, self.class.initial_state.to_s - end - - # Returns the current state the object is in, as a Ruby symbol. - def current_state - self.send(self.class.state_column).to_sym - end - - # Returns what the next state for a given event would be, as a Ruby symbol. - def next_state_for_event(event) - ns = next_states_for_event(event) - ns.empty? ? nil : ns.first.to - end - - def next_states_for_event(event) - self.class.read_inheritable_attribute(:transition_table)[event.to_sym].select do |s| - s.from == current_state - end - end - - def run_transition_action(action) - if action.kind_of?(Symbol) - self.method(action).call - else - action.call(self) - end - end - private :run_transition_action - end - - module ClassMethods - # Returns an array of all known states. - def states - read_inheritable_attribute(:states).keys - end - - # Define an event. This takes a block which describes all valid transitions - # for this event. - # - # Example: - # - # class Order < ActiveRecord::Base - # acts_as_state_machine :initial => :open - # - # state :open - # state :closed - # - # event :close_order do - # transitions :to => :closed, :from => :open - # end - # end - # - # +transitions+ takes a hash where :to is the state to transition - # to and :from is a state (or Array of states) from which this - # event can be fired. - # - # This creates an instance method used for firing the event. The method - # created is the name of the event followed by an exclamation point (!). - # Example: order.close_order!. - def event(event, &block) - class_eval <<-EOV - def #{event.to_s}! - next_states = next_states_for_event(:#{event.to_s}) - next_states.each do |ns| - if ns.guard(self) - loopback = current_state == ns.to - exitact = self.class.read_inheritable_attribute(:states)[current_state][:exit] - enteract = self.class.read_inheritable_attribute(:states)[ns.to][:enter] - - run_transition_action(enteract) if enteract && !loopback - - self.update_attribute(self.class.state_column, ns.to.to_s) - - run_transition_action(exitact) if exitact && !loopback - break - end - end - end - EOV - - tt = read_inheritable_attribute(:transition_table) - tt[event.to_sym] ||= [] - - if block_given? - t = SupportingClasses::TransitionCollector.new - t.instance_eval(&block) - trannys = t.opts - trannys.each do |tranny| - Array(tranny[:from]).each do |s| - tt[event.to_sym] << SupportingClasses::StateTransition.new(s.to_sym, tranny[:to], tranny[:guard]) - end - end - end - end - - def transitions(opts) #:nodoc: - opts - end - - # Define a state of the system. +state+ can take an optional Proc object - # which will be executed every time the system transitions into that - # state. The proc will be passed the current object. - # - # Example: - # - # class Order < ActiveRecord::Base - # acts_as_state_machine :initial => :open - # - # state :open - # state :closed, Proc.new { |o| Mailer.send_notice(o) } - # end - def state(state, opts={}) - read_inheritable_attribute(:states)[state.to_sym] = opts - - class_eval <<-EOS - def #{state.to_s}? - current_state == :#{state.to_s} - end - EOS - end - - # Wraps ActiveRecord::Base.find to conveniently find all records in - # a given state. Options: - # - # * +number+ - This is just :first or :all from ActiveRecord - # * +state+ - The state to find - # * +args+ - The rest of the args are passed down to ActiveRecord +find+ - def find_in_state(number, state, *args) - raise InvalidState unless states.include?(state) - - options = args.last.is_a?(Hash) ? args.pop : {} - if options[:conditions] - options[:conditions].first << " AND #{self.state_column} = ?" - options[:conditions] << state.to_s - else - options[:conditions] = ["#{self.state_column} = ?", state.to_s] - end - self.find(number, options) - end - - # Wraps ActiveRecord::Base.count to conveniently count all records in - # a given state. Options: - # - # * +state+ - The state to find - # * +args+ - The rest of the args are passed down to ActiveRecord +find+ - def count_in_state(state, conditions=nil) - raise InvalidState unless states.include?(state) - - if conditions - conditions.first << " AND #{self.state_column} = ?" - conditions << state.to_s - else - conditions = ["#{self.state_column} = ?", state.to_s] - end - self.count(conditions) - end - end - end - end -end \ No newline at end of file diff --git a/tracks/vendor/plugins/acts_as_state_machine/test/acts_as_state_machine_test.rb b/tracks/vendor/plugins/acts_as_state_machine/test/acts_as_state_machine_test.rb deleted file mode 100644 index 60dd7b17..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/test/acts_as_state_machine_test.rb +++ /dev/null @@ -1,174 +0,0 @@ -require File.dirname(__FILE__) + '/test_helper' - -include RailsStudio::Acts::StateMachine - -class ActsAsStateMachineTest < Test::Unit::TestCase - fixtures :conversations - - def test_no_initial_value_raises_exception - assert_raise(NoInitialState) { - Person.acts_as_state_machine({}) - } - end - - def test_initial_state_value - assert_equal :needs_attention, Conversation.initial_state - end - - def test_column_was_set - assert_equal 'state_machine', Conversation.state_column - end - - def test_initial_state - c = Conversation.create - assert_equal :needs_attention, c.current_state - assert c.needs_attention? - end - - def test_states_were_set - [:needs_attention, :read, :closed, :awaiting_response, :junk].each do |s| - assert Conversation.states.include?(s) - end - end - - def test_event_methods_created - c = Conversation.create - %w(new_message! view! reply! close! junk! unjunk!).each do |event| - assert c.respond_to?(event) - end - end - - def test_query_methods_created - c = Conversation.create - %w(needs_attention? read? closed? awaiting_response? junk?).each do |event| - assert c.respond_to?(event) - end - end - - def test_transition_table - tt = Conversation.transition_table - - assert tt[:new_message].include?(SupportingClasses::StateTransition.new(:read, :needs_attention)) - assert tt[:new_message].include?(SupportingClasses::StateTransition.new(:closed, :needs_attention)) - assert tt[:new_message].include?(SupportingClasses::StateTransition.new(:awaiting_response, :needs_attention)) - end - - def test_next_state_for_event - c = Conversation.create - assert_equal :read, c.next_state_for_event(:view) - end - - def test_change_state - c = Conversation.create - c.view! - assert c.read? - end - - def test_can_go_from_read_to_closed_because_guard_passes - c = Conversation.create - c.can_close = true - c.view! - c.reply! - c.close! - assert_equal :closed, c.current_state - end - - def test_cannot_go_from_read_to_closed_because_of_guard - c = Conversation.create - c.can_close = false - c.view! - c.reply! - c.close! - assert_equal :read, c.current_state - end - - def test_ignore_invalid_events - c = Conversation.create - c.view! - c.junk! - - # This is the invalid event - c.new_message! - assert_equal :junk, c.current_state - end - - def test_entry_action_executed - c = Conversation.create - c.read_enter = false - c.view! - assert c.read_enter - end - - def test_exit_action_executed - c = Conversation.create - c.read_exit = false - c.view! - c.junk! - assert c.read_exit - end - - def test_entry_and_exit_not_run_on_loopback_transition - c = Conversation.create - c.view! - c.read_enter = false - c.read_exit = false - c.view! - assert !c.read_enter - assert !c.read_exit - end - - def test_run_transition_action_is_private - c = Conversation.create - assert_raise(NoMethodError) { c.run_transition_action :foo } - end - - def test_find_all_in_state - cs = Conversation.find_in_state(:all, :read) - - assert_equal 2, cs.size - end - - def test_find_first_in_state - c = Conversation.find_in_state(:first, :read) - - assert_equal conversations(:first).id, c.id - end - - def test_find_all_in_state_with_conditions - cs = Conversation.find_in_state(:all, :read, :conditions => ['subject = ?', conversations(:second).subject]) - - assert_equal 1, cs.size - assert_equal conversations(:second).id, cs.first.id - end - - def test_find_first_in_state_with_conditions - c = Conversation.find_in_state(:first, :read, :conditions => ['subject = ?', conversations(:second).subject]) - assert_equal conversations(:second).id, c.id - end - - def test_count_in_state - cnt0 = Conversation.count(['state_machine = ?', 'read']) - cnt = Conversation.count_in_state(:read) - - assert_equal cnt0, cnt - end - - def test_count_in_state_with_conditions - cnt0 = Conversation.count(['state_machine = ? AND subject = ?', 'read', 'Foo']) - cnt = Conversation.count_in_state(:read, ['subject = ?', 'Foo']) - - assert_equal cnt0, cnt - end - - def test_find_in_invalid_state_raises_exception - assert_raise(InvalidState) { - Conversation.find_in_state(:all, :dead) - } - end - - def test_count_in_invalid_state_raises_exception - assert_raise(InvalidState) { - Conversation.count_in_state(:dead) - } - end -end diff --git a/tracks/vendor/plugins/acts_as_state_machine/test/database.yml b/tracks/vendor/plugins/acts_as_state_machine/test/database.yml deleted file mode 100644 index f012c25c..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/test/database.yml +++ /dev/null @@ -1,18 +0,0 @@ -sqlite: - :adapter: sqlite - :dbfile: state_machine.sqlite.db -sqlite3: - :adapter: sqlite3 - :dbfile: state_machine.sqlite3.db -postgresql: - :adapter: postgresql - :username: postgres - :password: postgres - :database: state_machine_test - :min_messages: ERROR -mysql: - :adapter: mysql - :host: localhost - :username: rails - :password: - :database: state_machine_test \ No newline at end of file diff --git a/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/conversation.rb b/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/conversation.rb deleted file mode 100644 index e18bad63..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/conversation.rb +++ /dev/null @@ -1,50 +0,0 @@ -class Conversation < ActiveRecord::Base - attr_writer :can_close - attr_accessor :read_enter, :read_exit - - acts_as_state_machine :initial => :needs_attention, :column => 'state_machine' - - state :needs_attention - state :read, :enter => :read_enter_action, - :exit => Proc.new { |o| o.read_exit = true } - state :closed - state :awaiting_response - state :junk - - event :new_message do - transitions :to => :needs_attention, :from => [:read, :closed, :awaiting_response] - end - - event :view do - transitions :to => :read, :from => [:needs_attention, :read] - end - - event :reply do - transitions :to => :awaiting_response, :from => [:read, :closed] - end - - event :close do - transitions :to => :closed, :from => [:read, :awaiting_response], :guard => Proc.new {|o| o.can_close?} - transitions :to => :read, :from => [:read, :awaiting_response], :guard => :always_true - end - - event :junk do - transitions :to => :junk, :from => [:read, :closed, :awaiting_response] - end - - event :unjunk do - transitions :to => :closed, :from => :junk - end - - def can_close? - @can_close - end - - def read_enter_action - self.read_enter = true - end - - def always_true - true - end -end diff --git a/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/conversations.yml b/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/conversations.yml deleted file mode 100644 index 572e0ee1..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/conversations.yml +++ /dev/null @@ -1,11 +0,0 @@ -first: - id: 1 - state_machine: read - subject: This is a test - closed: false - -second: - id: 2 - state_machine: read - subject: Foo - closed: false diff --git a/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/person.rb b/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/person.rb deleted file mode 100644 index e64a8262..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/test/fixtures/person.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Person < ActiveRecord::Base -end \ No newline at end of file diff --git a/tracks/vendor/plugins/acts_as_state_machine/test/schema.rb b/tracks/vendor/plugins/acts_as_state_machine/test/schema.rb deleted file mode 100644 index d79ee4a7..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/test/schema.rb +++ /dev/null @@ -1,11 +0,0 @@ -ActiveRecord::Schema.define(:version => 1) do - create_table :conversations do |t| - t.column :state_machine, :string - t.column :subject, :string - t.column :closed, :boolean - end - - create_table :people do |t| - t.column :name, :string - end -end diff --git a/tracks/vendor/plugins/acts_as_state_machine/test/test_helper.rb b/tracks/vendor/plugins/acts_as_state_machine/test/test_helper.rb deleted file mode 100644 index 95331d03..00000000 --- a/tracks/vendor/plugins/acts_as_state_machine/test/test_helper.rb +++ /dev/null @@ -1,38 +0,0 @@ -$:.unshift(File.dirname(__FILE__) + '/../lib') -RAILS_ROOT = File.dirname(__FILE__) - -require 'rubygems' -require 'test/unit' -require 'active_record' -require 'active_record/fixtures' -require 'active_support/binding_of_caller' -require 'active_support/breakpoint' -require "#{File.dirname(__FILE__)}/../init" - - -config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) -ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") -ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite']) - -load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb") - -Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/" -$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path) - -class Test::Unit::TestCase #:nodoc: - def create_fixtures(*table_names) - if block_given? - Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield } - else - Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) - end - end - - # Turn off transactional fixtures if you're working with MyISAM tables in MySQL - self.use_transactional_fixtures = true - - # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david) - self.use_instantiated_fixtures = false - - # Add more helper methods to be used by all tests here... -end \ No newline at end of file