Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AASM::NO_VALUE as to_state argument default value #484

Merged
merged 3 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/aasm/aasm.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module AASM
# this is used internally as an argument default value to represent no value
NO_VALUE = :_aasm_no_value

# provide a state machine for the including class
# make sure to load class methods as well
Expand Down
21 changes: 10 additions & 11 deletions lib/aasm/core/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def initialize_copy(orig)
# a neutered version of fire - it doesn't actually fire the event, it just
# executes the transition guards to determine if a transition is even
# an option given current conditions.
def may_fire?(obj, to_state=nil, *args)
def may_fire?(obj, to_state=::AASM::NO_VALUE, *args)
_fire(obj, {:test_only => true}, to_state, *args) # true indicates test firing
end

def fire(obj, options={}, to_state=nil, *args)
def fire(obj, options={}, to_state=::AASM::NO_VALUE, *args)
_fire(obj, options, to_state, *args) # false indicates this is not a test (fire!)
end

Expand Down Expand Up @@ -121,20 +121,19 @@ def attach_event_guards(definitions)
definitions
end

def _fire(obj, options={}, to_state=nil, *args)
def _fire(obj, options={}, to_state=::AASM::NO_VALUE, *args)
result = options[:test_only] ? false : nil
transitions = @transitions.select { |t| t.from == obj.aasm(state_machine.name).current_state || t.from == nil}
return result if transitions.size == 0

# If to_state is not nil it either contains a potential
# to_state or an arg
unless to_state == nil
if !to_state.respond_to?(:to_sym) || !transitions.map(&:to).flatten.include?(to_state.to_sym)
args.unshift(to_state)
to_state = nil
end
if to_state == ::AASM::NO_VALUE
to_state = nil
elsif to_state.respond_to?(:to_sym) && transitions.map(&:to).flatten.include?(to_state.to_sym)
# nop, to_state is a valid to-state
else
args.unshift(nil) if args.nil? || args.empty? # If single arg given which is nil, push it back to args
# to_state is an argument
args.unshift(to_state)
to_state = nil
end

transitions.each do |transition|
Expand Down
2 changes: 1 addition & 1 deletion lib/aasm/core/transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def invoke_callbacks_compatible_with_guard(code, record, args, options={})

case code
when Symbol, String
result = (record.__send__(:method, code.to_sym).arity != 0 ? record.__send__(code, *args) : record.__send__(code))
result = (record.__send__(:method, code.to_sym).arity == 0 ? record.__send__(code) : record.__send__(code, *args))
failures << code unless result
result
when Proc
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/complex_example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
it 'should be able to be unsuspended into active if polite' do
auth.suspend!
expect(auth.may_wait?(:waiting, :please)).to be true
auth.wait!(nil, :please)
auth.wait!(:please)
end

it 'should not be able to be unsuspended into active if not polite' do
auth.suspend!
expect(auth.may_wait?(:waiting)).not_to be true
expect(auth.may_wait?(:waiting, :rude)).not_to be true
expect {auth.wait!(nil, :rude)}.to raise_error(AASM::InvalidTransition)
expect {auth.wait!(:rude)}.to raise_error(AASM::InvalidTransition)
expect {auth.wait!}.to raise_error(AASM::InvalidTransition)
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/complex_multiple_example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
it 'should be able to wait into waiting if polite' do
auth.left_suspend!
expect(auth.may_left_wait?(:waiting, :please)).to be true
auth.left_wait!(nil, :please)
Copy link
Contributor Author

@teohm teohm Jul 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anilmaurya I noticed nil is explicitly included in argument list when triggering event in the specs. Is this a common in real usage?

This is different from the usage examples written in https://github.com/aasm/aasm/wiki/State-transitions-with-guard-clauses-that-require-arguments

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@teohm Including nil in argument list seems weird to me.

auth.left_wait!(:please)

auth.right_suspend!
expect(auth.may_right_wait?(:waiting)).to be false
auth.right_wait!(nil, :please)
auth.right_wait!(:please)
end

it 'should not be able to be unsuspended into active if not polite' do
auth.left_suspend!
expect(auth.may_left_wait?(:waiting)).not_to be true
expect(auth.may_left_wait?(:waiting, :rude)).not_to be true
expect {auth.left_wait!(nil, :rude)}.to raise_error(AASM::InvalidTransition)
expect {auth.left_wait!(:rude)}.to raise_error(AASM::InvalidTransition)
expect {auth.left_wait!}.to raise_error(AASM::InvalidTransition)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/event_multiple_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end

it 'should transition to default state when :after transition invoked' do
pe.dress!(nil, 'purple', 'dressy')
pe.dress!('purple', 'dressy')
expect(pe.aasm(:left).current_state).to eq(:working)
end

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
obj = double('object', :aasm => double('aasm', :current_state => :open))
expect(obj).to receive(:guard_fn).with('arg1', 'arg2').and_return(true)

expect(event.fire(obj, {}, nil, 'arg1', 'arg2')).to eq(:closed)
expect(event.fire(obj, {}, 'arg1', 'arg2')).to eq(:closed)
end

context 'when given a gaurd proc' do
Expand All @@ -118,7 +118,7 @@
line_number = __LINE__ - 2
obj = double('object', :aasm => double('aasm', :current_state => :student))

event.fire(obj, {}, nil)
event.fire(obj, {})
expect(event.failed_callbacks).to eq ["#{__FILE__}##{line_number}"]
end
end
Expand All @@ -133,7 +133,7 @@
obj = double('object', :aasm => double('aasm', :current_state => :student))
allow(obj).to receive(:paid_tuition?).and_return(false)

event.fire(obj, {}, nil)
event.fire(obj, {})
expect(event.failed_callbacks).to eq [:paid_tuition?]
end
end
Expand Down Expand Up @@ -315,7 +315,7 @@
end

it 'should transition to default state when :after transition invoked' do
pe.dress!(nil, 'purple', 'dressy')
pe.dress!('purple', 'dressy')
expect(pe.aasm.current_state).to eq(:working)
end

Expand Down