about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/invite.rb3
-rw-r--r--app/models/invite_filter.rb32
2 files changed, 35 insertions, 0 deletions
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