ANN: scoped_proxy plugin

ScopedProxy uses with_scope and proxy objects to make it easy to find
and
count different types of records.

Example:

class Person < ActiveRecord::Base
scoped_proxy :minor, :conditions => ‘age <= 17’
scoped_proxy :adult, :conditions => ‘age >= 18’
scoped_proxy :old, :conditions => ‘age >= 70’

scoped_proxy :male, :conditions => ‘gender = “male”’
scoped_proxy :female, :conditions => ‘gender = “female”’

scoped_proxy :single, :conditions => ‘married = 0’
scoped_proxy :married, :conditions => ‘married = 1’
end

Person.adult.find :all
Person.single.find :all

They can also be chained together.

Person.single.female.find :all
Person.old.male.count

They can be used directly as shown above or the scoped proxy object
itself can
be stored and passed to methods expecting an ActiveRecord object with
full
find and count capabilities.

single_female_finder = Person.single.female
single_female_finder.find :all

Download from:
http://www.jackchristensen.com/article/4/scopedproxy-plugin-for-ruby-on-rails

Jack

Ezra Z. wrote:

class Person < ActiveRecord::Base
scoped_proxy :minor, ‘age <= 17’
scoped_proxy :aduult, ‘age >= 18’

or even this maybe:

scoped_proxy :old => ‘age >= 70’
scoped_proxy :male => ‘gender = “male”’
scoped_proxy :female => ‘gender = “female”’
end

Eww ;), unnamed params. I think :conditions is also necessary if
additional params might be used (like :limit, :order, etc.).

Heh, had to change the param to :aduult to avoid the spam filters. :confused:

Joe

Hi~

On Jul 23, 2006, at 1:56 PM, Jack C. wrote:

scoped_proxy :male, :conditions => ‘gender = “male”’

single_female_finder.find :all

Download from:
http://www.jackchristensen.com/article/4/scopedproxy-plugin-for-
ruby-on-rails

Jack

Jack-

This looks like a cool plugin, nice and useful. I have only one

nitpick with it. It seems to me that the :conditions part of your
scoped_proxy class method is totally redundant. I think you could
easily have it work without the need to retype :conditions every time
since every time you call this method you could internally re-arrange
it into a hash with :conditions as your key. then the calls to your
scoped_proxy method could be kept DRY’er:

class Person < ActiveRecord::Base
scoped_proxy :minor, ‘age <= 17’
scoped_proxy :adult, ‘age >= 18’

or even this maybe:

scoped_proxy :old => ‘age >= 70’
scoped_proxy :male => ‘gender = “male”’
scoped_proxy :female => ‘gender = “female”’
end

Cheers-
-Ezra

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

additional params might be used (like :limit, :order, etc.).
I don’t think its that bad. In fact I would expect that as the
interface. Rails has tons of methods where the required arguments
are positional, and the options parameters are passed in last as
a hash.

In this case I think the scope name, and the condition are required
parameters so it makes sense to have it like this.

I’d suggest a method signature like this:

scoped_proxy(scope_name, conditions, options = {})


Dan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (Darwin)

iD8DBQFExBd/4DfZD7OEWk0RAv8PAJ4vaG05hduR/v+Ac2QDwdhw2/od0QCfVbsu
oIbkQxDJ3k3yMJKmjdqmYMs=
=8aqq
-----END PGP SIGNATURE-----

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs