<commit_before>if CANVAS_RAILS2

ActiveRecord::Base.class_eval do
  class << self
    # taken from fake_arel, and extended further to support combining of :group
    def with_scope(method_scoping = {}, action = :merge, &block)
      method_scoping = {:find => method_scoping.proxy_options} if method_scoping.class == ActiveRecord::NamedScope::Scope
      method_scoping = method_scoping.method_scoping if method_scoping.respond_to?(:method_scoping)

      # Dup first and second level of hash (method and params).
      method_scoping = method_scoping.inject({}) do |hash, (method, params)|
        hash[method] = (params == true) ? params : params.dup
        hash
      end

      method_scoping.assert_valid_keys([ :find, :create ])

      if f = method_scoping[:find]
        f.assert_valid_keys(VALID_FIND_OPTIONS)
        set_readonly_option! f
      end

      # Merge scopings
      if [:merge, :reverse_merge].include?(action) && current_scoped_methods
        method_scoping = current_scoped_methods.inject(method_scoping) do |hash, (method, params)|
          case hash[method]
            when Hash
              if method == :find
                (hash[method].keys + params.keys).uniq.each do |key|
                  merge = hash[method][key] && params[key] # merge if both scopes have the same key
                  if key == :conditions && merge
                    if params[key].is_a?(Hash) && hash[method][key].is_a?(Hash)
                      hash[method][key] = merge_conditions(hash[method][key].deep_merge(params[key]))
                    else
                      hash[method][key] = merge_conditions(params[key], hash[method][key])
                    end
                  elsif key == :select && merge
                    hash[method][key] = merge_includes(hash[method][key], params[key]).uniq.join(', ')
                  elsif key == :readonly
                    hash[method][key] = params[key] unless params[:readonly].nil?
                  elsif key == :include && merge
                    hash[method][key] = merge_includes(hash[method][key], params[key]).uniq
                  elsif key == :joins && merge
                    hash[method][key] = merge_joins(params[key], hash[method][key])
                    # see https://rails.lighthouseapp.com/projects/8994/tickets/2810-with_scope-should-accept-and-use-order-option
                    # it works now in reverse order to comply with ActiveRecord 3
                  elsif [:group, :order].include?(key) && merge && !default_scoping.any?{ |s| s[method].keys.include?(key) }
                    hash[method][key] = [hash[method][key], params[key]].select{|o| !o.blank?}.join(', ')
                  else
                    hash[method][key] = hash[method][key] || params[key]
                  end
                end
              else
                if action == :reverse_merge
                  hash[method] = hash[method].merge(params)
                else
                  hash[method] = params.merge(hash[method])
                end
              end
            else
              hash[method] = params
          end
          hash
        end
      end

      self.scoped_methods << method_scoping
      begin
        yield
      ensure
        self.scoped_methods.pop
      end
    end

    # returns a new scope, having removed the options mentioned
    # does *not* support extended scopes
    def except(*options)
      # include is renamed to includes in Rails 3
      includes = options.delete(:includes)
      options << :include if includes

      new_options = (scope(:find) || {}).reject { |k, v| options.include?(k) }
      with_exclusive_scope(:find => new_options) { scoped }
    end

    # returns a new scope, with just the order replaced
    # does *not* support extended scopes
    def reorder(*order)
      new_options = (scope(:find) || {}).dup
      new_options[:order] = order.flatten.join(',')
      with_exclusive_scope(:find =>new_options) { scoped }
    end

    def uniq(value = true)
      current_select = scope(:find, :select)
      if current_select.blank?
        return scoped unless value
        return select("DISTINCT #{quoted_table_name}.*")
      end

      match = current_select =~ /^\s*DISTINCT\s+(.+)/i
      if match && !value
        new_options = scope(:find).dup
        new_options[:select] = $1
        with_exclusive_scope(:find => new_options) { scoped }
      elsif !match && value
        new_options = scope(:find).dup
        new_options[:select] = "DISTINCT #{current_select}"
        with_exclusive_scope(:find => new_options) { scoped }
      else
        scoped
      end
    end

    def pluck(column)
      new_options = (scope(:find) || {}).dup
      new_options[:select] = "#{quoted_table_name}.#{column}"
      new_options.delete(:include)
      with_exclusive_scope(:find => new_options) { all.map(&column) }
    end

    # allow validate to recognize the Rails 3 style on: action modifiers,
    # translating those calls into validate_on_action calls.
    def validate_with_rails3_compatibility(*methods, &block)
      options = methods.extract_options! || {}
      callback =
        case validation_method(options[:on])
        when :validate_on_create then :validate_on_create
        when :validate_on_update then :validate_on_update
        else :validate_without_rails3_compatibility
        end
      methods << block if block_given?
      methods << options unless options.empty?
      send(callback, *methods)
    end
    alias_method_chain :validate, :rails3_compatibility

    # allow before_validation to recognize the Rails 3 style on: action
    # modifiers, translating those calls into before_validation_on_action
    # calls.
    def before_validation_with_rails3_compatibility(*methods, &block)
      options = methods.extract_options! || {}
      callback =
        case validation_method(options[:on])
        when :validate_on_create then :before_validation_on_create
        when :validate_on_update then :before_validation_on_update
        else :before_validation_without_rails3_compatibility
        end
      methods << block if block_given?
      methods << options unless options.empty?
      send(callback, *methods)
    end
    alias_method_chain :before_validation, :rails3_compatibility

    def quote_bound_value_with_relations(value)
      if ActiveRecord::Associations::AssociationCollection === value
        with_exclusive_scope do
          value = value.scoped
        end
      end
      if ActiveRecord::NamedScope::Scope === value
        with_exclusive_scope do
          unless value.scope(:find, :select)
            value = value.select("#{value.quoted_table_name}.#{value.primary_key}")
          end
          return value.to_sql
        end
      end
      quote_bound_value_without_relations(value)
    end
    alias_method_chain :quote_bound_value, :relations
  end
