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

Conversation

marcrohloff
Copy link

This change makes it possible to use custom sub-classes of the core, Transaction, Event and State classes.

This makes it possible to add to the DSL, for example:

  class AasmWithRequiredParams < AASM::Base
    class CustomEvent < AASM::Core::Event
      def required_params!(*keys)
        options[:before] ||= []
        options[:before] << ->(**args) do
          missing = keys - args.keys
          raise ArgumentError, "Missing required arguments #{missing.inspect}" unless missing == []
        end
      end
    end
  
    def aasm_event_class; RequiredParamsEvent; end
  end

  aasm with_klass: AasmWithRequiredParams do
    state :initialised, :initial => true
    state :filled_out

    event :fill_out do
      required_params! :user, :quantity
      transitions :from => :initialised, :to => :filled_out
    end
  end

With this DSL
instance.fill_out(user: 1, quantity: 3)
will succeed, but
instance.fill_out(user: 1)
will raise an exception

@marcrohloff
Copy link
Author

@anilmaurya I am not sure how you'd like to proceed with resolving the code climate issues. The change to core/event either triggers a complexity error or a too many methods error

@codeclimate
Copy link

codeclimate bot commented Mar 16, 2023

Code Climate has analyzed commit b08c94c and detected 0 issues on this pull request.

View more on Code Climate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant