docbrown/app/queries/admin/moderators_query.rb
Josh Puetz 1c566e0ec4
[deploy] Move /internal to `/admin (#9639)
* First draft - all the big changes

* Changing some more references to 'internal'

* Relocate internal request tests to admin

* Relocate internal system tests to admin

* Fix trailing space

* Test fix

* Move queries from internal to admin

* Docs updates

* Rename internal stimuls controllers to admin (plus docs)

* Rename admin layout

* Fix routing after rebase

* Fixes for latest added admin interfaces

* Serviceworker ignore paths
2020-08-07 10:36:26 -04:00

37 lines
1 KiB
Ruby

module Admin
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