> I have the following in user.rb:
>
> lifecycle do
> state :active, :default => true
> state :inactive
> state :invited
>
> ... some custom creators and transitions ...
> end
>
[snip - default question already answered]
> Also, I'd like to have a checkbox input on the "new user" form that sets the state to active or inactive (along with taking some other actions) depending on whether the box is checked. I was trying to use a before_filter on the create action to set the state, etc. but I'm not sure how to access the User. For example, @user is nil. My current understanding is that a new User is instantiated with the state already set when the "new form" is loaded.
I suspect the lifecycle creators are probably not the best path for this, since you really only want one form. The standard vanilla-Rails way to do this is with attr_accessor for the checkbox (in user.rb):
attr_accessor :send_activation, :type => :boolean
You'll need to add the field to your field-list on the 'new' page, but given that it's customized that shouldn't be an issue.
Then, in a model callback (again in user.rb), do something like:
before_validation :set_user_state, :on => :create
def set_user_state
self.state = 'inactive' if send_activation
end
This will override the default state as needed. I used before_validation because it's frequently the case that some user validations might be checking it, and setting it in before_create or before_save will be too late.
Then, to actually handle sending the email, use an after_create callback (or an observer) to fire off the activation email.
--Matt Jones