Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

User information update #30

Open
jigyasa opened this issue Mar 15, 2010 · 9 comments
Open

User information update #30

jigyasa opened this issue Mar 15, 2010 · 9 comments

Comments

@jigyasa
Copy link

jigyasa commented Mar 15, 2010

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.

@kkerley
Copy link

kkerley commented Apr 24, 2010

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

@sfsekaran
Copy link

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

@bhellman1
Copy link

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

@sfsekaran
Copy link

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.

@bhellman1
Copy link

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?

@sfsekaran
Copy link

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.

@bhellman1
Copy link

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?

@sfsekaran
Copy link

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
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants