I''m pleased to announce a new plugin for Rails: Userstamp. You can read my blog post at http://www.delynnberry.com/articles/2006/01/20/userstamp-plugin and/or read all about it at the perminant page http://www.delynnberry.com/pages/userstamp. Any comments or suggestions for improvement are much appreciated. -- DeLynn Berry delynn@gmail.com http://www.delynnberry.com A dump of the Readme file is contained below -- Userstamp Plugin (v 1.0) ======================= Overview -------- The Userstamp Plugin extends ActiveRecord::Base( http://api.rubyonrails.com/classes/ActiveRecord/Base.html) to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp( http://api.rubyonrails.com/classes/ActiveRecord/Timestamp.html) module updates created_(at/on) and updated_(at/on) attributes. The module requires that your application''s user object (User by default) contains an accessor called current_user be set with an instance of the currently logged in user (typically using a before_filter( http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html#M000127). This module can also be turned off on a case by case basis by setting the record_userstamps attribute of your ActiveRecord object to false. Installation ------------ To install the Userstamp plugin into a current Rails application run the script/plugin script from the root of your application passing it the url of http://www.delynnberry.com/svn/code/rails/plugins/userstamp/. For example: script/plugin install http://www.delynnberry.com/svn/code/rails/plugins/userstamp Once installed you will need to restart your webserver for the plugin to be loaded into the Rails environment. Usage ----- Here is a simple example for how to use the Userstamp plugin. First, create a User model object (either using the generator or manually creating the file). Adjust your model to include the current_user accessor like so: class User < ActiveRecord::Base cattr_accessor :current_user end Second, create another table and model that will use the Userstamp functionality (I''m using Post for this example). Be sure to add the created_by and updated_by columns to your table definition and also create a belongs_to relationship. For example: class Post < ActiveRecord::Base belongs_to :created_by, :class_name => "User", :foreign_key => "created_by" belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by" end Then in your ApplicationController create a before_filter to automatically set the current_user: class ApplicationController < ActionController::Base before_filter do |c| User.current_user = User.find(c.session[:user].id) unless c.session[:user].nil? end end Uninstall --------- Uninstalling is simply a matter of running script/plugin from the root of your Rails application. Except this time you pass the uninstall parameter: script/plugin unistall userstamp Documentation ------------- RDoc has been run on the plugin directory and is available in the download. Running Unit Tests ------------------ There are extensive unit tests in the "test" directory of the plugin. Currently, only MySQL is supported, but you should be able to easily fix this by looking at "connection.rb". You''ll need to create a database for the tests and put the connection information into " connection.rb" as well as import the schema file for MySQL that can be found at "test/fixtures/mysql.sql". To run the test simply execute the follow from the test directory inside the Userstamp plugin directory: ruby userstamp_test.rb Bugs & Feedback --------------- Bug reports and feedback are always welcome. Please send them to delynn@gmail.com with [Userstamp] in the subject line. You can also visit ( http://www.delynnberry.com/articles/category/userstamp/) and post a comment on any of the posts. Credits and Special Thanks -------------------------- The original idea for this plugin came from the Rails Wiki article entitled "Extending ActiveRecord" ( http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample ). Special Thanks to Ben Reubenstein (benr75@gmail.com) for helping me stress test this plugin. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060120/f2548f72/attachment-0001.html
Thx for not naming it ActiveStamp ... ;) All joking aside I have your early version running in my app, and it works as described. Thx, ~ Ben On 1/20/06, DeLynn Berry <delynn@gmail.com> wrote:> > I''m pleased to announce a new plugin for Rails: Userstamp. You can read my > blog post at http://www.delynnberry.com/articles/2006/01/20/userstamp-plugin > and/or read all about it at the perminant page > http://www.delynnberry.com/pages/userstamp . Any comments or suggestions > for improvement are much appreciated. > > -- > DeLynn Berry > delynn@gmail.com > http://www.delynnberry.com > > A dump of the Readme file is contained below > > -- > Userstamp Plugin (v 1.0) > =======================> > Overview > -------- > The Userstamp Plugin extends ActiveRecord::Base(http://api.rubyonrails.com/classes/ActiveRecord/Base.html > ) to add automatic updating of created_by and updated_by attributes of > your models in much the same way that the ActiveRecord::Timestamp( > http://api.rubyonrails.com/classes/ActiveRecord/Timestamp.html) module > updates created_(at/on) and updated_(at/on) attributes. > > The module requires that your application''s user object (User by default) > contains an accessor called current_user be set with an instance of the > currently logged in user (typically using a before_filter( http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html#M000127 > ). This module can also be turned off on a case by case basis by setting > the record_userstamps attribute of your ActiveRecord object to false. > > Installation > ------------ > To install the Userstamp plugin into a current Rails application run the > script/plugin script from the root of your application passing it the url of > http://www.delynnberry.com/svn/code/rails/plugins/userstamp/. For example: > > script/plugin install http://www.delynnberry.com/svn/code/rails/plugins/userstamp > > > Once installed you will need to restart your webserver for the plugin to > be loaded into the Rails environment. > > Usage > ----- > Here is a simple example for how to use the Userstamp plugin. First, > create a User model object (either using the generator or manually creating > the file). Adjust your model to include the current_user accessor like so: > > class User < ActiveRecord::Base > cattr_accessor :current_user > end > > Second, create another table and model that will use the Userstamp > functionality (I''m using Post for this example). Be sure to add the > created_by and updated_by columns to your table definition and also create a > belongs_to relationship. For example: > > class Post < ActiveRecord::Base > belongs_to :created_by, :class_name => "User", :foreign_key => > "created_by" > belongs_to :updated_by, :class_name => "User", :foreign_key => > "updated_by" > end > > Then in your ApplicationController create a before_filter to automatically > set the current_user: > > class ApplicationController < ActionController::Base > before_filter do |c| > User.current_user = User.find(c.session[:user].id) unless > c.session[:user].nil? > end > end > > > Uninstall > --------- > Uninstalling is simply a matter of running script/plugin from the root of > your Rails application. Except this time you pass the uninstall parameter: > > script/plugin unistall userstamp > > Documentation > ------------- > RDoc has been run on the plugin directory and is available in the > download. > > Running Unit Tests > ------------------ > There are extensive unit tests in the "test" directory of the plugin. > Currently, only MySQL is supported, but > you should be able to easily fix this by looking at "connection.rb". > You''ll need to create a > database for the tests and put the connection information into " > connection.rb" as well as import the schema file > for MySQL that can be found at "test/fixtures/mysql.sql". > > To run the test simply execute the follow from the test directory inside > the Userstamp plugin directory: > > ruby userstamp_test.rb > > Bugs & Feedback > --------------- > Bug reports and feedback are always welcome. Please send them to > delynn@gmail.com with [Userstamp] in the subject line. > You can also visit ( > http://www.delynnberry.com/articles/category/userstamp/) and post a > comment on any of the posts. > > Credits and Special Thanks > -------------------------- > The original idea for this plugin came from the Rails Wiki article > entitled "Extending ActiveRecord" (http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample > ). > Special Thanks to Ben Reubenstein (benr75@gmail.com) for helping me stress > test this plugin. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Ben Reubenstein http://www.benr75.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060120/8a2dae0b/attachment.html
> class Post < ActiveRecord::Base > belongs_to :created_by, :class_name => "User", :foreign_key => > "created_by" > belongs_to :updated_by, :class_name => "User", :foreign_key => > "updated_by" > endWhy created_by/updated_by? Why not creator/updater? @post.created_by.login vs @post.creator.login -- Rick Olson http://techno-weenie.net
belongs_to :creator, :class_name => "User", :foreign_key => "created_by" belongs_to :updater, :class_name => "User", :foreign_key => "updated_by" I think the goal was to follow existing conventions, created_on, updated_on, etc... ~ Ben On 1/20/06, Rick Olson <technoweenie@gmail.com> wrote:> > > class Post < ActiveRecord::Base > > belongs_to :created_by, :class_name => "User", :foreign_key => > > "created_by" > > belongs_to :updated_by, :class_name => "User", :foreign_key => > > "updated_by" > > end > > Why created_by/updated_by? Why not creator/updater? > > @post.created_by.login > > vs > > @post.creator.login > > -- > Rick Olson > http://techno-weenie.net > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein http://www.benr75.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060120/7e298e1e/attachment-0001.html
Rick, The primary reason for choosing the created_by and updated_by column names was primarily to follow Timestamp''s created_(at/on) updated_(at/on) naming convention. Like Ben stated, you can call the relationship "name" whatever you want when you set it up in your model. -- DeLynn Berry delynn@gmail.com
On 1/20/06, DeLynn Berry <delynn@gmail.com> wrote:> Rick, > > The primary reason for choosing the created_by and updated_by column > names was primarily to follow Timestamp''s created_(at/on) > updated_(at/on) naming convention. > > Like Ben stated, you can call the relationship "name" whatever you > want when you set it up in your model.Well, I prefer to follow the conventions rails has for associations instead. I was just making an observation though. Have fun. -- Rick Olson http://techno-weenie.net
Rick Olson wrote:>>The primary reason for choosing the created_by and updated_by column >>names was primarily to follow Timestamp''s created_(at/on) >>updated_(at/on) naming convention. >> >>Like Ben stated, you can call the relationship "name" whatever you >>want when you set it up in your model. > > Well, I prefer to follow the conventions rails has for associations > instead. I was just making an observation though. Have fun. >There''s an idea. Since this is a plugin, why not add a class method which expresses the relationship better and adds the functionality to the model. Something like this: class Post < ActiveRecord::Base owned_by :user end It could wrap the belongs_to calls. And perhaps the stuff for the user class could be put in a class method too: class User < ActiveRecord::Base acts_as_user end Wow. Very nice. This would clean things up quite a bit. Maybe you could even figure out a way to remove the before filter from the Application class as well. -- John Long http://wiseheartdesign.com
I had done something like that. You have to love easy metaprogramming. module ActiveRecord module Userstamp... class Base include Userstamp class << self # Allows class declaration of creating association # creates :foos # Automatically creates #created_foos which use # a ''creator_id'' foreign key. def creates( *args ) created_symbols = ( ''created_'' + args[0].to_s ).to_sym created_class = args[0].to_s.singularize.camelize created_hash = { :foreign_key => ''creator_id'', :class_name => created_class } created_hash.merge( args[1] ) if args[1].is_a?( Hash ) has_many created_symbols, created_hash end def created_by( *args ) creator_symbol = args[0] creator_hash = { :foreign_key => ''creator_id'', :class_name => ''User'' } creator_hash.merge( args[1] ) if args[1].is_a?( Hash ) belongs_to creator_symbol, creator_hash end end end end I use ''creator_id'' instead of created_by for the table field. The above code makes the association look more beautiful. class User < ActiveRecord::Base creates :documents has_many :documents creates :programs end class Document < ActiveRecord::Base created_by :creator end Note that in this example a user is the creator of documents as well as the owner of documents. A user with privileges, like a moderator, could create documents but might not actually own the documents. -Bill On 1/20/06, John W. Long <ng@johnwlong.com> wrote:> > There''s an idea. Since this is a plugin, why not add a class method > which expresses the relationship better and adds the functionality to > the model. Something like this: > > class Post < ActiveRecord::Base > owned_by :user > end > > I-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060121/24b112c5/attachment.html
Has anyone received an error when using this new plugin? I cannot do anything with it installed as apparently there is a constructor missing ''UserStamp''. I''ve looked at the init.rb file and it seems to all be in order. I can''t do anything, start WebBRick, generate a model, nothing. I''ve posted the error at the bottom. I don''t know enough about plugins to properly troubleshoot though. Do I need to do something to the environment.rb file? The instructions didn''t say to, but I''m wondering if I do because I''ve also got Engines and a couple of engines installed. If anyone has an idea, I would appreciate any help. ruby script/generate model User c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:195:in `const_missing'': uninitialized constant Userstamp (NameError) from ./script/../config/../vendor/plugins/userstamp/init.rb:7:in `load_plugin'' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:311:in `load_plugin'' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:311:in `silence_warnings'' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:311:in `load_plugin'' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:145:in `load_plugins'' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:145:in `each'' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:145:in `load_plugins'' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:100:in `process'' ... 9 levels... from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require'' from ./script/generate:3 from -e:3:in `load'' from -e:3 -- Posted via http://www.ruby-forum.com/.
Hi Josh ~ You can follow up with DeLynn who wrote the plug-in. I know he would love some feedback if there is a bug. It might be that you have to have the table set up before you add in the plugin, but that is just a guess. I have it working in my app. http://www.delynnberry.com ~ Ben On 1/22/06, Joshua Schairbaum <jschairbaum@yahoo.com> wrote:> Has anyone received an error when using this new plugin? I cannot do > anything with it installed as apparently there is a constructor missing > ''UserStamp''. I''ve looked at the init.rb file and it seems to all be in > order. I can''t do anything, start WebBRick, generate a model, nothing. > I''ve posted the error at the bottom. > > > I don''t know enough about plugins to properly troubleshoot though. Do I > need to do something to the environment.rb file? The instructions > didn''t say to, but I''m wondering if I do because I''ve also got Engines > and a couple of engines installed. > > If anyone has an idea, I would appreciate any help. > > ruby script/generate model User > c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:195:in > `const_missing'': uninitialized constant Userstamp (NameError) > from ./script/../config/../vendor/plugins/userstamp/init.rb:7:in > `load_plugin'' > from > c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:311:in > `load_plugin'' > from > c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:311:in > `silence_warnings'' > from > c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:311:in > `load_plugin'' > from > c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:145:in > `load_plugins'' > from > c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:145:in > `each'' > from > c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:145:in > `load_plugins'' > from > c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/initializer.rb:100:in > `process'' > ... 9 levels... > from > c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in > `require'' > from ./script/generate:3 > from -e:3:in `load'' > from -e:3 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein http://www.benr75.com
> I''ve looked at the init.rb file and it seems to all be in > order. I can''t do anything, start WebBRick, generate a model, nothing. > I''ve posted the error at the bottom.There was actually a problem with the way the init file was sending the Userstamp module into ActiveRecord. Line 7 of the init.rb should read: ActiveRecord::Base.send(:include, ActiveRecord::Userstamp) I have updated this file in the subversion repository, so feel free to either edit the init.rb file yourself, or uninstall and re-install the plugin. Sorry for the inconvenience. -- DeLynn Berry delynn@gmail.com http://www.delynnberry.com
#if delynn /* Jan 20, 04:15 */> > I''m pleased to announce a new plugin for Rails: Userstamp. You can > read my blog post at > [1]http://www.delynnberry.com/articles/2006/01/20/userstamp-plugin > and/or read all about it at the perminant page > [2]http://www.delynnberry.com/pages/userstamp . Any comments or > suggestions for improvement are much appreciated.#endif /* delynn@gmail.com */ Hi there, Great plugin, not sure if you have seen [0]usermonitor.rb but it does a similar thing, albeit not as a module. One thing i had to hunt around for, in order to make your plugin work, was overriding the User Class. I''m storing them in People/Person, this was accomplished by the following in environment.rb: ActiveRecord::Base.user_model_name = ''Person'' Being a newbie to ruby/rails I''m not 100% certain this is the "correct" way of achieving this, but it worked for me. HTH anyone having the same problem. -v [0] http://rubyurl.com/JLA -- keys: http://codex.net/gpg.asc An honest politician is one who when he is bought will stay bought. -- Simon Cameron