Skip to content

Commit

Permalink
Add instance search feature (#4925)
Browse files Browse the repository at this point in the history
  • Loading branch information
nullkal authored and Gargron committed Sep 13, 2017
1 parent 9e2ff3e commit da77f65
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/controllers/admin/instances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ def resubscribe

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
Expand All @@ -27,5 +31,11 @@ def ordered_instances
def subscribeable_accounts
Account.with_followers.remote.where(domain: params[:by_domain])
end

def filter_params
params.permit(
:domain_name
)
end
end
end
1 change: 1 addition & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions app/models/instance_filter.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions app/views/admin/instances/index.html.haml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit da77f65

Please sign in to comment.