-
-
Notifications
You must be signed in to change notification settings - Fork 229
User information update #30
Comments
I've been trying to do the same thing as you and even tried something similar to this but I always get this error: undefined local variable or method `current_user' It's somewhat annoying. I've tried numerous ways of passing current_user in the controller as well, but nothing seems to take. I feel like it's something obvious I'm just missing but I cannot, for the life of me, figure it out. :-\ |
Yeah, I'm trying to figure that out too... I might start trolling acts_as_audited's code for a solution. |
I'm running into the same issue. Did you guys ever figure out a way to get the user info in the versions table? |
I figured it out!
config/initializers/vestal_versions.rb:
app/models/audit_version.rb:
|
Thanks for the follow-up. Sounds interesting, but not 100% there yet? I don't have a problem getting the current_user in the controller, that's available. The problem is VestalVersions doesn't do anything with it. Here's an example code snippet of when I update a Book. Which is where I need the user info to be stored in the versions table: What do you think? |
Yeah, I don't think I explained myself properly. The method I outlined helps with everything you need regarding getting the current_user set when the version record gets created. SentientUser grabs the current_user from the controller and allows you to access it in the model. Then, the alternate version model (I called mine AuditVersion) has a before_save hook to grab the User.current (a method added by SentientUser) and assign it to the version that's being created. That's how I get VestalVersions to set the current user before saving itself. |
Ah very nice, I'm sure I'll have a need for that in the future so thanks for the info. Have you had any luck getting the user info saved on the updates, like I'm been failing to achieve as seen in the pastebin :) ? Any advice? |
The method I use should work either way, actually. |
Plus it'll do it in a really DRY way. No need to edit every controller for every model. It just adds the current_user into the version automatically for every versioned model. |
First off, great work!
Next, I'd like to clarify as to how do I set the user information? I know there is 'updated_by' to do the job, but where do I call it? If I have versioned a model, and my requirement is to set user information automatically when the entry is made into versions table. Right now, I have created my own callback that does the job like -
after_update :set_user_to_version
def set_user_to_version
self.versions.last.user = current_user
end
Is that the right way to do it? Also, when I try to execute 'revert_to!(<version_no>), 'set_user_to_version' is not invoked. So the state now is that user information is set in updates but not in reverts. Please elaborate a bit on this.
The text was updated successfully, but these errors were encountered: