Thursday, December 13, 2007

Pope notes* - Rails - restful_authentication --stateful

* Pope notes - things that I've just worked on, and got halfway, but need to press on with. Like little papal snapshots, mainly for my reference so I can come back to it later.

I like state machines; they make life cycles much easier to manage and acts_as_state_machine is a nice way of expressing state machines. The problem I just hit though was that restful_authentication plugin just added --stateful, which generates up an authentication setup that includes the Stateful authentication setup. It went well enough getting it to work, but there seems to be a mismatch in statefulness and the use of an observer to generate mail. The observer watches save and creates, and then uses the state of the user to decide what should be done. But, it does this for all saves, and because there's no indication of a state change having just occurred, then having the after_save observer mail when the users state is active triggers off multiple "you have signed in mails" and the variant, checking for a pending state, triggers an activation mail. What struck me was it should be better to move the mail generation into the user model, using the same hooks that the state machine uses for when a state is entered. I had a quick hack, and it seems to work, but does generate two activation mails.... this is probably fixable if I understood the acts_as_state_machine plugin better, so I'm going back to another part of the code which does use that.

Oh, and I'm in day two of my "coding rails without an IDE"... it's all TextMate and Terminal here for the next fortnight. No better way to know what an IDE is doing right or wrong than getting back to basics.


3 comments:

Glenn Rempe said...

Hi,

I wonder if you ever figured out a good solution for this? I would love to see the code you used to work around this if possible.

Unknown said...

I'm afraid I took the pragmatic approach and pulled out the state machine based authenticator, and went off down the route of plain old stateless auth. IME, acts_as_state_machine is good for expressing states and transitions, less good at being an actor during transitions.

Eric said...

I'm inclined to follow codepope. I found this page trying to track down a problem with stateful auth generating emails with the wrong activiation code (by default, apparently). So far, no joy.