mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Merge pull request #389 from anilmaurya/fix_guard_params_for_permissible_state
Fix [#388] guard parameter for permissible states
This commit is contained in:
commit
d174ff0f1e
6 changed files with 73 additions and 8 deletions
|
@ -935,6 +935,9 @@ job.aasm.events(:reject => :sleep).map(&:name)
|
|||
# list states for select
|
||||
Job.aasm.states_for_select
|
||||
=> [["Sleeping", "sleeping"], ["Running", "running"], ["Cleaning", "cleaning"]]
|
||||
|
||||
# show permitted states with guard parameter
|
||||
job.aasm.states({:permitted => true}, guard_parameter).map(&:name)
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -33,9 +33,9 @@ module AASM
|
|||
AASM::Localizer.new.human_state_name(@instance.class, state_object_for_name(current_state))
|
||||
end
|
||||
|
||||
def states(options={})
|
||||
def states(options={}, *args)
|
||||
if options.has_key?(:permitted)
|
||||
selected_events = events(:permitted => options[:permitted])
|
||||
selected_events = events({:permitted => options[:permitted]}, *args)
|
||||
# An array of arrays. Each inner array represents the transitions that
|
||||
# transition from the current state for an event
|
||||
event_transitions = selected_events.map {|e| e.transitions_from_state(current_state) }
|
||||
|
@ -45,10 +45,10 @@ module AASM
|
|||
return nil if transitions.empty?
|
||||
|
||||
# Return the :to state of the first transition that is allowed (or not) or nil
|
||||
if options[:permitted]
|
||||
transition = transitions.find { |t| t.allowed?(@instance) }
|
||||
if options[:permitted]
|
||||
transition = transitions.find { |t| t.allowed?(@instance, *args) }
|
||||
else
|
||||
transition = transitions.find { |t| !t.allowed?(@instance) }
|
||||
transition = transitions.find { |t| !t.allowed?(@instance, *args) }
|
||||
end
|
||||
transition ? transition.to : nil
|
||||
end.flatten.compact.uniq
|
||||
|
@ -60,7 +60,7 @@ module AASM
|
|||
end
|
||||
end
|
||||
|
||||
def events(options={})
|
||||
def events(options={}, *args)
|
||||
state = options[:state] || current_state
|
||||
events = @instance.class.aasm(@name).events.select {|e| e.transitions_from_state?(state) }
|
||||
|
||||
|
@ -71,9 +71,9 @@ module AASM
|
|||
# filters the results of events_for_current_state so that only those that
|
||||
# are really currently possible (given transition guards) are shown.
|
||||
if options[:permitted]
|
||||
events.select! { |e| @instance.send("may_#{e.name}?") }
|
||||
events.select! { |e| @instance.send("may_#{e.name}?", *args) }
|
||||
else
|
||||
events.select! { |e| !@instance.send("may_#{e.name}?") }
|
||||
events.select! { |e| !@instance.send("may_#{e.name}?", *args) }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
24
spec/models/guard_with_params.rb
Normal file
24
spec/models/guard_with_params.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
class GuardWithParams
|
||||
include AASM
|
||||
aasm do
|
||||
state :new, :reviewed, :finalized
|
||||
|
||||
event :mark_as_reviewed do
|
||||
transitions :from => :new, :to => :reviewed, :guards => [:user_is_manager?]
|
||||
end
|
||||
end
|
||||
|
||||
def user_is_manager?(user)
|
||||
ok = false
|
||||
if user.has_role? :manager
|
||||
ok = true
|
||||
end
|
||||
return ok
|
||||
end
|
||||
end
|
||||
|
||||
class GuardParamsClass
|
||||
def has_role?(role)
|
||||
true
|
||||
end
|
||||
end
|
18
spec/models/guard_with_params_multiple.rb
Normal file
18
spec/models/guard_with_params_multiple.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
class GuardWithParamsMultiple
|
||||
include AASM
|
||||
aasm(:left) do
|
||||
state :new, :reviewed, :finalized
|
||||
|
||||
event :mark_as_reviewed do
|
||||
transitions :from => :new, :to => :reviewed, :guards => [:user_is_manager?]
|
||||
end
|
||||
end
|
||||
|
||||
def user_is_manager?(user)
|
||||
ok = false
|
||||
if user.has_role? :manager
|
||||
ok = true
|
||||
end
|
||||
return ok
|
||||
end
|
||||
end
|
10
spec/unit/guard_with_params_multiple_spec.rb
Normal file
10
spec/unit/guard_with_params_multiple_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe "guards with params" do
|
||||
let(:guard) { GuardWithParamsMultiple.new }
|
||||
let(:user) {GuardParamsClass.new}
|
||||
|
||||
it "list permitted states" do
|
||||
expect(guard.aasm(:left).states({:permitted => true}, user).map(&:name)).to eql [:reviewed]
|
||||
end
|
||||
end
|
10
spec/unit/guard_with_params_spec.rb
Normal file
10
spec/unit/guard_with_params_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe "guards with params" do
|
||||
let(:guard) { GuardWithParams.new }
|
||||
let(:user) {GuardParamsClass.new}
|
||||
|
||||
it "list permitted states" do
|
||||
expect(guard.aasm.states({:permitted => true}, user).map(&:name)).to eql [:reviewed]
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue