From 7f432a97a03e5568edbba0194500d0b33ad8acfa Mon Sep 17 00:00:00 2001 From: nullkal Date: Wed, 13 Sep 2017 19:30:07 +0900 Subject: [PATCH] Add instance search feature (#4925) --- app/controllers/admin/instances_controller.rb | 12 +++++++- app/models/account.rb | 1 + app/models/instance_filter.rb | 28 +++++++++++++++++++ app/views/admin/instances/index.html.haml | 10 +++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 app/models/instance_filter.rb diff --git a/app/controllers/admin/instances_controller.rb b/app/controllers/admin/instances_controller.rb index 3296e08db8..22f02e5d08 100644 --- a/app/controllers/admin/instances_controller.rb +++ b/app/controllers/admin/instances_controller.rb @@ -14,8 +14,12 @@ module Admin private + def filtered_instances + InstanceFilter.new(filter_params).results + end + def paginated_instances - Account.remote.by_domain_accounts.page(params[:page]) + filtered_instances.page(params[:page]) end helper_method :paginated_instances @@ -27,5 +31,11 @@ module Admin def subscribeable_accounts Account.with_followers.remote.where(domain: params[:by_domain]) end + + def filter_params + params.permit( + :domain_name + ) + end end end diff --git a/app/models/account.rb b/app/models/account.rb index 864b3cb6e6..f175c70818 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -104,6 +104,7 @@ class Account < ApplicationRecord scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') } scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) } scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) } + scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) } delegate :email, :current_sign_in_ip, diff --git a/app/models/instance_filter.rb b/app/models/instance_filter.rb new file mode 100644 index 0000000000..5073cf1faa --- /dev/null +++ b/app/models/instance_filter.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class InstanceFilter + attr_reader :params + + def initialize(params) + @params = params + end + + def results + scope = Account.remote.by_domain_accounts + params.each do |key, value| + scope.merge!(scope_for(key, value)) if value.present? + end + scope + end + + private + + def scope_for(key, value) + case key.to_s + when 'domain_name' + Account.matches_domain(value) + else + raise "Unknown filter: #{key}" + end + end +end diff --git a/app/views/admin/instances/index.html.haml b/app/views/admin/instances/index.html.haml index edbd3b2173..3314ce0778 100644 --- a/app/views/admin/instances/index.html.haml +++ b/app/views/admin/instances/index.html.haml @@ -1,6 +1,16 @@ - content_for :page_title do = t('admin.instances.title') += form_tag admin_instances_url, method: 'GET', class: 'simple_form' do + .fields-group + - %i(domain_name).each do |key| + .input.string.optional + = text_field_tag key, params[key], class: 'string optional', placeholder: I18n.t("admin.instances.#{key}") + + .actions + %button= t('admin.instances.search') + = link_to t('admin.instances.reset'), admin_instances_path, class: 'button negative' + .table-wrapper %table.table %thead