Skip to content

Rails AR plugin that generates sexy named scopes for models

Notifications You must be signed in to change notification settings

cthulhu/by_whatever

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rails AR plugin that generates sexy named scopes for models

I often need to add such named scopes as by_user_id or by_account_id and they always looks the same

named_scope :by_account_id, lambda { |account_id|
  (account_id.blank?) ? {} : { :conditions => ['account_id = ?', account_id.to_i] }
}

AND

named_scope :by_account_ids, lambda { |account_ids|
  (account_id.blank?) ? {} : { :conditions => ['account_id IN ( ? )', account_ids] }
}

So, By Whatever simply adds scopes for all foreight keys from model, and you don’t have to write them all the time.

Simplest way to get all needed scopes

class User < ActiveRecord::Base
  ...
  by_whatever 
  ...
end

Implicit inclusion and exclusion:

class User < ActiveRecord::Base
  ...
  by_whatever :only => [:user_id, :account_id]
  ...
end

class User < ActiveRecord::Base
  ...
  by_whatever :except => [:user_id, :account_id]
  ...
end

ruby ./script/plugin install git://github.com/cthulhu/by_whatever.git

Copyright © 2009 Stanislav O. Pogrebnyak, released under the MIT license.

About

Rails AR plugin that generates sexy named scopes for models

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages