gitlab-org--gitlab-foss/app/finders/concerns/custom_attributes_filter.rb

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

25 lines
909 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-09-28 16:49:42 +00:00
module CustomAttributesFilter
# rubocop: disable CodeReuse/ActiveRecord
2017-09-28 16:49:42 +00:00
def by_custom_attributes(items)
return items unless params[:custom_attributes].is_a?(Hash)
return items unless Ability.allowed?(current_user, :read_custom_attribute)
association = items.reflect_on_association(:custom_attributes)
attributes_table = association.klass.arel_table
attributable_table = items.model.arel_table
custom_attributes = association.klass.select('true').where(
attributes_table[association.foreign_key]
.eq(attributable_table[association.association_primary_key])
)
# perform a subquery for each attribute to be filtered
params[:custom_attributes].inject(items) do |scope, (key, value)|
scope.where('EXISTS (?)', custom_attributes.where(key: key, value: value))
end
end
# rubocop: enable CodeReuse/ActiveRecord
2017-09-28 16:49:42 +00:00
end