about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorYamagishi Kazutoshi <ykzts@desire.sh>2017-12-02 00:40:02 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-12-01 16:40:02 +0100
commit74320971e2cc9f605dbcc23c52ac36e18b80716f (patch)
tree444edcce8371270e8f4a632a4423fc589ec9aa7a /app
parenteee3b32b7714a302ae384c92ef13601167ec892a (diff)
Add invite filter (#5862)
Diffstat (limited to 'app')
-rw-r--r--app/controllers/admin/invites_controller.rb10
-rw-r--r--app/helpers/admin/filter_helper.rb3
-rw-r--r--app/models/invite.rb3
-rw-r--r--app/models/invite_filter.rb32
-rw-r--r--app/views/admin/invites/index.html.haml8
5 files changed, 54 insertions, 2 deletions
diff --git a/app/controllers/admin/invites_controller.rb b/app/controllers/admin/invites_controller.rb
index 607588d90..faccaa7c8 100644
--- a/app/controllers/admin/invites_controller.rb
+++ b/app/controllers/admin/invites_controller.rb
@@ -5,7 +5,7 @@ module Admin
     def index
       authorize :invite, :index?
 
-      @invites = Invite.includes(user: :account).page(params[:page])
+      @invites = filtered_invites.includes(user: :account).page(params[:page])
       @invite  = Invite.new
     end
 
@@ -35,5 +35,13 @@ module Admin
     def resource_params
       params.require(:invite).permit(:max_uses, :expires_in)
     end
+
+    def filtered_invites
+      InviteFilter.new(filter_params).results
+    end
+
+    def filter_params
+      params.permit(:available, :expired)
+    end
   end
 end
diff --git a/app/helpers/admin/filter_helper.rb b/app/helpers/admin/filter_helper.rb
index e0fae9d9a..73250cbf5 100644
--- a/app/helpers/admin/filter_helper.rb
+++ b/app/helpers/admin/filter_helper.rb
@@ -3,8 +3,9 @@
 module Admin::FilterHelper
   ACCOUNT_FILTERS = %i(local remote by_domain silenced suspended recent username display_name email ip).freeze
   REPORT_FILTERS = %i(resolved account_id target_account_id).freeze
+  INVITE_FILTER = %i(available expired).freeze
 
-  FILTERS = ACCOUNT_FILTERS + REPORT_FILTERS
+  FILTERS = ACCOUNT_FILTERS + REPORT_FILTERS + INVITE_FILTER
 
   def filter_link_to(text, link_to_params, link_class_params = link_to_params)
     new_url = filtered_url_for(link_to_params)
diff --git a/app/models/invite.rb b/app/models/invite.rb
index 7626f4cfa..6907c1f1d 100644
--- a/app/models/invite.rb
+++ b/app/models/invite.rb
@@ -17,6 +17,9 @@ class Invite < ApplicationRecord
   belongs_to :user, required: true
   has_many :users, inverse_of: :invite
 
+  scope :available, -> { where(expires_at: nil).or(where('expires_at >= ?', Time.now.utc)) }
+  scope :expired, -> { where.not(expires_at: nil).where('expires_at < ?', Time.now.utc) }
+
   before_validation :set_code
 
   attr_reader :expires_in
diff --git a/app/models/invite_filter.rb b/app/models/invite_filter.rb
new file mode 100644
index 000000000..7d89bad4a
--- /dev/null
+++ b/app/models/invite_filter.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+class InviteFilter
+  attr_reader :params
+
+  def initialize(params)
+    @params = params
+  end
+
+  def results
+    scope = Invite.order(created_at: :desc)
+
+    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 'available'
+      Invite.available
+    when 'expired'
+      Invite.expired
+    else
+      raise "Unknown filter: #{key}"
+    end
+  end
+end
diff --git a/app/views/admin/invites/index.html.haml b/app/views/admin/invites/index.html.haml
index 52a748fe0..944a60471 100644
--- a/app/views/admin/invites/index.html.haml
+++ b/app/views/admin/invites/index.html.haml
@@ -1,6 +1,14 @@
 - content_for :page_title do
   = t('admin.invites.title')
 
+.filters
+  .filter-subset
+    %strong= t('admin.invites.filter.title')
+    %ul
+      %li= filter_link_to t('admin.invites.filter.all'), available: nil, expired: nil
+      %li= filter_link_to t('admin.invites.filter.available'), available: 1, expired: nil
+      %li= filter_link_to t('admin.invites.filter.expired'), available: nil, expired: 1
+
 - if policy(:invite).create?
   %p= t('invites.prompt')