Skip to content

Commit

Permalink
aasm_from_states_for_state now supports to filter for specific transi…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
alto committed Oct 19, 2012
1 parent 5075462 commit a70de40
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 3.0.12

* aasm_from_states_for_state now supports to filter for specific transition

## 3.0.11

* added class method aasm_from_states_for_state to retrieve all from states (regarding transitions) for a given state
Expand Down
8 changes: 6 additions & 2 deletions lib/aasm/aasm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ def aasm_initial_state(set_state=nil)
end
end

def aasm_from_states_for_state(state)
aasm.events.map {|k,v| v.transitions_to_state(state)}.flatten.map(&:from).flatten
def aasm_from_states_for_state(state, options={})
if options[:transition]
aasm.events[options[:transition]].transitions_to_state(state).flatten.map(&:from).flatten
else
aasm.events.map {|k,v| v.transitions_to_state(state)}.flatten.map(&:from).flatten
end
end

# deprecated
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helpers/models_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class AuthMachine
event :unpassify do
transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false }
end

event :unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| u.has_activated? }
transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| u.has_activation_code? }
Expand Down
7 changes: 6 additions & 1 deletion spec/unit/aasm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@
describe "aasm_from_states_for_state" do
it "should return all from states for a state" do
froms = AuthMachine.aasm_from_states_for_state(:active)
[:pending, :passive, :suspended].each {|from| froms.should include(:pending)}
[:pending, :passive, :suspended].each {|from| froms.should include(from)}
end

it "should return from states for a state for a particular transition only" do
froms = AuthMachine.aasm_from_states_for_state(:active, :transition => :unsuspend)
[:suspended].each {|from| froms.should include(from)}
end
end

Expand Down

0 comments on commit a70de40

Please sign in to comment.