Hi everyone. I''m writing the admin area of a website, which has several users. The id of the current user is stored in the session. There are several models that the user can edited (article, link, customer etc), and whenever a user edits a model I would like to record this fact in a textfield of the model, so that lists of the various models can display who last edited them. I wrote a module to include into each model that wants this behaviour, but then I realised I can''t get at the user_id from the model, and that I would have to go edit all my controllers, passing in the user_id to each different model wherever an instance is created. This is obviously hairy duplication, and I''m sure there is a clever way to factor it out, I just can''t see where. Cheers. Robie _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Have a look at the way Bruce Perens has done it in ModelSecurity (http://perens.com/FreeSoftware/ModelSecurity/Tutorial.html). Basically he creates a class attribute on User called ''current'', and uses a before_filter to pull the current user out of the session and set it in the User class. It''s quite a neat solution, and one that I''m adopting in my own code. Several of my tables have a ''created_by_id'' that automatically gets set to the id of the current user on creation, and that can be done entirely in the model. Pete Yandell On 22/11/2005, at 9:42 PM, Robbie Carlton wrote:> Hi everyone. > > I''m writing the admin area of a website, which has several users. > The id of the current user is stored in the session. There are > several models that the user can edited (article, link, customer > etc), and whenever a user edits a model I would like to record this > fact in a textfield of the model, so that lists of the various > models can display who last edited them. I wrote a module to > include into each model that wants this behaviour, but then I > realised I can''t get at the user_id from the model, and that I > would have to go edit all my controllers, passing in the user_id to > each different model wherever an instance is created. This is > obviously hairy duplication, and I''m sure there is a clever way to > factor it out, I just can''t see where. > > Cheers. > > Robie > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks. Thats perfect. On 11/22/05, Pete Yandell <pete-AylieETRJAdBDgjK7y7TUQ@public.gmane.org> wrote:> > Have a look at the way Bruce Perens has done it in ModelSecurity > (http://perens.com/FreeSoftware/ModelSecurity/Tutorial.html). > > Basically he creates a class attribute on User called ''current'', and > uses a before_filter to pull the current user out of the session and > set it in the User class. > > It''s quite a neat solution, and one that I''m adopting in my own code. > Several of my tables have a ''created_by_id'' that automatically gets > set to the id of the current user on creation, and that can be done > entirely in the model. > > Pete Yandell > > > On 22/11/2005, at 9:42 PM, Robbie Carlton wrote: > > > Hi everyone. > > > > I''m writing the admin area of a website, which has several users. > > The id of the current user is stored in the session. There are > > several models that the user can edited (article, link, customer > > etc), and whenever a user edits a model I would like to record this > > fact in a textfield of the model, so that lists of the various > > models can display who last edited them. I wrote a module to > > include into each model that wants this behaviour, but then I > > realised I can''t get at the user_id from the model, and that I > > would have to go edit all my controllers, passing in the user_id to > > each different model wherever an instance is created. This is > > obviously hairy duplication, and I''m sure there is a clever way to > > factor it out, I just can''t see where. > > > > Cheers. > > > > Robie > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails