Skip to content

Commit

Permalink
Token: Status method should be in decorator instead of model
Browse files Browse the repository at this point in the history
  • Loading branch information
Un3x committed Oct 20, 2023
1 parent 434e9a2 commit 69f53ce
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
7 changes: 7 additions & 0 deletions app/decorators/token_show_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def passed_time_as_ratio
time_passed.to_f / total_duration * 100
end

def status
return 'expired' if expired?
return 'revoked' if blacklisted? || blacklisted_later?

'active'
end

def progress_bar_color
day_left = (exp - Time.zone.now.to_i) / 86_400
if day_left < 30
Expand Down
8 changes: 3 additions & 5 deletions app/models/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ def blacklisted?
blacklisted_at < Time.zone.now
end

def status
return 'expired' if expired?
return 'blacklisted' if blacklisted?

'active'
def blacklisted_later?
blacklisted_at.present? &&
blacklisted_at > Time.zone.now
end

private
Expand Down
22 changes: 22 additions & 0 deletions spec/decorators/token_show_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,26 @@
it { is_expected.to eq('red') }
end
end

describe '#status' do
subject { described_class.new(token).status }

context 'when expired' do
let(:token) { create(:token, exp: 1.day.ago) }

it { is_expected.to eq('expired') }
end

context 'when blacklisted' do
let(:token) { create(:token, blacklisted_at: 1.day.ago) }

it { is_expected.to eq('revoked') }
end

context 'when active' do
let(:token) { create(:token) }

it { is_expected.to eq('active') }
end
end
end
22 changes: 0 additions & 22 deletions spec/models/token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,4 @@
end
end
end

describe '#status' do
subject { token.status }

context 'when expired' do
let(:token) { create(:token, exp: 1.day.ago) }

it { is_expected.to eq('expired') }
end

context 'when blacklisted' do
let(:token) { create(:token, blacklisted_at: 1.day.ago) }

it { is_expected.to eq('blacklisted') }
end

context 'when active' do
let(:token) { create(:token) }

it { is_expected.to eq('active') }
end
end
end

0 comments on commit 69f53ce

Please sign in to comment.