Module: CustomAttributesFilter

Included in:
GroupsFinder, ProjectsFinder, UsersFinder, UsersStarProjectsFinder
Defined in:
app/finders/concerns/custom_attributes_filter.rb

Instance Method Summary collapse

Instance Method Details

#by_custom_attributes(items) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/finders/concerns/custom_attributes_filter.rb', line 5

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