Welcome to mirror list, hosted at ThFree Co, Russian Federation.

case_sensitivity.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e80365ee5b80894443b84e9e2b627d327cce290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

# Concern for querying columns with specific case sensitivity handling.
module CaseSensitivity
  extend ActiveSupport::Concern

  class_methods do
    # Queries the given columns regardless of the casing used.
    #
    # Unlike other ActiveRecord methods this method only operates on a Hash.
    def iwhere(params)
      criteria   = self
      cast_lower = Gitlab::Database.postgresql?

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

        condition =
          if cast_lower
            "LOWER(#{column}) = LOWER(:value)"
          else
            "#{column} = :value"
          end

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

      criteria
    end
  end
end