Class: ActiveRecord::DeprecatedFinders::ScopeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/deprecated_finders/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, scope) ⇒ ScopeWrapper

Returns a new instance of ScopeWrapper.



21
22
23
24
# File 'lib/active_record/deprecated_finders/base.rb', line 21

def initialize(klass, scope)
  @klass = klass
  @scope = scope
end

Class Method Details

.wrap(klass, scope) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Method Details

#call(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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