module HasScope::ClassMethods
Public Instance Methods
Detects params from url and apply as scopes to your classes.
Options¶ ↑
-
:type
- Checks the type of the parameter sent. If set to :booleanit 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. By default is :all. -
:except
- In which actions the scope is not applied. By default is :none. -
:as
- The key in the params hash expected to find the scope.Defaults to the scope name.
-
:using
- If type is a hash, you can provide :using to convert the hash toa named scope call with several arguments.
-
:in
- A shortcut for combining the `:using` option with nested hashes. -
:if
- Specifies a method, proc or string to call to determineif the scope should apply
-
:unless
- Specifies a method, proc or string to call to determineif the scope should NOT apply.
-
:default
- Default value for the scope. Whenever supplied the scopeis always called.
-
:allow_blank
- Blank values are not sent to scopes by default. Set to true to overwrite.
Block usage¶ ↑
has_scope
also accepts a block. The controller, current scope and value are yielded to the block so the user can apply the scope on its own. This is useful in case we need to manipulate the given value:
has_scope :category do |controller, scope, value| value != "all" ? scope.by_category(value) : scope end has_scope :not_voted_by_me, type: :boolean do |controller, scope| scope.not_voted_by(controller.current_user.id) end
# File lib/has_scope.rb, line 69 def has_scope(*scopes, &block) options = scopes.extract_options! options.symbolize_keys! options.assert_valid_keys(:type, :only, :except, :if, :unless, :default, :as, :using, :allow_blank, :in) if options.key?(:in) options[:as] = options[:in] options[:using] = scopes if options.key?(:default) && !options[:default].is_a?(Hash) options[:default] = scopes.each_with_object({}) { |scope, hash| hash[scope] = options[:default] } end end if options.key?(:using) if options.key?(:type) && options[:type] != :hash raise "You cannot use :using with another :type different than :hash" else options[:type] = :hash end options[:using] = Array(options[:using]) end options[:only] = Array(options[:only]) options[:except] = Array(options[:except]) self.scopes_configuration = scopes_configuration.dup scopes.each do |scope| scopes_configuration[scope] ||= { as: scope, type: :default, block: block } scopes_configuration[scope] = self.scopes_configuration[scope].merge(options) end end