Skip to content

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages