README.rdoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. == HasScope
  2. Has scope allows you to easily create controller filters based on your resources named scopes.
  3. Imagine the following model called graduations:
  4. class Graduation < ActiveRecord::Base
  5. named_scope :featured, :conditions => { :featured => true }
  6. named_scope :by_degree, proc {|degree| { :conditions => { :degree => degree } } }
  7. end
  8. You can use those named scopes as filters by declaring them on your controller:
  9. class GraduationsController < ApplicationController
  10. has_scope :featured, :type => :boolean
  11. has_scope :by_degree
  12. end
  13. Now, if you want to apply them to an specific resource, you just need to call <tt>apply_scopes</tt>:
  14. class GraduationsController < ApplicationController
  15. has_scope :featured, :type => :boolean
  16. has_scope :by_degree
  17. has_scope :by_period, :using => [:started_at, :ended_at]
  18. def index
  19. @graduations = apply_scopes(Graduation).all
  20. end
  21. end
  22. Then for each request:
  23. /graduations
  24. #=> acts like a normal request
  25. /graduations?featured=true
  26. #=> calls the named scope and bring featured graduations
  27. /graduations?params[by_period][started_at]=20100701&params[by_period][ended_at]=20101013
  28. #=> brings graduations in the given period
  29. /graduations?featured=true&by_degree=phd
  30. #=> brings featured graduations with phd degree
  31. You can retrieve all the scopes applied in one action with <tt>current_scopes</tt> method.
  32. In the last case, it would return: { :featured => true, :by_degree => "phd" }.
  33. == Installation
  34. HasScope is available as gem on Gemcutter, so just run the following:
  35. sudo gem install has_scope
  36. If you want it as plugin, just do:
  37. script/plugin install git://github.com/plataformatec/has_scope.git
  38. == Options
  39. HasScope supports several options:
  40. * <tt>:type</tt> - 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.
  41. * <tt>:only</tt> - In which actions the scope is applied.
  42. * <tt>:except</tt> - In which actions the scope is not applied.
  43. * <tt>:as</tt> - The key in the params hash expected to find the scope. Defaults to the scope name.
  44. * <tt>:using</tt> - The subkeys to be used as args when type is a hash.
  45. * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the scope should apply.
  46. * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the scope should NOT apply.
  47. * <tt>:default</tt> - Default value for the scope. Whenever supplied the scope is always called.
  48. * <tt>:allow_blank</tt> - Blank values are not sent to scopes by default. Set to true to overwrite.
  49. == Block usage
  50. has_scope also accepts a block. The controller, current scope and value are yielded
  51. to the block so the user can apply the scope on its own. This is useful in case we
  52. need to manipulate the given value:
  53. has_scope :category do |controller, scope, value|
  54. value != "all" ? scope.by_category(value) : scope
  55. end
  56. When used with booleans, it just receives two arguments and is just invoked if true is given:
  57. has_scope :not_voted_by_me, :type => :boolean do |controller, scope|
  58. scope.not_voted_by(controller.current_user.id)
  59. end
  60. == Bugs and Feedback
  61. If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
  62. http://github.com/plataformatec/has_scope/issues
  63. MIT License. Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br