Bernard,
I've applied your technique but found a problem.
I'm using a context in a before :save hook, but there is no place
where this context is referenced prior, so I get an error that the
context isn't defined.
gems/dm-validations-0.9.8/lib/dm-validations/contextual_validators.rb
in `execute' - 43
43 raise(ArgumentError, 'invalid context specified') if !named_context
|| (contexts.length > 0 &&
my before hook looks like this:
before :save do | context |
case context
...
when :password_reset_request
...
end
end
To get around it, I added an unneeded validation:
validates_with_method :activated?, :when => [:password_reset_request]
I can think of three solutions:
1 - DM doesn't check for invalid contexts
2 - somehow, you figure out which contexts get defined in hooks
(probably too hard)
3 - need to expose a way to simply add a context without defining a
validator
I'm guessing option 3 is the way to go.
What do you think?
thanks, Jon
On Dec 8, 3:38 am, "Bernerd Schaefer" <[email protected]> wrote:
> > I am using context with validations. I cannot tell if there is a way
> > to limit my before and after hooks by context as well. It looks as
> > though contexts are only in dm-validations.
>
> Jon,
>
> I pushed a fix a while back to dm-validations
> (http://github.com/sam/dm-more/commit/d574d29078b7f3afe087d4693f81ee43...)
> that makes hooking save with contexts possible.
>
> before :save do |context|
> # context will be:
> # :default - if you call user.save
> # nil - if you call user.save!
> # or, the context you passed in - user.save(:registration)
> end
>
> Bernerd Schaefer
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---