Module: ActiveRecord::Relation::DeprecatedMethods

Included in:
ActiveRecord::Relation
Defined in:
lib/active_record/deprecated_finders/relation.rb

Constant Summary collapse

VALID_FIND_OPTIONS =
[ :conditions, :include, :joins, :limit, :offset,
:order, :select, :readonly, :group, :having, :from, :lock ]

Instance Method Summary collapse

Instance Method Details

#all(*args) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/active_record/deprecated_finders/relation.rb', line 161

def all(*args)
  ActiveSupport::Deprecation.warn(
    "Relation#all is deprecated. If you want to eager-load a relation, you can " \
    "call #load (e.g. `Post.where(published: true).load`). If you want " \
    "to get an array of records from a relation, you can call #to_a (e.g. " \
    "`Post.where(published: true).to_a`).", caller
  )
  apply_finder_options(args.first, true).to_a
end

#apply_finder_options(options, silence_deprecation = false) ⇒ Object

The silence_deprecation arg is for internal use, where we have already output a deprecation further up the call stack.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record/deprecated_finders/relation.rb', line 12

def apply_finder_options(options, silence_deprecation = false)
  ActiveSupport::Deprecation.warn("#apply_finder_options is deprecated") unless silence_deprecation

  relation = clone
  return relation unless options

  options.assert_valid_keys(VALID_FIND_OPTIONS)
  finders = options.dup
  finders.delete_if { |key, value| value.nil? && key != :limit }

  ((VALID_FIND_OPTIONS - [:conditions, :include]) & finders.keys).each do |finder|
    relation = relation.send(finder, finders[finder])
  end

  relation = relation.where(finders[:conditions]) if options.has_key?(:conditions)
  relation = relation.includes(finders[:include]) if options.has_key?(:include)

  relation
end

#calculate(operation, column_name, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/active_record/deprecated_finders/relation.rb', line 74

def calculate(operation, column_name, options = {})
  if options.except(:distinct).present?
    ActiveSupport::Deprecation.warn(
      "Relation#calculate with finder options is deprecated. Please build " \
      "a scope and then call calculate on it instead.", caller
    )

    apply_finder_options(options.except(:distinct), true)
      .calculate(operation, column_name, options.slice(:distinct))
  else
    super
  end
end

#find(*args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_record/deprecated_finders/relation.rb', line 88

def find(*args)
  options = args.extract_options!

  if options.present?
    scope = apply_finder_options(options, true)

    case finder = args.first
    when :first, :last, :all
      ActiveSupport::Deprecation.warn(
        "Calling #find(#{finder.inspect}) is deprecated. Please call " \
        "##{finder} directly instead. You have also used finder options. " \
        "These are also deprecated. Please build a scope instead of using " \
        "finder options.", caller
      )

      scope.send(finder)
    else
      ActiveSupport::Deprecation.warn(
        "Passing options to #find is deprecated. Please build a scope " \
        "and then call #find on it.", caller
      )

      scope.find(*args)
    end
  else
    case finder = args.first
    when :first, :last, :all
      ActiveSupport::Deprecation.warn(
        "Calling #find(#{finder.inspect}) is deprecated. Please call " \
        "##{finder} directly instead.", caller
      )

      send(finder)
    else
      super
    end
  end
end

#find_in_batches(options = {}, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_record/deprecated_finders/relation.rb', line 57

def find_in_batches(options = {}, &block)
  if (finder_options = options.except(:start, :batch_size)).present?
    ActiveSupport::Deprecation.warn(
      "Relation#find_in_batches with finder options is deprecated. Please build " \
      "a scope and then call find_in_batches on it instead.", caller
    )

    raise "You can't specify an order, it's forced to be #{batch_order}" if options[:order].present?
    raise "You can't specify a limit, it's forced to be the batch_size"  if options[:limit].present?

    apply_finder_options(finder_options, true).
      find_in_batches(options.slice(:start, :batch_size), &block)
  else
    super
  end
end

#first(*args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_record/deprecated_finders/relation.rb', line 127

def first(*args)
  if args.empty?
    super
  else
    if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash))
      super
    else
      ActiveSupport::Deprecation.warn(
        "Relation#first with finder options is deprecated. Please build " \
        "a scope and then call #first on it instead.", caller
      )

      apply_finder_options(args.first, true).first
    end
  end
end

#last(*args) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/active_record/deprecated_finders/relation.rb', line 144

def last(*args)
  if args.empty?
    super
  else
    if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash))
      super
    else
      ActiveSupport::Deprecation.warn(
        "Relation#last with finder options is deprecated. Please build " \
        "a scope and then call #last on it instead.", caller
      )

      apply_finder_options(args.first, true).last
    end
  end
end

#update_all_with_deprecated_options(updates, conditions = nil, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_record/deprecated_finders/relation.rb', line 32

def update_all_with_deprecated_options(updates, conditions = nil, options = {})
  scope = self

  if conditions
    scope = where(conditions)

    ActiveSupport::Deprecation.warn(
      "Relation#update_all with conditions is deprecated. Please use " \
      "Item.where(color: 'red').update_all(...) rather than " \
      "Item.update_all(..., color: 'red').", caller
    )
  end

  if options.present?
    scope = scope.apply_finder_options(options.slice(:limit, :order), true)

    ActiveSupport::Deprecation.warn(
      "Relation#update_all with :limit / :order options is deprecated. " \
      "Please use e.g. Post.limit(1).order(:foo).update_all instead.", caller
    )
  end

  scope.update_all_without_deprecated_options(updates)
end