diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d9b30d..845d1b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ ## 5.3.1 * Fix fire! with string argument [#787](https://github.com/aasm/aasm/issues/787), in PR[#788](https://github.com/aasm/aasm/pull/788) thanks to [norman](https://github.com/norman) +* Allow `fire` and `fire!` to accept a String or Symbol for the event name, and raise AASM::UndefinedEvent rather than AASM::UndefinedState + when an event can not be found, thanks to [norman](https://github.com/norman). + ## 5.3.0 * Add Ruby 3.1 and Rails 7 to the CI matrix [#775](https://github.com/aasm/aasm/pull/775), thanks to [petergoldstein](https://github.com/petergoldstein) diff --git a/lib/aasm/errors.rb b/lib/aasm/errors.rb index 8e9c744..1b8e716 100644 --- a/lib/aasm/errors.rb +++ b/lib/aasm/errors.rb @@ -17,5 +17,6 @@ module AASM end class UndefinedState < RuntimeError; end + class UndefinedEvent < UndefinedState; end class NoDirectAssignmentError < RuntimeError; end end diff --git a/lib/aasm/instance_base.rb b/lib/aasm/instance_base.rb index 113e638..b861054 100644 --- a/lib/aasm/instance_base.rb +++ b/lib/aasm/instance_base.rb @@ -134,10 +134,11 @@ module AASM private def event_exists?(event_name, bang = false) - event = @instance.class.aasm(@name).state_machine.events[event_name] - event_error = bang ? "#{event_name}!" : event_name + event = @instance.class.aasm(@name).state_machine.events[event_name.to_sym] + return true if event - raise AASM::UndefinedState, "State :#{event_error} doesn't exist" if event.nil? + event_error = bang ? "#{event_name}!" : event_name + raise AASM::UndefinedEvent, "Event :#{event_error} doesn't exist" if event.nil? end end end diff --git a/spec/unit/callbacks_spec.rb b/spec/unit/callbacks_spec.rb index db258c8..c1a0863 100644 --- a/spec/unit/callbacks_spec.rb +++ b/spec/unit/callbacks_spec.rb @@ -153,11 +153,11 @@ describe 'callbacks for the new DSL' do expect { callback.aasm.fire(:unknown) - }.to raise_error(AASM::UndefinedState, "State :unknown doesn't exist") + }.to raise_error(AASM::UndefinedEvent, "Event :unknown doesn't exist") expect { callback.aasm.fire!(:unknown) - }.to raise_error(AASM::UndefinedState, "State :unknown! doesn't exist") + }.to raise_error(AASM::UndefinedEvent, "Event :unknown! doesn't exist") end it "does not run any state callback if the event guard fails" do diff --git a/spec/unit/complex_example_spec.rb b/spec/unit/complex_example_spec.rb index debe3f4..191e42c 100644 --- a/spec/unit/complex_example_spec.rb +++ b/spec/unit/complex_example_spec.rb @@ -92,10 +92,10 @@ describe 'when being unsuspended' do end it "should raise AASM::UndefinedState when firing unknown events" do - expect { auth.aasm.fire(:unknown) }.to raise_error(AASM::UndefinedState, "State :unknown doesn't exist") + expect { auth.aasm.fire(:unknown) }.to raise_error(AASM::UndefinedEvent, "Event :unknown doesn't exist") end it "should raise AASM::UndefinedState when firing unknown bang events" do - expect { auth.aasm.fire!(:unknown) }.to raise_error(AASM::UndefinedState, "State :unknown! doesn't exist") + expect { auth.aasm.fire!(:unknown) }.to raise_error(AASM::UndefinedEvent, "Event :unknown! doesn't exist") end end diff --git a/spec/unit/event_spec.rb b/spec/unit/event_spec.rb index 08da8fd..55e78dc 100644 --- a/spec/unit/event_spec.rb +++ b/spec/unit/event_spec.rb @@ -296,15 +296,32 @@ describe 'current event' do end describe "when calling events with fire/fire!" do - it "fire should populate aasm.current_event and transition (sleeping to showering)" do - pe.aasm.fire(:wakeup) - expect(pe.aasm.current_event).to eq :wakeup - expect(pe.aasm.current_state).to eq :showering + context "fire" do + it "should populate aasm.current_event and transition (sleeping to showering)" do + pe.aasm.fire(:wakeup) + expect(pe.aasm.current_event).to eq :wakeup + expect(pe.aasm.current_state).to eq :showering + end + + it "should allow event names as strings" do + pe.aasm.fire("wakeup") + expect(pe.aasm.current_event).to eq :wakeup + expect(pe.aasm.current_state).to eq :showering + end end - it "fire! should populate aasm.current_event and transition (sleeping to showering)" do - pe.aasm.fire!(:wakeup) - expect(pe.aasm.current_event).to eq :wakeup! - expect(pe.aasm.current_state).to eq :showering + + context "fire!" do + it "should populate aasm.current_event and transition (sleeping to showering)" do + pe.aasm.fire!(:wakeup) + expect(pe.aasm.current_event).to eq :wakeup! + expect(pe.aasm.current_state).to eq :showering + end + + it "should allow event names as strings" do + pe.aasm.fire!("wakeup") + expect(pe.aasm.current_event).to eq :wakeup! + expect(pe.aasm.current_state).to eq :showering + end end end end