diff options
author | Markus Koller <markus-koller@gmx.ch> | 2017-09-28 19:49:42 +0300 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-09-28 19:49:42 +0300 |
commit | e9eae3eb0dd25e4a34c9a4b6bcc7de312dde4489 (patch) | |
tree | d2402a99dd96927ae458f49cbd7de8ad043e297a /app/finders/concerns | |
parent | 93a33556e3e01a83c1af9c21e11ff433b624c40d (diff) |
Support custom attributes on users
Diffstat (limited to 'app/finders/concerns')
-rw-r--r-- | app/finders/concerns/custom_attributes_filter.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/app/finders/concerns/custom_attributes_filter.rb b/app/finders/concerns/custom_attributes_filter.rb new file mode 100644 index 00000000000..5bbf9ca242d --- /dev/null +++ b/app/finders/concerns/custom_attributes_filter.rb @@ -0,0 +1,20 @@ +module CustomAttributesFilter + 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 +end |