class ActiveRecord::DeprecatedFinders::ScopeWrapper - activerecord-deprecated_finders-1.0.4 Documentation

# class ActiveRecord::DeprecatedFinders::ScopeWrapper

### Public Class Methods

new(klass, scope) click to toggle source

    # File lib/active_record/deprecated_finders/base.rb, line 21
    def initialize(klass, scope)
      @klass = klass
      @scope = scope
    end

wrap(klass, scope) click to toggle source

    # File lib/active_record/deprecated_finders/base.rb, line 6
    def self.wrap(klass, scope)
      if scope.is_a?(Hash)
        ActiveSupport::Deprecation.warn(
          "Calling #scope or #default_scope with a hash is deprecated. Please use a lambda "              "containing a scope. E.g. scope :red, -> { where(color: 'red') }"
        )

        new(klass, scope)
      elsif !scope.is_a?(Relation) && scope.respond_to?(:call)
        new(klass, scope)
      else
        scope
      end
    end

### Public Instance Methods

call(*args) click to toggle source

    # File lib/active_record/deprecated_finders/base.rb, line 26
    def call(*args)
      if @scope.respond_to?(:call)
        result = @scope.call(*args)

        if result.is_a?(Hash)
          msg = "Returning a hash from a #scope or #default_scope block is deprecated. Please "                "return an actual scope object instead. E.g. scope :red, -> { where(color: 'red') } "                "rather than scope :red, -> { { conditions: { color: 'red' } } }. "

          if @scope.respond_to?(:source_location)
            msg << "(The scope was defined at #{@scope.source_location.join(':')}.)"
          end

          ActiveSupport::Deprecation.warn(msg)
        end
      else
        result = @scope
      end

      if result.is_a?(Hash)
        @klass.all.apply_finder_options(result, true)
      else
        result
      end
    end