Skip to content

Commit

Permalink
Helper method for when a banner expires for index page
Browse files Browse the repository at this point in the history
  • Loading branch information
albertchae committed Jun 1, 2024
1 parent a9b07bf commit 2619a57
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
10 changes: 10 additions & 0 deletions app/helpers/banner_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ def conditionally_add_hidden_class(current_banner_is_active)
"d-none"
end
end

def banner_expiration_time_in_words(banner)
if banner.expired?
"Yes"
elsif banner.expires_at
"in #{distance_of_time_in_words(Time.now, banner.expires_at)}"
else
"No"
end
end
end
6 changes: 1 addition & 5 deletions app/views/banners/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@
<% end %>
</td>
<td class="min-width">
<% if banner.expires_at %>
<%= distance_of_time_in_words(Time.now, banner.expires_at) %>
<% else %>
No
<% end %>
<%= banner_expiration_time_in_words(banner) %>
</td>
<td class="min-width">
<%= banner.user.display_name %>
Expand Down
28 changes: 28 additions & 0 deletions spec/helpers/banner_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@
expect(helper.conditionally_add_hidden_class(true)).to eq(nil)
end
end

describe "#banner_expiration_time_in_words" do
let(:banner) { create(:banner, expires_at: expires_at) }

context "when expires_at isn't set" do
let(:expires_at) { nil }

it "returns No" do
expect(helper.banner_expiration_time_in_words(banner)).to eq("No")
end
end

context "when expires_at is in the future" do
let(:expires_at) { 7.days.from_now }

it "returns a word description of how far in the future" do
expect(helper.banner_expiration_time_in_words(banner)).to eq("in 7 days")
end
end

context "when expires_at is in the past" do
let(:expires_at) { 7.days.ago }

it "returns yes" do
expect(helper.banner_expiration_time_in_words(banner)).to eq("Yes")
end
end
end
end

0 comments on commit 2619a57

Please sign in to comment.