feature request or how to implement: Created_by automatically filled by session variable

29 views
Skip to first unread message

Gianna Giavelli

unread,
Apr 29, 2015, 9:16:17 PM4/29/15
to activejd...@googlegroups.com
I'd like to have some audit in my tables of who made the record change. 

It would be nice if I could have a "created_by" like "created_at" that auto-filled upon save, using a userid value from the session

Is there an easy way to do some kind of IOC intercept and make that happen globally? If not, please consider it a feature request. 

thanks

Gia

Igor Polevoy

unread,
Apr 30, 2015, 1:15:25 AM4/30/15
to activejd...@googlegroups.com
Gianna,. this will not work, as I do not want to make ActiveJDBC to be dependent on ActiveWeb.

What you can do, is create a parent controller and extend it by all other controllers. 
In the parent controller, you can create a method like this: 

public class MySuperController<T extends Model> extends AppController{
  protected T createModel(Class<T> clazz){
         User user = (User)session("user");
         Model m = clazz.newInstance();
         m.set("user_id", user.getId());
  }
}

Then, in your child controller you can:

public class BookController extends MySuperController{
  @POST
  public void save(){
     Person p = createModel(Person.class);
     /// more code 
  }
}

will this work?

Gianna Giavelli

unread,
May 3, 2015, 4:16:04 AM5/3/15
to activejd...@googlegroups.com
I think that will work. But for the models which do not contain a created_by column I don't want to fail, how do I check to see if the model contains the field? I won't have it in EVERY table. Yes, I could only extend the supercontroller in the cases where I DO have the column in the model but then I that defeats the "don't worry about it it just works" nice-ness of the feature

thanks

Gia

Igor Polevoy

unread,
May 3, 2015, 12:13:12 PM5/3/15
to activejd...@googlegroups.com
this is easy, just add a condition: 


protected T createModel(Class<T> clazz){
         User user = (User)session("user");
         Model m = clazz.newInstance();
         if(m.attributeNames().contains("created_by"))
             m.set("user_id", user.getId());
  }


Thanks

Igor Polevoy

unread,
May 3, 2015, 12:13:47 PM5/3/15
to activejd...@googlegroups.com
Sorry, I meant: 
protected T createModel(Class<T> clazz){
         User user = (User)session("user");
         Model m = clazz.newInstance();
         if(m.attributeNames().contains("user_id"))

Gianna Giavelli

unread,
May 3, 2015, 3:13:56 PM5/3/15
to activejd...@googlegroups.com
awesome! I'll integrate it right away!
Reply all
Reply to author
Forward
0 new messages