HasScope

Block usage

This fork works only for sunspot_rails and i have only used the block variants

class GraduationsController < ApplicationController

  has_scope :query do |controller, search, value|
    search.keywords(value)
  end

  has_scope :category do |controller, search, value|
    search.with(:category_id, value)
  end

  def index
    @search = Graduation.search do |search|
      apply_scopes(search)
      search.facet(:category_id, :exclude => scope_return_values[:category] #multi-facet search
    end
    @graduations = @search.results
  end
end

You can retrieve all the scopes applied in one action with current_scopes method.

Installation

HasScope is available as gem on Gemcutter, so just run the following:

sudo gem install has_scope

If you want it as plugin, just do:

script/plugin install git://github.com/plataformatec/has_scope.git

Options

HasScope supports several options:

  • :type - Checks the type of the parameter sent. If set to :boolean it just calls the named scope, without any argument. By default, it does not allow hashes or arrays to be given, except if type :hash or :array are set.

  • :only - In which actions the scope is applied.

  • :except - In which actions the scope is not applied.

  • :as - The key in the params hash expected to find the scope. Defaults to the scope name.

  • :using - The subkeys to be used as args when type is a hash.

  • :if - Specifies a method, proc or string to call to determine if the scope should apply.

  • :unless - Specifies a method, proc or string to call to determine if the scope should NOT apply.

  • :default - Default value for the scope. Whenever supplied the scope is always called.

  • :allow_blank - Blank values are not sent to scopes by default. Set to true to overwrite.

Bugs and Feedback

If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.

github.com/plataformatec/has_scope/issues

MIT License. Copyright 2009 Plataforma Tecnologia. blog.plataformatec.com.br