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

Allow subclassing AASM core classes #816

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Extract build_transition method to satisfy Code Climate complexity
  • Loading branch information
marcrohloff committed Feb 27, 2023
commit c31949a2ff97919e9068c30e74821d8fed7b41f9
14 changes: 9 additions & 5 deletions lib/aasm/core/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,13 @@ def ==(event)
## DSL interface
def transitions(definitions=nil, &block)
if definitions # define new transitions
transition_class = state_machine.implementation.aasm_transition_class
raise ArgumentError, "The class #{transition_class} must inherit from AASM::Core::Transition!" unless transition_class.ancestors.include?(AASM::Core::Transition)

# Create a separate transition for each from-state to the given state
Array(definitions[:from]).each do |s|
@transitions << transition_class.new(self, attach_event_guards(definitions.merge(:from => s.to_sym)), &block)
build_transition(attach_event_guards(definitions.merge(:from => s.to_sym)), &block)
end
# Create a transition if :to is specified without :from (transitions from ANY state)
if !definitions[:from] && definitions[:to]
@transitions << transition_class.new(self, attach_event_guards(definitions), &block)
build_transition(attach_event_guards(definitions), &block)
end
end
@transitions
Expand All @@ -110,6 +107,13 @@ def to_s

private

def build_transition(definitions=nil, &block)
transition_class = state_machine.implementation.aasm_transition_class
raise ArgumentError, "The class #{transition_class} must inherit from AASM::Core::Transition!" unless transition_class.ancestors.include?(AASM::Core::Transition)

@transitions << transition_class.new(self, definitions, &block)
end

def attach_event_guards(definitions)
unless @guards.empty?
given_guards = Array(definitions.delete(:guard) || definitions.delete(:guards) || definitions.delete(:if))
Expand Down