<commit_before>require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/blank'

module ActiveRecord
  module QueryMethods
    extend ActiveSupport::Concern

    Relation::MULTI_VALUE_METHODS.each do |name|
      class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name}_values            # def select_values
          @values[:#{name}] || []     #   @values[:select] || []
        end                           # end
                                      #
        def #{name}_values=(values)   # def select_values=(values)
          @values[:#{name}] = values  #   @values[:select] = values
        end                           # end
      CODE
    end

    (Relation::SINGLE_VALUE_METHODS - [:create_with]).each do |name|
      class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name}_value             # def readonly_value
          @values[:#{name}]           #   @values[:readonly]
        end                           # end
                                      #
        def #{name}_value=(value)     # def readonly_value=(value)
          @values[:#{name}] = value   #   @values[:readonly] = value
        end                           # end
      CODE
    end

    def create_with_value
      @values[:create_with] || {}
    end

    def create_with_value=(value)
      @values[:create_with] = value
    end

    alias extensions extending_values

    def includes(*args)
      args.empty? ? self : spawn.includes!(*args)
    end

    def includes!(*args)
      args.reject! {|a| a.blank? }

      self.includes_values = (includes_values + args).flatten.uniq
      self
    end

    def eager_load(*args)
      args.blank? ? self : spawn.eager_load!(*args)
    end

    def eager_load!(*args)
      self.eager_load_values += args
      self
    end

    def preload(*args)
      args.blank? ? self : spawn.preload!(*args)
    end

    def preload!(*args)
      self.preload_values += args
      self
    end

    # Used to indicate that an association is referenced by an SQL string, and should
    # therefore be JOINed in any query rather than loaded separately.
    #
    # For example:
    #
    #   User.includes(:posts).where("posts.name = 'foo'")
    #   # => Doesn't JOIN the posts table, resulting in an error.
    #
    #   User.includes(:posts).where("posts.name = 'foo'").references(:posts)
    #   # => Query now knows the string references posts, so adds a JOIN
    def references(*args)
      args.blank? ? self : spawn.references!(*args)
    end

    def references!(*args)
      self.references_values = (references_values + args.flatten.map(&:to_s)).uniq
      self
    end

    # Works in two unique ways.
    #
    # First: takes a block so it can be used just like Array#select.
    #
    #   Model.scoped.select { |m| m.field == value }
    #
    # This will build an array of objects from the database for the scope,
    # converting them into an array and iterating through them using Array#select.
    #
    # Second: Modifies the SELECT statement for the query so that only certain
    # fields are retrieved:
    #
    #   >> Model.select(:field)
    #   => [#<Model field:value>]
    #
    # Although in the above example it looks as though this method returns an
    # array, it actually returns a relation object and can have other query
    # methods appended to it, such as the other methods in ActiveRecord::QueryMethods.
    #
    # The argument to the method can also be an array of fields.
    #
    #   >> Model.select([:field, :other_field, :and_one_more])
    #   => [#<Model field: "value", other_field: "value", and_one_more: "value">]
    #
    # Accessing attributes of an object that do not have fields retrieved by a select
    # will throw <tt>ActiveModel::MissingAttributeError</tt>:
    #
    #   >> Model.select(:field).first.other_field
    #   => ActiveModel::MissingAttributeError: missing attribute: other_field
    def select(value = Proc.new)
      if block_given?
        to_a.select { |*block_args| value.call(*block_args) }
      else
        spawn.select!(value)
      end
    end

    def select!(value)
      self.select_values += Array.wrap(value)
      self
    end

    def group(*args)
      args.blank? ? self : spawn.group!(*args)
    end

    def group!(*args)
      self.group_values += args.flatten
      self
    end

    def order(*args)
      args.blank? ? self : spawn.order!(*args)
    end

    def order!(*args)
      args       = args.flatten

      references = args.reject { |arg| Arel::Node === arg }
                       .map { |arg| arg =~ /^([a-zA-Z]\w*)\.(\w+)/ && $1 }
                       .compact
      references!(references) if references.any?

      self.order_values += args
      self
    end

    # Replaces any existing order defined on the relation with the specified order.
    #
    #   User.order('email DESC').reorder('id ASC') # generated SQL has 'ORDER BY id ASC'
    #
    # Subsequent calls to order on the same relation will be appended. For example:
    #
    #   User.order('email DESC').reorder('id ASC').order('name ASC')
    #
    # generates a query with 'ORDER BY id ASC, name ASC'.
    #
    def reorder(*args)
      args.blank? ? self : spawn.reorder!(*args)
    end

    def reorder!(*args)
      self.reordering_value = true
      self.order_values = args.flatten
      self
    end

    def joins(*args)
      args.compact.blank? ? self : spawn.joins!(*args)
    end

    def joins!(*args)
      args.flatten!

      self.joins_values += args
      self
    end

    def bind(value)
      spawn.bind!(value)
    end

    def bind!(value)
      self.bind_values += [value]
      self
    end

    def where(opts, *rest)
      opts.blank? ? self : spawn.where!(opts, *rest)
    end

    def where!(opts, *rest)
      references!(PredicateBuilder.references(opts)) if Hash === opts

      self.where_values += build_where(opts, rest)
      self
    end

    def having(opts, *rest)
      opts.blank? ? self : spawn.having!(opts, *rest)
    end

    def having!(opts, *rest)
      references!(PredicateBuilder.references(opts)) if Hash === opts

      self.having_values += build_where(opts, rest)
      self
    end

    def limit(value)
      spawn.limit!(value)
    end

    def limit!(value)
      self.limit_value = value
      self
    end

    def offset(value)
      spawn.offset!(value)
    end

    def offset!(value)
      self.offset_value = value
      self
    end

    def lock(locks = true)
      spawn.lock!(locks)
    end

    def lock!(locks = true)
      case locks
      when String, TrueClass, NilClass
        self.lock_value = locks || true<commit_msg>Add documentation for ActiveRecord::QueryMethods#limit [ci skip]<commit_after>require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/blank'

module ActiveRecord
  module QueryMethods
    extend ActiveSupport::Concern

    Relation::MULTI_VALUE_METHODS.each do |name|
      class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name}_values            # def select_values
          @values[:#{name}] || []     #   @values[:select] || []
        end                           # end
                                      #
        def #{name}_values=(values)   # def select_values=(values)
          @values[:#{name}] = values  #   @values[:select] = values
        end                           # end
      CODE
    end

    (Relation::SINGLE_VALUE_METHODS - [:create_with]).each do |name|
      class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name}_value             # def readonly_value
          @values[:#{name}]           #   @values[:readonly]
        end                           # end
                                      #
        def #{name}_value=(value)     # def readonly_value=(value)
          @values[:#{name}] = value   #   @values[:readonly] = value
        end                           # end
      CODE
    end

    def create_with_value
      @values[:create_with] || {}
    end

    def create_with_value=(value)
      @values[:create_with] = value
    end

    alias extensions extending_values

    def includes(*args)
      args.empty? ? self : spawn.includes!(*args)
    end

    def includes!(*args)
      args.reject! {|a| a.blank? }

      self.includes_values = (includes_values + args).flatten.uniq
      self
    end

    def eager_load(*args)
      args.blank? ? self : spawn.eager_load!(*args)
    end

    def eager_load!(*args)
      self.eager_load_values += args
      self
    end

    def preload(*args)
      args.blank? ? self : spawn.preload!(*args)
    end

    def preload!(*args)
      self.preload_values += args
      self
    end

    # Used to indicate that an association is referenced by an SQL string, and should
    # therefore be JOINed in any query rather than loaded separately.
    #
    #   User.includes(:posts).where("posts.name = 'foo'")
    #   # => Doesn't JOIN the posts table, resulting in an error.
    #
    #   User.includes(:posts).where("posts.name = 'foo'").references(:posts)
    #   # => Query now knows the string references posts, so adds a JOIN
    def references(*args)
      args.blank? ? self : spawn.references!(*args)
    end

    def references!(*args)
      self.references_values = (references_values + args.flatten.map(&:to_s)).uniq
      self
    end

    # Works in two unique ways.
    #
    # First: takes a block so it can be used just like Array#select.
    #
    #   Model.scoped.select { |m| m.field == value }
    #
    # This will build an array of objects from the database for the scope,
    # converting them into an array and iterating through them using Array#select.
    #
    # Second: Modifies the SELECT statement for the query so that only certain
    # fields are retrieved:
    #
    #   >> Model.select(:field)
    #   => [#<Model field:value>]
    #
    # Although in the above example it looks as though this method returns an
    # array, it actually returns a relation object and can have other query
    # methods appended to it, such as the other methods in ActiveRecord::QueryMethods.
    #
    # The argument to the method can also be an array of fields.
    #
    #   >> Model.select([:field, :other_field, :and_one_more])
    #   => [#<Model field: "value", other_field: "value", and_one_more: "value">]
    #
    # Accessing attributes of an object that do not have fields retrieved by a select
    # will throw <tt>ActiveModel::MissingAttributeError</tt>:
    #
    #   >> Model.select(:field).first.other_field
    #   => ActiveModel::MissingAttributeError: missing attribute: other_field
    def select(value = Proc.new)
      if block_given?
        to_a.select { |*block_args| value.call(*block_args) }
      else
        spawn.select!(value)
      end
    end

    def select!(value)
      self.select_values += Array.wrap(value)
      self
    end

    def group(*args)
      args.blank? ? self : spawn.group!(*args)
    end

    def group!(*args)
      self.group_values += args.flatten
      self
    end

    def order(*args)
      args.blank? ? self : spawn.order!(*args)
    end

    def order!(*args)
      args       = args.flatten

      references = args.reject { |arg| Arel::Node === arg }
                       .map { |arg| arg =~ /^([a-zA-Z]\w*)\.(\w+)/ && $1 }
                       .compact
      references!(references) if references.any?

      self.order_values += args
      self
    end

    # Replaces any existing order defined on the relation with the specified order.
    #
    #   User.order('email DESC').reorder('id ASC') # generated SQL has 'ORDER BY id ASC'
    #
    # Subsequent calls to order on the same relation will be appended. For example:
    #
    #   User.order('email DESC').reorder('id ASC').order('name ASC')
    #
    # generates a query with 'ORDER BY id ASC, name ASC'.
    def reorder(*args)
      args.blank? ? self : spawn.reorder!(*args)
    end

    def reorder!(*args)
      self.reordering_value = true
      self.order_values = args.flatten
      self
    end

    def joins(*args)
      args.compact.blank? ? self : spawn.joins!(*args)
    end

    def joins!(*args)
      args.flatten!

      self.joins_values += args
      self
    end

    def bind(value)
      spawn.bind!(value)
    end

    def bind!(value)
      self.bind_values += [value]
      self
    end

    def where(opts, *rest)
      opts.blank? ? self : spawn.where!(opts, *rest)
    end

    def where!(opts, *rest)
      references!(PredicateBuilder.references(opts)) if Hash === opts

      self.where_values += build_where(opts, rest)
      self
    end

    def having(opts, *rest)
      opts.blank? ? self : spawn.having!(opts, *rest)
    end

    def having!(opts, *rest)
      references!(PredicateBuilder.references(opts)) if Hash === opts

      self.having_values += build_where(opts, rest)
      self
    end

    # Specifies a limit of records.
    #
    #   User.limit(10) # generated SQL has 'LIMIT 10'
    #
    # Replaces any existing previous limit.
    #
    #   User.limit(10).limit(20) # generated SQL has 'LIMIT 20'
    def limit(value)
      spawn.limit!(value)
    end

    def limit!(value)
      self.limit_value = value
      self
    end

    def offset(value)
      spawn.offset!(value)
    end

    def offset!(value)
      self.offset_value = value
      self
    end

    def lock(locks = true)
      spawn.lock!(locks)
    end

    def lock!(locks = true)
      case locks
      when String, TrueClass, NilClass
        self.lock_value = locks || true
{'fraction_non_alphanumeric': 0.11190828402366863, 'fraction_numerical': 0.001183431952662722, 'mean_word_length': 2.1334878331402085, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6716863', 'n_tokens_mistral': 4156, 'n_tokens_neox': 3997, 'n_words': 1116}