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

Issue with Subclass trying to use the Superclass state machine #64

Closed
etagwerker opened this issue Apr 12, 2013 · 9 comments
Closed

Issue with Subclass trying to use the Superclass state machine #64

etagwerker opened this issue Apr 12, 2013 · 9 comments
Assignees

Comments

@etagwerker
Copy link
Contributor

Hello,

It seems that subclassing is supported by aasm but I am getting an error which seems to be related to this.

I am getting:

Failure/Error: @subclass.update_state
  NoMethodError:
  undefined method `may_fire?' for nil:NilClass

My code looks like this:

class Superclass < ActiveRecord::Base
  include AASM

  aasm(column: 'state') do
    state :missing_details, initial: true
    state :invalid_details
    state :confirmed_details, after_enter: :after_confirm
    state :pending_details_confirmation
  end

  def update_state

     if may_add_details?
       add_details!
     end

     # More stuff

 end
end

class Subclass < Superclass
  # Subclass specific code
end

After debugging the code, I see that aasm is looking for the state machine inside Subclass and not finding it, as intended (or finding an empty state machine)

So, when it looks for the event add_details it is not finding it and then raising the undefined method error.

What's the expected behavior here? Is this correct or a bug?

I would expect aasm to be smart enough to know that:

  1. There is no state machine in Subclass
  2. Use the state machine in Superclass

Maybe there is something I am doing wrong.

Proposed solutions:

A. Add an inheritance section in the README
B. Change the way aasm looks up the state machine

I'd appreciate your feedback.

Thanks!

@etagwerker
Copy link
Contributor Author

For now I've fixed it adding this code:

class Subclass < Superclass

  def self.aasm
    self.parent.aasm
  end

end

I know it's not ideal and there is most likely a better way.

@ghost ghost assigned alto Apr 19, 2013
@alto
Copy link
Member

alto commented Apr 19, 2013

Subclassing is supported in AASM, and its specs show that it works. But there might be a case in your code, which is not covered yet. I will try to reproduce your error by adding a test. Maybe you can help me with that, any broken spec is appreciated!

@alto
Copy link
Member

alto commented Apr 19, 2013

Do you have the full backtrace for me?

etagwerker pushed a commit to etagwerker/aasm that referenced this issue Apr 30, 2013
@etagwerker
Copy link
Contributor Author

@alto I've added a test to aasm to check the use case that's failing in my case. It's in PR #65.

I see the test passing in my local environment, but then I go to my project and it still fails if I remove this from the subclass:

def self.aasm
  self.parent.aasm
end

I guess it's an issue with my dependencies. I am still investigating.

@alto
Copy link
Member

alto commented May 3, 2013

Keep me up-to-date with this, please! If I can help you with anything, let me know.

@alto
Copy link
Member

alto commented Aug 20, 2013

@etagwerker Did you find anything causing the trouble?

@etagwerker
Copy link
Contributor Author

@alto Sorry, no. I am still using this in the Subclass:

  def self.aasm
    self.parent.aasm
  end

@alto
Copy link
Member

alto commented Sep 24, 2013

That's not good. Anything I can do?

@etagwerker
Copy link
Contributor Author

No, I'm closing this issue until I have more info.

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

No branches or pull requests

2 participants