From 404a9d31036be314490daecd857395d9ba40c415 Mon Sep 17 00:00:00 2001 From: Petrica Ghiurca Date: Thu, 8 Oct 2009 13:05:21 +0300 Subject: [PATCH] support for multiple events for enter/exit states --- lib/aasm/state.rb | 19 +++++++++++++++---- spec/unit/state_spec.rb | 12 ++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/aasm/state.rb b/lib/aasm/state.rb index 477f7013..37766a5d 100644 --- a/lib/aasm/state.rb +++ b/lib/aasm/state.rb @@ -17,6 +17,21 @@ def ==(state) def call_action(action, record) action = @options[action] + if action.is_a? Array + action.each do |a| + _call(a, record) + end + else + _call(action, record) + end + end + + def for_select + [name.to_s.gsub(/_/, ' ').capitalize, name.to_s] + end + + private + def _call(action, record) case action when Symbol, String record.send(action) @@ -26,10 +41,6 @@ def call_action(action, record) action.each { |a| record.send(a) } end end - - def for_select - [name.to_s.gsub(/_/, ' ').capitalize, name.to_s] - end end end end diff --git a/spec/unit/state_spec.rb b/spec/unit/state_spec.rb index b01d6984..07d77b41 100644 --- a/spec/unit/state_spec.rb +++ b/spec/unit/state_spec.rb @@ -50,6 +50,18 @@ def new_state(options={}) state.call_action(:entering, record) end + + it 'should send a message to the record for each action' do + state = new_state(:entering => [:a, :b, "c", lambda {|r| r.foobar }]) + + record = mock('record') + record.should_receive(:a) + record.should_receive(:b) + record.should_receive(:c) + record.should_receive(:foobar) + + state.call_action(:entering, record) + end it 'should call a proc, passing in the record for an action if the action is present' do state = new_state(:entering => Proc.new {|r| r.foobar})