vestal_versions icon indicating copy to clipboard operation
vestal_versions copied to clipboard

User information update

Open jigyasa opened this issue 14 years ago • 9 comments

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.

jigyasa avatar Mar 15 '10 13:03 jigyasa

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. :-\

kkerley avatar Apr 24 '10 18:04 kkerley

Yeah, I'm trying to figure that out too... I might start trolling acts_as_audited's code for a solution.

sfsekaran avatar Jul 15 '10 23:07 sfsekaran

I'm running into the same issue. Did you guys ever figure out a way to get the user info in the versions table?

bhellman1 avatar Sep 12 '10 03:09 bhellman1

I figured it out!

  1. 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.
  2. Set up the vestal versions initializer

config/initializers/vestal_versions.rb: VestalVersions.configure do |config| config.class_name = 'AuditVersion' end

  1. 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

  1. Drink a margarita (or a beer). And maybe have some nuts. Now I'm kinda hungry.
  2. 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.

sfsekaran avatar Sep 16 '10 06:09 sfsekaran

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?

bhellman1 avatar Sep 16 '10 20:09 bhellman1

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.

sfsekaran avatar Sep 16 '10 21:09 sfsekaran

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?

bhellman1 avatar Sep 16 '10 21:09 bhellman1

The method I use should work either way, actually. It will do exactly what your pastebin code looks like it's trying to do.

sfsekaran avatar Sep 16 '10 21:09 sfsekaran

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.

sfsekaran avatar Sep 16 '10 21:09 sfsekaran