debian-mirror-gitlab/app/models/concerns/case_sensitivity.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.2 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2015-10-24 18:46:33 +05:30
# Concern for querying columns with specific case sensitivity handling.
module CaseSensitivity
extend ActiveSupport::Concern
2018-11-20 20:47:30 +05:30
class_methods do
2015-10-24 18:46:33 +05:30
# Queries the given columns regardless of the casing used.
#
# Unlike other ActiveRecord methods this method only operates on a Hash.
def iwhere(params)
2018-11-20 20:47:30 +05:30
criteria = self
2015-10-24 18:46:33 +05:30
2021-02-22 17:27:13 +05:30
params.each do |column, value|
column = arel_table[column] unless column.is_a?(Arel::Attribute)
2018-11-20 20:47:30 +05:30
criteria = case value
when Array
2021-02-22 17:27:13 +05:30
criteria.where(value_in(column, value))
2018-11-20 20:47:30 +05:30
else
2021-02-22 17:27:13 +05:30
criteria.where(value_equal(column, value))
2018-11-20 20:47:30 +05:30
end
end
criteria
end
private
2015-10-24 18:46:33 +05:30
2018-11-20 20:47:30 +05:30
def value_equal(column, value)
lower_value = lower_value(value)
2021-02-22 17:27:13 +05:30
lower_column(column).eq(lower_value).to_sql
2018-11-20 20:47:30 +05:30
end
2015-10-24 18:46:33 +05:30
2018-11-20 20:47:30 +05:30
def value_in(column, values)
lower_values = values.map do |value|
lower_value(value)
2015-10-24 18:46:33 +05:30
end
2021-02-22 17:27:13 +05:30
lower_column(column).in(lower_values).to_sql
2018-11-20 20:47:30 +05:30
end
def lower_value(value)
Arel::Nodes::NamedFunction.new('LOWER', [Arel::Nodes.build_quoted(value)])
end
def lower_column(column)
column.lower
2015-10-24 18:46:33 +05:30
end
end
end