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

51 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
params.each do |key, value|
2018-11-20 20:47:30 +05:30
criteria = case value
when Array
criteria.where(value_in(key, value))
else
criteria.where(value_equal(key, value))
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)
lower_column(arel_table[column]).eq(lower_value).to_sql
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
2018-11-20 20:47:30 +05:30
lower_column(arel_table[column]).in(lower_values).to_sql
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