has_scope

has_scope 源代码定义

   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)

      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 = (self.scopes_configuration || {}).dup

      scopes.each do |scope|
        self.scopes_configuration[scope] ||= { :as => scope, :type => :default, :block => block }
        self.scopes_configuration[scope] = self.scopes_configuration[scope].merge(options)
      end
    end

Options

HasScope 使用时支持以下 9 个 options:

:type - 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.

:only - In which actions the scope is applied.

:except - In which actions the scope is not applied.

:as - The key in the params hash expected to find the scope. Defaults to the scope name.

:using - The subkeys to be used as args when type is a hash.

:if - Specifies a method, proc or string to call to determine if the scope should apply.

:unless - Specifies a method, proc or string to call to determine if the scope should NOT apply.

:default - Default value for the scope. Whenever supplied the scope is always called.

:allow_blank - Blank values are not sent to scopes by default. Set to true to overwrite.

你可能感兴趣的:(has_scope)