module HasScope

Constants

ALLOWED_TYPES
TRUE_VALUES
VERSION

Public Class Methods

deprecator() click to toggle source
# File lib/has_scope.rb, line 14
def self.deprecator
  @deprecator ||= ActiveSupport::Deprecation.new("1.0", "HasScope")
end
included(base) click to toggle source
# File lib/has_scope.rb, line 18
def self.included(base)
  base.class_eval do
    extend ClassMethods
    class_attribute :scopes_configuration, instance_writer: false
    self.scopes_configuration = {}
  end
end

Protected Instance Methods

apply_scopes(target, hash = params) click to toggle source

Receives an object where scopes will be applied to.

class GraduationsController < ApplicationController
  has_scope :featured, type: true, only: :index
  has_scope :by_degree, only: :index

  def index
    @graduations = apply_scopes(Graduation).all
  end
end
# File lib/has_scope.rb, line 122
def apply_scopes(target, hash = params)
  scopes_configuration.each do |scope, options|
    next unless apply_scope_to_action?(options)
    key = options[:as]

    if hash.key?(key)
      value, call_scope = hash[key], true
    elsif options.key?(:default)
      value, call_scope = options[:default], true
      if value.is_a?(Proc)
        value = value.arity == 0 ? value.call : value.call(self)
      end
    end

    value = parse_value(options[:type], value)
    value = normalize_blanks(value)

    if value && options.key?(:using)
      scope_value = value.values_at(*options[:using])
      call_scope &&= scope_value.all?(&:present?) || options[:allow_blank]
    else
      scope_value = value
      call_scope &&= value.present? || options[:allow_blank]
    end

    if call_scope
      current_scopes[key] = value
      target = call_scope_by_type(options[:type], scope, target, scope_value, options)
    end
  end

  target
end
current_scopes() click to toggle source

Returns the scopes used in this action.

# File lib/has_scope.rb, line 224
def current_scopes
  @current_scopes ||= {}
end