Class: ActiveRecord::Associations::Builder::Association

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

Constant Summary collapse

DEPRECATED_OPTIONS =
[:readonly, :order, :limit, :group, :having,
:offset, :select, :uniq, :include, :conditions]

Instance Method Summary collapse

Instance Method Details

#initialize_with_deprecated_options(model, name, scope, options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_record/deprecated_finders/association_builder.rb', line 40

def initialize_with_deprecated_options(model, name, scope, options)
  options            = scope if scope.is_a?(Hash)
  deprecated_options = options.slice(*DEPRECATED_OPTIONS)

  if scope.respond_to?(:call) && !deprecated_options.empty?
    raise ArgumentError,
      "Invalid mix of scope block and deprecated finder options on " \
      "ActiveRecord association: #{model.name}.#{macro} :#{name}"
  end

  if scope.is_a?(Hash)
    if deprecated_options.empty?
      scope = nil
    else
      ActiveSupport::Deprecation.warn(
        "The following options in your #{model.name}.#{macro} :#{name} declaration are deprecated: " \
        "#{deprecated_options.keys.map(&:inspect).join(',')}. Please use a scope block instead. " \
        "For example, the following:\n" \
        "\n" \
        "    has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'\n" \
        "\n" \
        "should be rewritten as the following:\n" \
        "\n" \
        "    has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'\n"
      )
      scope   = DeprecatedOptionsProc.new(deprecated_options)
      options = options.except(*DEPRECATED_OPTIONS)
    end
  end

  initialize_without_deprecated_options(model, name, scope, options)
end