Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Map incoming controller parameters to named scopes in your resources
Ruby Logos
Tree: ccf5b4f157

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
CHANGELOG.rdoc
MIT-LICENSE
README.rdoc
Rakefile

README.rdoc

HasScope

Has scope allows you to easily create controller filters based on your resources named scopes. Imagine the following model called graduations:

class Graduation < ActiveRecord::Base
  named_scope :featured, :conditions => { :featured => true }
  named_scope :by_degree, proc {|degree| { :conditions => { :degree => degree } } }
end

You can use those named scopes as filters by declaring them on your controller:

class GraduationsController < ApplicationController
  has_scope :featured, :boolean => true
  has_scope :by_degree
end

Now, if you want to apply them to an specific resource, you just need to call apply_scopes:

class GraduationsController < ApplicationController
	has_scope :featured, :boolean => true
	has_scope :by_degree

def index @graduations = apply_scopes(Graduations).all end

end

Then for each request:

/graduations
#=> acts like a normal request

/graduations?featured=true
#=> calls the named scope and bring featured graduations

/graduations?featured=true&by_degree=phd
#=> brings featured graduations with phd degree

You can retrieve all the scopes applied in one action with current_scopes method. In the last case, it would return: { :featured => true, :by_degree => “phd” }.

Please check has_scope method for all the supported options.

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

Something went wrong with that request. Please try again.