
before :valid? > ArgumentError: wrong number of arguments (1 for 0)
Reported by Maxime Guilbot | May 29th, 2008 @ 05:27 AM
This code: http://pastie.org/205299 is bringing an error on my setup.
before :save is working but before: valid? is causing this error.
I am using dm 0.9.1
Thanks,
Maxime
Comments and changes to this ticket
-
Maxime Guilbot May 29th, 2008 @ 05:45 AM
Note:
Passing a block to before :valid? is working just fine.
This is the way I deal with that now.
-
Guillaume Maury (giom) May 31st, 2008 @ 04:10 AM
This is because valid? is called with an argument inside save (the context which is set to :default by default).
The before and after hooks are called with the same arguments as the method they advise...
You can see the code calling the hooks in inline_hooks of hook.rb
method_def << " #{e}(#{args})\n"
I'm not sure if it should be fixed, but it might take people by surprise.
For example, in dm-core save doesn't take any arguments but if you add the dm-validations dependency save takes an optional context argument
If any call is done to save with a context, all the previously working validations will fail with this error.
So maybe we should check if the hook methods as an arity of 0 and call it without any arguments only in this case...
-
Maxime Guilbot May 31st, 2008 @ 04:30 PM
Hello Guillaume,
Thanks for your message and very clear explanations!
What surprises me more is the change between DM 0.9 and 0.9.1.. but that's ok :)
Thanks again,
Maxime
-
Carl Lerche July 6th, 2008 @ 12:21 PM
- State changed from new to open
- Tag set to question, validations
- Assigned user changed from Sam Smoot to Carl Lerche
I will be working this. Hooks will be able to be defined with no arguments and the same arity as the method being hooked.
-
Daniel Parker July 8th, 2008 @ 08:40 AM
I am using before :valid? :custom_method, and it is forcing me to allow it to send an argument, which happens to be the repository context (:default).
This is probably the same thing, I guess -- valid? is being called as model.valid?(:default), so it sends the argument to my custom_method too, but I really don't care about the argument at all there.
I guess I'm just restating what you guys said, but it explains it clearer for me.
-
Carl Lerche July 13th, 2008 @ 03:25 PM
- State changed from open to resolved
Hooks can now be defined without any arguments even if the method being hooked requires arguments. Hooks can still be declared with the arguments of the hookable method.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
People watching this ticket
Tags
Referenced by
-
371 A lot of funkiness going on with Hooks and dm-more The other tickets I found relating to Hooks are #343 and...