module ActiveRecord::Relation::DeprecatedMethods - activerecord-deprecated_finders-1.0.3 Documentation

# module ActiveRecord::Relation::DeprecatedMethods

### Constants

VALID_FIND_OPTIONS

### Public Instance Methods

all(*args) click to toggle source

    # 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) click to toggle source

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

    # 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 = {}) click to toggle source
Calls superclass method

    # 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, :distinct => options[:distinct])
      else
        super
      end
    end

find(*args) click to toggle source
Calls superclass method

    # 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) click to toggle source
Calls superclass method

    # 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) click to toggle source
Calls superclass method

    # 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) click to toggle source
Calls superclass method

    # 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 = {}) click to toggle source

    # 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