Well, in relation to my Useful #with_scope technique post, here’s a plugin that implements that idea. This code is used on a production system. It works perfectly for my needs at the moment.
AutoScope
Automatically create scoped access methods on your ActiveRecord models.
Examples
Declare your scopes within your ActiveRecord::Base subclasses.
1 class Contact < ActiveRecord::Base 2 auto_scope \ 3 :old => {:find => {:conditions => ["born_on < ?", 30.years.ago]}}, 4 :young => {:find => {:conditions => ["born_on > ?", 1.year.ago]}} 5 end 6 7 class Testimonial < ActiveRecord::Base 8 auto_scope \ 9 :approved => { 10 :find => {:conditions => ["approved_at < ?", proc {Time.now}]}, 11 :create => {:approved_at => proc {Time.now}}}, 12 :unapproved => { 13 :find => {:conditions => "approved_at IS NULL"}, 14 :create => {:approved_at => nil}} 15 end
These declarations give you access to the following scoped methods:
1 Testimonial.approved.count 2 Testimonial.unapproved.create!(params[:testimonial]) 3 @young_contacts = Contact.young 4 @contacts = Contact.old.find(:all, :conditions => ["name LIKE ?", params[:name]])
The plugin’s home page is: http://xlsuite.org/plugins/auto_scope
The plugin’s Subversion repository is: http://svn.xlsuite.org/plugins/auto_scope