
after hooks need more awareness
Reported by Troy K | June 25th, 2008 @ 10:54 AM
Best I can tell, after hooks are left in the dark as to what has transpired. Specifically, for CRUD operations, after hooks always fire regardless of the result, and there appears no way to obtain the result. I suggest either:
1) not firing after create,update,destroy,save is the result is false.
2) having the result of create,update,destroy,save exposed through created?,updated?,destroyed?,saved?
We are currently having to bypass the aspect love and do stuff like
... dm::resource ...
def save
result = super
if result
Activity.log(self)
Searcher.update(self)
end
result
end
def destroy
result = super
if result
Activity.log(self)
Searcher.remove(self)
end
result
end
but would rather do something like:
... dm::resource ...
after :save do
# this would never get called if save == false
Activity.log(self)
Searcher.update(self)
end
or
... dm::resource ...
after :save do
return unless saved?
Activity.log(self)
Searcher.update(self)
end
The astute may recommend using transactions and before hooks, but that would require the hooks to be attached to both after create (need the id) and before update, instead of the nice clean, DRY after update.
I can submit a patch implementing your preferred approach, if any.
thanks -- troy
Comments and changes to this ticket
-
Carl Lerche July 5th, 2008 @ 02:05 PM
What if after hooks could accept the return value of the function as an argument.
after :save do |saved| throw(:halt) unless saved # Or return if you want the rest of the hooks to run ... end
-
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 »