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/subscription.rb24
1 files changed, 19 insertions, 5 deletions
diff --git a/app/models/subscription.rb b/app/models/subscription.rb
index 6046ae4c2..35a228df0 100644
--- a/app/models/subscription.rb
+++ b/app/models/subscription.rb
@@ -1,4 +1,5 @@
 # frozen_string_literal: true
+
 # == Schema Information
 #
 # Table name: subscriptions
@@ -15,18 +16,20 @@
 #
 
 class Subscription < ApplicationRecord
-  MIN_EXPIRATION = 3600 * 24 * 7
-  MAX_EXPIRATION = 3600 * 24 * 30
+  MIN_EXPIRATION = 7.days.seconds.to_i
+  MAX_EXPIRATION = 30.days.seconds.to_i
 
   belongs_to :account, required: true
 
   validates :callback_url, presence: true
   validates :callback_url, uniqueness: { scope: :account_id }
 
-  scope :active, -> { where(confirmed: true).where('expires_at > ?', Time.now.utc) }
+  scope :confirmed, -> { where(confirmed: true) }
+  scope :future_expiration, -> { where(arel_table[:expires_at].gt(Time.now.utc)) }
+  scope :active, -> { confirmed.future_expiration }
 
-  def lease_seconds=(str)
-    self.expires_at = Time.now.utc + [[MIN_EXPIRATION, str.to_i].max, MAX_EXPIRATION].min.seconds
+  def lease_seconds=(value)
+    self.expires_at = future_expiration(value)
   end
 
   def lease_seconds
@@ -41,6 +44,17 @@ class Subscription < ApplicationRecord
 
   private
 
+  def future_expiration(value)
+    Time.now.utc + future_offset(value).seconds
+  end
+
+  def future_offset(seconds)
+    [
+      [MIN_EXPIRATION, seconds.to_i].max,
+      MAX_EXPIRATION,
+    ].min
+  end
+
   def set_min_expiration
     self.lease_seconds = 0 unless expires_at
   end