about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorJenkins <jenkins@jenkins.ninjawedding.org>2017-12-02 14:17:11 +0000
committerJenkins <jenkins@jenkins.ninjawedding.org>2017-12-02 14:17:11 +0000
commit933eafdcd3a3803e9fcf934f6f7ade667281c527 (patch)
tree94f5ab03f7cd733cc0743f3c79cf02a8de4c148f /app/models
parentad46bc9772dfc52c7ed522658eda1a3ef608a7b3 (diff)
parent51f2eca8877d8c56a19e1b8ce4edf407c2d236fb (diff)
Merge remote-tracking branch 'tootsuite/master' into glitchsoc/master
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