From da77f65c4684a8a9ee25c3e18f6f09824c765c2d Mon Sep 17 00:00:00 2001 From: nullkal Date: Wed, 13 Sep 2017 19:30:07 +0900 Subject: Add instance search feature (#4925) --- app/models/account.rb | 1 + app/models/instance_filter.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 app/models/instance_filter.rb (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index 864b3cb6e..f175c7081 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 000000000..5073cf1fa --- /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 -- cgit