E52B Allow subclassing AASM core classes by marcrohloff · Pull Request #816 · aasm/aasm · GitHub
[go: up one dir, main page]

Skip to content

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

@qlty-cloud-legacy
Copy link

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.

1 participant

0