bugfix: take private methods into account when checking for callbacks #197

This commit is contained in:
Thorsten Böttger 2014-12-20 20:12:15 +01:00
parent 046dad9dfd
commit 763957f6ec
4 changed files with 63 additions and 2 deletions

View File

@ -5,6 +5,10 @@
* `aasm_column` has been removed. Use `aasm.attribute_name` instead
* `aasm_human_event_name` has been removed. Use `aasm.human_event_name` instead
## 4.0.7
* bugfix: take private methods into account when checking for callbacks (see [issue #197](https://github.com/aasm/aasm/issues/197) for details)
## 4.0.6
* bugfix: `false` is treated as uninitialised state (same as `nil`) (see [issue #195](https://github.com/aasm/aasm/issues/195) for details)

View File

@ -124,8 +124,8 @@ module AASM::Core
def invoke_callbacks(code, record, args)
case code
when Symbol, String
unless record.respond_to?(code)
raise NoMethodError.new("NoMethodError: undefined method `#{code}' for #{self.inspect}:#{self.class}")
unless record.respond_to?(code, true)
raise NoMethodError.new("NoMethodError: undefined method `#{code}' for #{record.inspect}:#{record.class}")
end
arity = record.send(:method, code.to_sym).arity
record.send(code, *(arity < 0 ? args : args[0...arity]))

View File

@ -0,0 +1,44 @@
module Callbacks
class PrivateMethod
include AASM
def initialize(options={})
@fail_event_guard = options[:fail_event_guard]
@fail_transition_guard = options[:fail_transition_guard]
@log = options[:log]
reset_data
end
def reset_data
@data = []
end
def data
@data.join(' ')
end
aasm do
state :open, :initial => true
state :closed
event :close, :after => :after_event do
transitions :to => :closed, :from => [:open]
end
event :open, :after => :after_event do
transitions :to => :open, :from => :closed
end
end
def log(text)
@data << text
puts text if @log
end
def aasm_write_state(*args); log('aasm_write_state'); true; end
private
def after_event; log('after_event'); end
end
end

View File

@ -52,6 +52,18 @@ describe 'callbacks for the new DSL' do
}.to raise_error(AASM::InvalidTransition)
end
it "it handles private callback methods as well" do
show_debug_log = false
callback = Callbacks::PrivateMethod.new(:log => show_debug_log)
callback.aasm.current_state
# puts "------- close!"
expect {
callback.close!
}.to_not raise_error
end
context "if the transition guard fails" do
it "does not run any state callback if guard is defined inline" do
show_debug_log = false
@ -279,4 +291,5 @@ describe 'event callbacks' do
@foo.close!
end
end
end