From 0adf6c579970394278715571785d7ea9f15fc229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20B=C3=B6ttger?= Date: Fri, 12 Sep 2014 11:42:01 +0200 Subject: [PATCH] allow retrieving the current event (see #159 and #168) --- CHANGELOG.md | 4 ++++ README.md | 25 +++++++++++++++++++++++++ lib/aasm/base.rb | 2 ++ lib/aasm/instance_base.rb | 2 +- spec/unit/event_spec.rb | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1a1bbd..98e08f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ * deprecated old aasm_* class methods (old-style DSL), in preparation for AASM v4.0.0 +## 3.4.0 + + * allow retrieving the current event (`aasm.current_event`) (see [issue #159](https://github.com/aasm/aasm/issues/159) and [issue #168](https://github.com/aasm/aasm/issues/168)) + ## 3.3.3 * bugfix: support reloading development environment in Rails (see [issue #148](https://github.com/aasm/aasm/issues/148)) diff --git a/README.md b/README.md index d2278e3..2d58283 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,31 @@ originating state (the from-state) and the target state (the to state), like thi end ``` +#### The current event triggered + +While running the callbacks you can easily retrieve the name of the event triggered +by using `aasm.current_event`: + +```ruby + # taken the example callback from above + def do_something + puts "triggered #{aasm.current_event}" + end +``` + +and then + +```ruby + job = Job.new + + # without bang + job.sleep # => triggered :sleep + + # with bang + job.sleep! # => triggered :sleep! +``` + + ### Guards Let's assume you want to allow particular transitions only if a defined condition is diff --git a/lib/aasm/base.rb b/lib/aasm/base.rb index 559f105..7a44d38 100644 --- a/lib/aasm/base.rb +++ b/lib/aasm/base.rb @@ -55,10 +55,12 @@ module AASM end @klass.send(:define_method, "#{name.to_s}!") do |*args, &block| + aasm.current_event = "#{name.to_s}!".to_sym aasm_fire_event(name, {:persist => true}, *args, &block) end @klass.send(:define_method, "#{name.to_s}") do |*args, &block| + aasm.current_event = name.to_sym aasm_fire_event(name, {:persist => false}, *args, &block) end end diff --git a/lib/aasm/instance_base.rb b/lib/aasm/instance_base.rb index 151c3a8..ab54db8 100644 --- a/lib/aasm/instance_base.rb +++ b/lib/aasm/instance_base.rb @@ -1,7 +1,7 @@ module AASM class InstanceBase - attr_accessor :from_state, :to_state + attr_accessor :from_state, :to_state, :current_event def initialize(instance) @instance = instance diff --git a/spec/unit/event_spec.rb b/spec/unit/event_spec.rb index e8b3f73..939596b 100644 --- a/spec/unit/event_spec.rb +++ b/spec/unit/event_spec.rb @@ -244,6 +244,24 @@ describe 'should fire callbacks' do end end +describe 'current event' do + let(:pe) {ParametrisedEvent.new} + + it 'if no event has been triggered' do + expect(pe.aasm.current_event).to be_nil + end + + it 'if a event has been triggered' do + pe.wakeup + expect(pe.aasm.current_event).to eql :wakeup + end + + it 'if no event has been triggered' do + pe.wakeup! + expect(pe.aasm.current_event).to eql :wakeup! + end +end + describe 'parametrised events' do let(:pe) {ParametrisedEvent.new}