Sha256: 7acdb71df5aa1d3326947438c1921acdd16ba6b24e27e0ce10d0d4d829319f2f

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

== 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, :type => :boolean
    has_scope :by_degree
  end

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

  class GraduationsController < ApplicationController
    has_scope :featured, :type => :boolean
    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 <tt>current_scopes</tt> method.
In the last case, it would return: { :featured => true, :by_degree => "phd" }.

<tt>has_scope</tt> support several options (:only, :except, :as, :if, :unless, :default
and :type), please check the documentation for a detailed description.

== Bugs and Feedback

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

http://github.com/plataformatec/has_scope/issues

MIT License. Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
has_scope-0.2 README.rdoc