vestal_versions
vestal_versions copied to clipboard
User information update
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.
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!
- Install and configure SentientUser by installing the plugin, requiring 'sentient_user' in your application controller and user model files, and including the appropriate modules for each.
- Set up the vestal versions initializer
config/initializers/vestal_versions.rb: VestalVersions.configure do |config| config.class_name = 'AuditVersion' end
- Then, add the new version model to your models directory
app/models/audit_version.rb: class AuditVersion < VestalVersions::Version before_save :use_current_user_if_necessary def use_current_user_if_necessary self.user = User.current if !self.user && User.current.present? end end
- Drink a margarita (or a beer). And maybe have some nuts. Now I'm kinda hungry.
- Oh, btw you might want to tailor the use_current_user_as_necessary function to what you need. You might want it to always set the user, or only after_create, but I woud test things out a lot after making changes.
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: http://pastebin.com/uHxzNgHC
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. It will do exactly what your pastebin code looks like it's trying to do.
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.