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

Search

Your Host

A picture of me

I am François Beausoleil, a Ruby on Rails and Scala developer. During the day, I work on Seevibes, a platform to measure social interactions related to TV shows. At night, I am interested many things. Read my biography.

Top Tags

Books I read and recommend

Links

Projects I work on

Projects I worked on