end

ActiveRecord::NamedScope::ClassMethods.module_eval do
  # make all arguments optional, like Rails 3
  def scoped(scope = {}, &block)
    ActiveRecord::NamedScope::Scope.new(self, scope, &block)
  end
end

ActiveRecord::NamedScope::Scope.class_eval do
  # Scope delegates this to proxy_found because it's an array method; remove
  # the delegation and let it go through normal method_missing to the model
  remove_method :uniq

  # fake_arel doesn't quite work right here - somehow it's getting
  # Kernel#select. so just duplicate the select we want
  def select(value = Proc.new)
    if block_given?
      all.select {|*block_args| value.call(*block_args) }<commit_msg>allow rails 3 style "don't validate" options to ActiveRecord::Base#save<commit_after>if CANVAS_RAILS2

ActiveRecord::Base.class_eval do
  class << self
    # taken from fake_arel, and extended further to support combining of :group
    def with_scope(method_scoping = {}, action = :merge, &block)
      method_scoping = {:find => method_scoping.proxy_options} if method_scoping.class == ActiveRecord::NamedScope::Scope
      method_scoping = method_scoping.method_scoping if method_scoping.respond_to?(:method_scoping)

      # Dup first and second level of hash (method and params).
      method_scoping = method_scoping.inject({}) do |hash, (method, params)|
        hash[method] = (params == true) ? params : params.dup
        hash
      end

      method_scoping.assert_valid_keys([ :find, :create ])

      if f = method_scoping[:find]
        f.assert_valid_keys(VALID_FIND_OPTIONS)
        set_readonly_option! f
      end

      # Merge scopings
      if [:merge, :reverse_merge].include?(action) && current_scoped_methods
        method_scoping = current_scoped_methods.inject(method_scoping) do |hash, (method, params)|
          case hash[method]
            when Hash
              if method == :find
                (hash[method].keys + params.keys).uniq.each do |key|
                  merge = hash[method][key] && params[key] # merge if both scopes have the same key
                  if key == :conditions && merge
                    if params[key].is_a?(Hash) && hash[method][key].is_a?(Hash)
                      hash[method][key] = merge_conditions(hash[method][key].deep_merge(params[key]))
                    else
                      hash[method][key] = merge_conditions(params[key], hash[method][key])
                    end
                  elsif key == :select && merge
                    hash[method][key] = merge_includes(hash[method][key], params[key]).uniq.join(', ')
                  elsif key == :readonly
                    hash[method][key] = params[key] unless params[:readonly].nil?
                  elsif key == :include && merge
                    hash[method][key] = merge_includes(hash[method][key], params[key]).uniq
                  elsif key == :joins && merge
                    hash[method][key] = merge_joins(params[key], hash[method][key])
                    # see https://rails.lighthouseapp.com/projects/8994/tickets/2810-with_scope-should-accept-and-use-order-option
                    # it works now in reverse order to comply with ActiveRecord 3
                  elsif [:group, :order].include?(key) && merge && !default_scoping.any?{ |s| s[method].keys.include?(key) }
                    hash[method][key] = [hash[method][key], params[key]].select{|o| !o.blank?}.join(', ')
                  else
                    hash[method][key] = hash[method][key] || params[key]
                  end
                end
              else
                if action == :reverse_merge
                  hash[method] = hash[method].merge(params)
                else
                  hash[method] = params.merge(hash[method])
                end
              end
            else
              hash[method] = params
          end
          hash
        end
      end

      self.scoped_methods << method_scoping
      begin
        yield
      ensure
        self.scoped_methods.pop
      end
    end

    # returns a new scope, having removed the options mentioned
    # does *not* support extended scopes
    def except(*options)
      # include is renamed to includes in Rails 3
      includes = options.delete(:includes)
      options << :include if includes

      new_options = (scope(:find) || {}).reject { |k, v| options.include?(k) }
      with_exclusive_scope(:find => new_options) { scoped }
    end

    # returns a new scope, with just the order replaced
    # does *not* support extended scopes
    def reorder(*order)
      new_options = (scope(:find) || {}).dup
      new_options[:order] = order.flatten.join(',')
      with_exclusive_scope(:find =>new_options) { scoped }
    end

    def uniq(value = true)
      current_select = scope(:find, :select)
      if current_select.blank?
        return scoped unless value
        return select("DISTINCT #{quoted_table_name}.*")
      end

      match = current_select =~ /^\s*DISTINCT\s+(.+)/i
      if match && !value
        new_options = scope(:find).dup
        new_options[:select] = $1
        with_exclusive_scope(:find => new_options) { scoped }
      elsif !match && value
        new_options = scope(:find).dup
        new_options[:select] = "DISTINCT #{current_select}"
        with_exclusive_scope(:find => new_options) { scoped }
      else
        scoped
      end
    end

    def pluck(column)
      new_options = (scope(:find) || {}).dup
      new_options[:select] = "#{quoted_table_name}.#{column}"
      new_options.delete(:include)
      with_exclusive_scope(:find => new_options) { all.map(&column) }
    end

    # allow validate to recognize the Rails 3 style on: action modifiers,
    # translating those calls into validate_on_action calls.
    def validate_with_rails3_compatibility(*methods, &block)
      options = methods.extract_options! || {}
      callback =
        case validation_method(options[:on])
        when :validate_on_create then :validate_on_create
        when :validate_on_update then :validate_on_update
        else :validate_without_rails3_compatibility
        end
      methods << block if block_given?
      methods << options unless options.empty?
      send(callback, *methods)
    end
    alias_method_chain :validate, :rails3_compatibility

    # allow before_validation to recognize the Rails 3 style on: action
    # modifiers, translating those calls into before_validation_on_action
    # calls.
    def before_validation_with_rails3_compatibility(*methods, &block)
      options = methods.extract_options! || {}
      callback =
        case validation_method(options[:on])
        when :validate_on_create then :before_validation_on_create
        when :validate_on_update then :before_validation_on_update
        else :before_validation_without_rails3_compatibility
        end
      methods << block if block_given?
      methods << options unless options.empty?
      send(callback, *methods)
    end
    alias_method_chain :before_validation, :rails3_compatibility

    def quote_bound_value_with_relations(value)
      if ActiveRecord::Associations::AssociationCollection === value
        with_exclusive_scope do
          value = value.scoped
        end
      end
      if ActiveRecord::NamedScope::Scope === value
        with_exclusive_scope do
          unless value.scope(:find, :select)
            value = value.select("#{value.quoted_table_name}.#{value.primary_key}")
          end
          return value.to_sql
        end
      end
      quote_bound_value_without_relations(value)
    end
    alias_method_chain :quote_bound_value, :relations
  end

  def save_with_rails3_options(options = true)
    if options == { validate: false }
      options = false
    end
    save_without_rails3_options(options)
  end
  alias_method_chain :save, :rails3_options
end

ActiveRecord::NamedScope::ClassMethods.module_eval do
  # make all arguments optional, like Rails 3
  def scoped(scope = {}, &block)
    ActiveRecord::NamedScope::Scope.new(self, scope, &block)
  end
end

ActiveRecord::NamedScope::Scope.class_eval do
  # Scope delegates this to proxy_found because it's an array method; remove
  # the delegation and let it go through normal method_missing to the model
  remove_method :uniq

  # fake_arel doesn't quite work right here - somehow it's getting
  # Kernel#select. so just duplicate the select we want
  def select(value = Proc.new)
    if block_given?
      all.select {|*block_args| value.call(*block_args) }
{'fraction_non_alphanumeric': 0.10033140554941841, 'fraction_numerical': 0.0029891480927935537, 'mean_word_length': 2.520128087831656, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5911289', 'n_tokens_mistral': 4431, 'n_tokens_neox': 4261, 'n_words': 1063}