Permalink
Browse files

Updated README and docs.

  • Loading branch information...
josevalim committed Dec 4, 2009
1 parent f6fb5fd commit ccf5b4f1577f10636be604f18a518d675f822969
Showing with 72 additions and 29 deletions.
  1. +20 −0 MIT-LICENSE
  2. +51 −0 README.rdoc
  3. +1 −29 lib/has_scope.rb
View
@@ -0,0 +1,20 @@
+Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,51 @@
+== 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 <tt>apply_scopes</tt>:
+
+ 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 <tt>current_scopes</tt> method.
+In the last case, it would return: { :featured => true, :by_degree => "phd" }.
+
+Please check <tt>has_scope</tt> 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.
+
+http://github.com/plataformatec/has_scope/issues
+
+MIT License. Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
View
@@ -1,6 +1,6 @@
module HasScope
TRUE_VALUES = ["true", true, "1", 1]
-
+
def self.included(base)
base.class_eval do
extend ClassMethods
@@ -14,34 +14,6 @@ def self.included(base)
module ClassMethods
# Detects params from url and apply as scopes to your classes.
#
- # Your model:
- #
- # class Graduation < ActiveRecord::Base
- # named_scope :featured, :conditions => { :featured => true }
- # named_scope :by_degree, proc {|degree| { :conditions => { :degree => degree } } }
- # end
- #
- # Your controller:
- #
- # class GraduationsController < InheritedResources::Base
- # has_scope :featured, :boolean => true, :only => :index
- # has_scope :by_degree, :only => :index
- # 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 the current scopes in use with <tt>current_scopes</tt>
- # method. In the last case, it would return: { :featured => "true", :by_degree => "phd" }
- #
# == Options
#
# * <tt>:boolean</tt> - When set to true, call the scope only when the param is true or 1,

0 comments on commit ccf5b4f

Please sign in to comment.