Skip to content

Commit

Permalink
6026: Improve timezones on org settings sent emails (#6043)
Browse files Browse the repository at this point in the history
* add partial specs for to_user_timezone helper

* add to_user_timezone helper

* replace emails created default time zone with user's time zone

* run linter

* fix views rspec

* update tests and to_user_timezone helper

* change syntax for calling the helper

* add specs for user_timezone

* split to_user_timezone method

* add a happy path test
  • Loading branch information
panacotar authored Oct 9, 2024
1 parent 01b966b commit 1881b09
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/controllers/concerns/users/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module TimeZone

included do
helper_method :browser_time_zone
helper_method :to_user_timezone
end

def browser_time_zone
Expand All @@ -12,5 +13,18 @@ def browser_time_zone
rescue TZInfo::UnknownTimezone, TZInfo::InvalidTimezoneIdentifier
Time.zone
end

def to_user_timezone(time_date)
return "" if time_date.nil? || (time_date.instance_of?(String) && time_date.empty?)

time_zone = user_timezone
return time_date.in_time_zone(time_zone) if time_date.respond_to?(:in_time_zone)

time_date.to_time(time_zone)
end

def user_timezone
(browser_time_zone && browser_time_zone != Time.zone) ? browser_time_zone : "Eastern Time (US & Canada)"
end
end
end
2 changes: 1 addition & 1 deletion app/views/casa_org/_sent_emails.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<%= sent_email.user.display_name %> &lt;<%= sent_email.sent_address %>&gt;
</td>
<td>
<%= sent_email.created_at.strftime("%l:%M%P %d %b %Y") %>
<%= to_user_timezone(sent_email.created_at).strftime("%l:%M%P %d %b %Y") %>
</td>
</tr>
<% end %>
Expand Down
48 changes: 48 additions & 0 deletions spec/controllers/concerns/users/time_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class MockController < ApplicationController

RSpec.describe MockController, type: :controller do
let(:browser_time_zone) { "America/Los_Angeles" }
let(:default_time_zone) { "Eastern Time (US & Canada)" }
let(:time_date) { Time.zone.now }
before do
allow(controller).to receive(:cookies).and_return(browser_time_zone: browser_time_zone)
end
Expand Down Expand Up @@ -37,4 +39,50 @@ class MockController < ApplicationController
end
end
end

describe "#to_user_timezone" do
it "takes a time_date and converts it to user's time zone" do
expected = controller.to_user_timezone(time_date)
returned = time_date.in_time_zone(browser_time_zone)
expect(expected.zone).to eq(returned.zone)
expect(expected.day).to eq(returned.day)
expect(expected.hour).to eq(returned.hour)
end

context "when invalid param is sent" do
it "returns the empty string for nil param" do
expect(controller.to_user_timezone(nil)).to eq("")
end

it "returns empty string if empty string param provided" do
expect(controller.to_user_timezone("")).to eq("")
end

it "returns nil for invalid date string" do
expect(controller.to_user_timezone("invalid-date")).to eq(nil)
end
end
end

describe "#user_timezone" do
context "when browser time zone has an invalid value" do
before do
allow(controller).to receive(:cookies).and_return(browser_time_zone: "Invalid/Timezone")
end

it "returns the default time zone" do
expect(controller.user_timezone).to eq(default_time_zone)
end
end

context "when browser time zone is not set" do
before do
allow(controller).to receive(:cookies).and_return({})
end

it "returns the default time zone" do
expect(controller.user_timezone).to eq(default_time_zone)
end
end
end
end
3 changes: 3 additions & 0 deletions spec/views/casa_orgs/edit.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
organization = build_stubbed(:casa_org)
admin = build_stubbed(:casa_admin, casa_org: organization)
allow(view).to receive(:current_organization).and_return(organization)
without_partial_double_verification do
allow(view).to receive(:to_user_timezone).and_return(Time.zone.local(2021, 1, 2, 12, 30, 0))
end

sent_email = build_stubbed(:sent_email, user: admin, created_at: Time.zone.local(2021, 1, 2, 12, 30, 0))
assign(:sent_emails, [sent_email])
Expand Down

0 comments on commit 1881b09

Please sign in to comment.