Change order of organizations on /admin (#19499)

* Change order of organizations on /admin

* Rubocop
This commit is contained in:
Joshua Wehner 2023-05-23 10:41:18 +02:00 committed by GitHub
parent 56c914cc8a
commit 50a286fe75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 8 deletions

View file

@ -1,6 +1,7 @@
module Admin
class OrganizationsController < Admin::ApplicationController
layout "admin"
PER_PAGE_MAX = 50
CREDIT_ACTIONS = {
add: :add_to,
@ -8,14 +9,8 @@ module Admin
}.with_indifferent_access.freeze
def index
@organizations = Organization.order(name: :desc).page(params[:page]).per(50)
return if params[:search].blank?
@organizations = @organizations.where(
"name ILIKE ?",
"%#{params[:search].strip}%",
)
@organizations = Organization.simple_name_match(params[:search].presence)
.page(params[:page]).per(PER_PAGE_MAX)
end
def show

View file

@ -68,6 +68,14 @@ class Organization < ApplicationRecord
alias_attribute :old_old_username, :old_old_slug
alias_attribute :website_url, :url
def self.simple_name_match(query)
scope = order(:name)
query&.strip!
return scope if query.blank?
scope.where("name ILIKE ?", "%#{query}%")
end
def self.integer_only
I18n.t("models.organization.integer_only")
end

View file

@ -339,4 +339,40 @@ RSpec.describe Organization do
expect(organization.enough_credits?(1)).to be(true)
end
end
describe ".simple_name_match" do
before do
create(:organization, name: "Not Matching")
create(:organization, name: "For Fans of Books")
create(:organization, name: "Boo! A Ghost")
end
it "finds them by simple ilike match" do
query = "boo"
results = described_class.simple_name_match(query)
expect(results.pluck(:name)).to eq(["Boo! A Ghost", "For Fans of Books"])
query = "book"
results = described_class.simple_name_match(query)
expect(results.pluck(:name)).to eq(["For Fans of Books"])
query = " BOOK "
results = described_class.simple_name_match(query)
expect(results.pluck(:name)).to eq(["For Fans of Books"])
end
it "returns all orgs on empty query" do
query = nil
results = described_class.simple_name_match(query)
expect(results.size).to eq(3)
query = ""
results = described_class.simple_name_match(query)
expect(results.size).to eq(3)
query = " "
results = described_class.simple_name_match(query)
expect(results.size).to eq(3)
end
end
end