Fix subscription expiration condition (#2715)

* Fix subscription expiration condition

* dry and add spec
remotes/1727458204337373841/tmp_refs/heads/signup-info-prompt
Yamagishi Kazutoshi 2017-05-03 01:21:22 +09:00 committed by Eugen Rochko
parent b9b78549f3
commit 6f75c8451d
3 changed files with 20 additions and 3 deletions

View File

@ -33,6 +33,10 @@ class Subscription < ApplicationRecord
(expires_at - Time.now.utc).to_i (expires_at - Time.now.utc).to_i
end end
def expired?
Time.now.utc > expires_at
end
before_validation :set_min_expiration before_validation :set_min_expiration
private private

View File

@ -11,7 +11,6 @@
%th= t('admin.pubsubhubbub.last_delivery') %th= t('admin.pubsubhubbub.last_delivery')
%tbody %tbody
- @subscriptions.each do |subscription| - @subscriptions.each do |subscription|
- expired = Time.now.utc < subscription.expires_at
%tr %tr
%td %td
%samp= subscription.account.acct %samp= subscription.account.acct
@ -20,8 +19,8 @@
%td %td
- if subscription.confirmed? - if subscription.confirmed?
%i.fa.fa-check %i.fa.fa-check
%td{ style: "color: #{expired ? 'red' : 'inherit'};" } %td{ style: "color: #{subscription.expired? ? 'red' : 'inherit'};" }
= precede expired ? '-' : '' do = precede subscription.expired? ? '-' : '' do
= time_ago_in_words(subscription.expires_at) = time_ago_in_words(subscription.expires_at)
%td %td
- if subscription.last_successful_delivery_at? - if subscription.last_successful_delivery_at?

View File

@ -1,5 +1,19 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Subscription, type: :model do RSpec.describe Subscription, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
subject { Fabricate(:subscription, account: alice) }
describe '#expired?' do
it 'return true when expires_at is past' do
subject.expires_at = 2.days.ago
expect(subject.expired?).to be true
end
it 'return false when expires_at is future' do
subject.expires_at = 2.days.from_now
expect(subject.expired?).to be false
end
end
end end