Callbacks (or handling state transitions)

4 views
Skip to first unread message

Paul Horsfall

unread,
Sep 26, 2009, 5:36:15 AM9/26/09
to factory_girl
Imagine a user model with several states, and that to create a user
instance the in the correct state I might do something like:

@user = Factory(:user)
@user.register
@user.verify

I use that all the time, and I'd like to make it easier to create a
verified user. I could create a helper method, but I'd need to create
one for each state, and that seems messy.

What I think I'd like to be able to do is define a factory for a
verified user, and specify the state transitions in a callback,
something like this:

Factory.define :verified_user do |u|
u.name "Lucy"

after_build do |u|
u.register
u.verify
end
end

Does anyone have a good strategy for dealing with this situation?

Cheers

Mark Wilden

unread,
Sep 26, 2009, 9:23:51 AM9/26/09
to factor...@googlegroups.com
On Sat, Sep 26, 2009 at 2:36 AM, Paul Horsfall <hors...@googlemail.com> wrote:
>
> Factory.define :verified_user do |u|
>  u.name "Lucy"
>
>  after_build do |u|
>    u.register
>    u.verify
>  end
> end

I think that ability would be good, but I don't see a huge difference
between defining a new Factory for each state, and defining a new
helper method for each state.

///ark

Reply all
Reply to author
Forward
0 new messages