Skip to content

Commit

Permalink
Move timezone conversion for frontend display to model method
Browse files Browse the repository at this point in the history
  • Loading branch information
albertchae committed Jun 1, 2024
1 parent 125d60e commit a9b07bf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/models/banner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def expired?
expires_at && Time.current > expires_at
end

def expires_at_in_time_zone(timezone)
expires_at&.in_time_zone(timezone)
end

private

def only_one_banner_is_active_per_organization
Expand Down
2 changes: 1 addition & 1 deletion app/views/banners/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</div>
<span class="input-style-1">
<%= form.label :expires_at, "Expires at (optional)" %>
<%= form.datetime_field :expires_at, value: banner.expires_at&.in_time_zone(cookies[:browser_time_zone]), required: false %>
<%= form.datetime_field :expires_at, value: banner.expires_at_in_time_zone(cookies[:browser_time]), required: false %>
</span>
<div class="input-style-1">
<%= form.label :content %>
Expand Down
14 changes: 14 additions & 0 deletions spec/models/banner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@
expect(banner).to be_expired
end
end

describe "#expires_at_in_time_zone" do
it "can shift time by timezone for equivalent times" do
banner = create(:banner, expires_at: "2024-06-13 12:00:00 UTC")

expires_at_in_pacific_time = banner.expires_at_in_time_zone("America/Los_Angeles")
expect(expires_at_in_pacific_time.to_s).to eq("2024-06-13 05:00:00 -0700")

expires_at_in_eastern_time = banner.expires_at_in_time_zone("America/New_York")
expect(expires_at_in_eastern_time.to_s).to eq("2024-06-13 08:00:00 -0400")

expect(expires_at_in_pacific_time).to eq(expires_at_in_eastern_time)
end
end
end

0 comments on commit a9b07bf

Please sign in to comment.