Build common named scopes automatically, and provide a simple way to merge them with a single #search call.
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 7 commits ahead, 4 commits behind technoweenie:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Allows you create common named_scopes and chain them together with #search and 

  class Topic
    belongs_to :forum
    has_many :commented
    has_many :comments, :through => :commented
    can_search do
      scoped_by :forums
      scoped_by :created, :scope => :date_range
      scoped_by :comments, :scope => :many
      scoped_by :name, :scope => :many_like, :on => comments

    # creates these three named scopes
    #   named_scope :by_forums, lambda { |f| {:conditions => {:forum_id => f}} }
    #   named_scope :created,   lambda { |range| {:conditions => ...} }
    #   named_scope :many_comments, lambda { |q| 
    #     {:joins => :commented,:conditions => {"commented.comments_key" => q},
    #      :select => "DISTINCT topics.*"}
    #   named_scope :many_comments_name_like, lambda { |q| 
    #     {:joins => :comments,:conditions => [" LIKE ?", format % q],
    #      :select => "DISTINCT topics.*"}
  end => 1)                  # Topic.by_forums(1) => [1,2])             # Topic.by_forums([1,2]) => (time1..time2))   # Topic.created(time1..time2) => \
    {:period => :daily, :start =>}) # Topic.created(, + => "something")         # Topic.many_comments_name_like("something")

You can automatically paginate: :forum => 1, :page => params[:page]

You can also access the named_scope directly for custom #find or #calculate calls.

  Topic.search_for(:forum => 1).sum(:hits)

Oh, and you can combine scopes: :forum => 1, :forums => [2, 3], :created => (time1..time2)

The options for every scope are: 

:named_scope => "name"
   "name" will be the name of the created scope
:attribute => "attr"
   "attr" will be the attribute the scope is applied on

many_... scopes take
:on => "association"
   "association" is the ActiveRecord class that is joined

many_scope takes
:foreign_key => "foreign_key"
  foreign_key will only be used if you are searching for the id on a has_many :through
  assocation. You only need it if you have a non standard foreign_key in the :through

like... scopes take 
:format => "%s"
   "%s" will be the format like is aplied on

Be careful when using multiple ":scope => :many" in one search. Right now the
:joins overwrite each other. The one with the alpabeticly lower name will be
used. You will have to wait for Rails 2.2 or run rails edge to get a fix for this.
See ticket: 

Copyright (c) 2008-* Rick Olson, released under the MIT license