From 551503d12f14e99ad12e2faf3e6464f619d860a5 Mon Sep 17 00:00:00 2001 From: Scott Barron Date: Thu, 21 Feb 2008 12:54:42 -0500 Subject: [PATCH] Add .aasm_states method to get a list of all states for a class --- MIT-LICENSE | 2 +- Rakefile | 1 - TODO | 1 - lib/aasm.rb | 9 ++++++++- spec/functional/conversation_spec.rb | 7 +++++++ 5 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 spec/functional/conversation_spec.rb diff --git a/MIT-LICENSE b/MIT-LICENSE index 3189ba6..ccc6a79 100644 --- a/MIT-LICENSE +++ b/MIT-LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2006 Scott Barron +Copyright (c) 2008 Scott Barron Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Rakefile b/Rakefile index 6e1d580..5bf0a10 100644 --- a/Rakefile +++ b/Rakefile @@ -14,7 +14,6 @@ rescue Exception nil end -# Version if `ruby -Ilib -rversion -e "print AASM::VERSION::STRING"` =~ /([0-9.]+)$/ CURRENT_VERSION = $1 else diff --git a/TODO b/TODO index 2bcfb14..56e773d 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ Before Next Release: * Add automatic persistence hooks for ActiveRecord classes * Add #aasm_next_state_for_event * Add #aasm_next_states_for_event -* Add .states Cool ideas from users: diff --git a/lib/aasm.rb b/lib/aasm.rb index 74e67a7..ac281e8 100644 --- a/lib/aasm.rb +++ b/lib/aasm.rb @@ -20,6 +20,7 @@ module AASM alias :initial_state :aasm_initial_state= def state(name, options={}) + aasm_states << name unless aasm_states.include?(name) self.aasm_initial_state = name unless self.aasm_initial_state define_method("#{name.to_s}?") do @@ -28,7 +29,9 @@ module AASM end def event(name, &block) - aasm_events[name] = AASM::SupportingClasses::Event.new(name, &block) + unless aasm_events.has_key?(name) + aasm_events[name] = AASM::SupportingClasses::Event.new(name, &block) + end define_method("#{name.to_s}!") do new_state = self.class.aasm_events[name].fire(self) @@ -41,6 +44,10 @@ module AASM end end + def aasm_states + @aasm_states ||= [] + end + def aasm_events @aasm_events ||= {} end diff --git a/spec/functional/conversation_spec.rb b/spec/functional/conversation_spec.rb new file mode 100644 index 0000000..e64d7bc --- /dev/null +++ b/spec/functional/conversation_spec.rb @@ -0,0 +1,7 @@ +require File.join(File.dirname(__FILE__), 'conversation') + +describe Conversation, 'description' do + it '.aasm_states should contain all of the states' do + Conversation.aasm_states.should == [:needs_attention, :read, :closed, :awaiting_response, :junk] + end +end