about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-05-05 14:56:00 -0400
committerGitHub <noreply@github.com>2017-05-05 14:56:00 -0400
commit484c9709b67685c95de351a39e3dfb140acd3681 (patch)
tree0d74cd725e46cd75ca1568d866d5d5ef906b8c0a /app
parentd08f1112d517788fb66d2683766cc168bac48315 (diff)
Misc spec coverage improvements (#2821)
* Dont use raise_error by itself (avoids warning)

* Add coverage for AccountFilter

* Improve coverage and refactor for Subscription#lease_seconds

* Improve coverage and refactor for NotificationMailer

* Simplify assignment of min/max threshold on subscription
Diffstat (limited to 'app')
-rw-r--r--app/mailers/notification_mailer.rb20
-rw-r--r--app/models/subscription.rb24
2 files changed, 33 insertions, 11 deletions
diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb
index 2bfc77721..f308c403b 100644
--- a/app/mailers/notification_mailer.rb
+++ b/app/mailers/notification_mailer.rb
@@ -7,7 +7,7 @@ class NotificationMailer < ApplicationMailer
     @me     = recipient
     @status = notification.target_status
 
-    I18n.with_locale(@me.user.locale || I18n.default_locale) do
+    locale_for_account(@me) do
       mail to: @me.user.email, subject: I18n.t('notification_mailer.mention.subject', name: @status.account.acct)
     end
   end
@@ -16,7 +16,7 @@ class NotificationMailer < ApplicationMailer
     @me      = recipient
     @account = notification.from_account
 
-    I18n.with_locale(@me.user.locale || I18n.default_locale) do
+    locale_for_account(@me) do
       mail to: @me.user.email, subject: I18n.t('notification_mailer.follow.subject', name: @account.acct)
     end
   end
@@ -26,7 +26,7 @@ class NotificationMailer < ApplicationMailer
     @account = notification.from_account
     @status  = notification.target_status
 
-    I18n.with_locale(@me.user.locale || I18n.default_locale) do
+    locale_for_account(@me) do
       mail to: @me.user.email, subject: I18n.t('notification_mailer.favourite.subject', name: @account.acct)
     end
   end
@@ -36,7 +36,7 @@ class NotificationMailer < ApplicationMailer
     @account = notification.from_account
     @status  = notification.target_status
 
-    I18n.with_locale(@me.user.locale || I18n.default_locale) do
+    locale_for_account(@me) do
       mail to: @me.user.email, subject: I18n.t('notification_mailer.reblog.subject', name: @account.acct)
     end
   end
@@ -45,7 +45,7 @@ class NotificationMailer < ApplicationMailer
     @me      = recipient
     @account = notification.from_account
 
-    I18n.with_locale(@me.user.locale || I18n.default_locale) do
+    locale_for_account(@me) do
       mail to: @me.user.email, subject: I18n.t('notification_mailer.follow_request.subject', name: @account.acct)
     end
   end
@@ -58,7 +58,7 @@ class NotificationMailer < ApplicationMailer
 
     return if @notifications.empty?
 
-    I18n.with_locale(@me.user.locale || I18n.default_locale) do
+    locale_for_account(@me) do
       mail to: @me.user.email,
            subject: I18n.t(
              :subject,
@@ -67,4 +67,12 @@ class NotificationMailer < ApplicationMailer
            )
     end
   end
+
+  private
+
+  def locale_for_account(account)
+    I18n.with_locale(account.user.locale || I18n.default_locale) do
+      yield
+    end
+  end
 end
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