case_sensitivity.rb 753 Bytes
Newer Older
1 2 3 4 5 6 7 8
# Concern for querying columns with specific case sensitivity handling.
module CaseSensitivity
  extend ActiveSupport::Concern

  module ClassMethods
    # Queries the given columns regardless of the casing used.
    #
    # Unlike other ActiveRecord methods this method only operates on a Hash.
9
    def iwhere(params)
10 11 12 13 14 15
      criteria   = self
      cast_lower = Gitlab::Database.postgresql?

      params.each do |key, value|
        column = ActiveRecord::Base.connection.quote_table_name(key)

Douwe Maan's avatar
Douwe Maan committed
16 17 18 19 20 21
        condition =
          if cast_lower
            "LOWER(#{column}) = LOWER(:value)"
          else
            "#{column} = :value"
          end
22 23 24 25 26 27 28 29

        criteria = criteria.where(condition, value: value)
      end

      criteria
    end
  end
end