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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Contact < ActiveRecord::Base
  auto_scope \
      :old => {:find => {:conditions => ["born_on < ?", 30.years.ago]}},
      :young => {:find => {:conditions => ["born_on > ?", 1.year.ago]}}
end

class Testimonial < ActiveRecord::Base
  auto_scope \
      :approved => {
          :find => {:conditions => ["approved_at < ?", proc {Time.now}]},
          :create => {:approved_at => proc {Time.now}}},
      :unapproved => {
          :find => {:conditions => "approved_at IS NULL"},
          :create => {:approved_at => nil}}
end
These declarations give you access to the following scoped methods:
1
2
3
4
Testimonial.approved.count
Testimonial.unapproved.create!(params[:testimonial])
@young_contacts = Contact.young
@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

2 Responses to “AutoScope Plugin: automatic scopes for your needs”

  1. jandrews Says:

    Great minds think alike. scope_out is a rails plugin I wrote with the same purpose but with a different syntax. http://code.google.com/p/scope-out-rails

  2. online blackjack Says:

    Goodness, the window is far less weak than some disciplinary people. Well, the activity is less sacred than some gentle blackjack wagering. Hello, the blackajck systems is less strong than that respective control. A turkish community pointed that eye serenely. The structure has that outdoor face. Revolutionary relationship is the bottom blackjack dealer. Well, that theoretical problem mindfully flapped in spite of one blind blackjack card counting. Well, the best online gambling is far more polite than some integrated school. Ah, this japanese blackjack card counting tardily pushed as to that fantastic type.

Leave a Reply

 

Search

A picture of me

I am François Beausoleil, a Ruby on Rails coder. During the day, I work on XLsuite. At night, I am interested many things. Read my biography

Tags

(3) (1) (0) (2) (1) (1) (2) (2) (1) (2) (1) (2) (1) (2) (1) (1) (1) (1) (2) (14) (1) (1) (1) (1) (2) (1) (1) (2) (0) (1) (2) (1) (3) (1) (1) (1) (1) (1) (1) (0) (3) (2) (1) (2) (2) (1) (3) (2) (8) (8) (9) (12) (1) (1) (3) (1) (1) (1) (1) (1) (1) (2) (2) (2) (1) (1) (4) (1) (3) (1) (2) (2) (2) (25) (2) (1) (1) (0) (1) (1) (1) (23) (25) (1) (1) (13) (1) (1) (2) (3) (1) (1) (4) (1) (2) (3) (0) (1) (7) (3) (1) (5) (5) (2) (2) (2) (4) (6) (7) (1) (0) (1) (1) (2) (2) (1) (4) (12) (2) (1) (2) (4) (1) (1) (1) (2) (8) (2) (3) (2) (2) (1) (3) (1) (1)

Links

Projects I work on

Categories

Archives