From bd788a52123ae2faf41bfd89c16d9545a7d567c3 Mon Sep 17 00:00:00 2001 From: Chad Humphries Date: Mon, 14 Apr 2008 15:57:34 -0400 Subject: [PATCH] Added aasm_states_for_select that will return a select friendly collection of states. Also added additional tests --- CHANGELOG | 2 ++ lib/aasm.rb | 5 +++++ spec/unit/aasm_spec.rb | 26 +++++++++++++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9898a48..7c84192 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Added aasm_states_for_select that will return a select friendly collection of states. + * Add some event callbacks, #aasm_event_fired(from, to), and #aasm_event_failed(event) Based on transition logging suggestion [Artem Vasiliev] and timestamp column suggestion [Mike Ferrier] diff --git a/lib/aasm.rb b/lib/aasm.rb index 9a2f6ef..85aae4b 100644 --- a/lib/aasm.rb +++ b/lib/aasm.rb @@ -64,6 +64,11 @@ module AASM def aasm_events @aasm_events ||= {} end + + def aasm_states_for_select + aasm_states.collect { |state| [state.to_s.gsub(/_/, ' ').capitalize, state] } + end + end # Instance methods diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index b418bf5..9a0582e 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -27,17 +27,37 @@ end describe AASM, '- class level definitions' do - it 'should define a class level initial_state() method on its including class' do + it 'should define a class level aasm_initial_state() method on its including class' do Foo.should respond_to(:aasm_initial_state) end - it 'should define a class level state() method on its including class' do + it 'should define a class level aasm_state() method on its including class' do Foo.should respond_to(:aasm_state) end - it 'should define a class level event() method on its including class' do + it 'should define a class level aasm_event() method on its including class' do Foo.should respond_to(:aasm_event) end + + it 'should define a class level aasm_states() method on its including class' do + Foo.should respond_to(:aasm_states) + end + + it 'should define a class level aasm_states_for_select() method on its including class' do + Foo.should respond_to(:aasm_states_for_select) + end + + it 'should define a class level aasm_events() method on its including class' do + Foo.should respond_to(:aasm_events) + end + +end + + +describe AASM, '- aasm_states_for_select' do + it "should return a select friendly array of states in the form of [['Friendly name', :state_name]]" do + Foo.aasm_states_for_select.should == [['Open', :open], ['Closed', :closed]] + end end describe AASM, '- instance level definitions' do