aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/state_machine/state.rb
diff options
context:
space:
mode:
authorSven Fuchs <svenfuchs@artweb-design.de>2008-07-16 03:41:11 +0200
committerSven Fuchs <svenfuchs@artweb-design.de>2008-07-16 03:41:11 +0200
commit931f366ffcacc0444fcca2fb2e2b44644db9642f (patch)
tree4c056de1273d23e2b8494cfe452caaeb98ade820 /activemodel/lib/active_model/state_machine/state.rb
parent8691e255402b27eae594530001227fc05416a00c (diff)
parentfbef982e4b906b879240a35a1ecff447007da6b2 (diff)
downloadrails-931f366ffcacc0444fcca2fb2e2b44644db9642f.tar.gz
rails-931f366ffcacc0444fcca2fb2e2b44644db9642f.tar.bz2
rails-931f366ffcacc0444fcca2fb2e2b44644db9642f.zip
merge forward to current rails/master
Diffstat (limited to 'activemodel/lib/active_model/state_machine/state.rb')
-rw-r--r--activemodel/lib/active_model/state_machine/state.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb
new file mode 100644
index 0000000000..68eb2aa34a
--- /dev/null
+++ b/activemodel/lib/active_model/state_machine/state.rb
@@ -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