docbrown/app/queries/internal/moderators_query.rb
Michael Kohl a4dadfb728
Standardize ActiveRecord order clauses (#9395)
* Change simple order clauses

* Change nested order clauses
2020-07-20 10:00:51 -04:00

37 lines
1 KiB
Ruby

module Internal
class ModeratorsQuery
DEFAULT_OPTIONS = {
state: :trusted
}.with_indifferent_access.freeze
def self.call(relation: User.all, options: {})
options = DEFAULT_OPTIONS.merge(options)
state, search = options.values_at(:state, :search)
relation = if state.to_s == "potential"
relation.where(
"id NOT IN (SELECT user_id FROM users_roles WHERE role_id = ?)",
role_id_for(:trusted),
).order("users.comments_count" => :desc)
else
relation.joins(:roles)
.where(users_roles: { role_id: role_id_for(state) })
end
relation = search_relation(relation, search) if search.presence
relation
end
def self.role_id_for(role)
Role.find_by!(name: role).id
end
def self.search_relation(relation, search)
relation.where(
"users.username ILIKE :search OR users.name ILIKE :search",
search: "%#{search}%",
)
end
end
end