Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6026: Improve timezones on org settings sent emails #6043

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 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,14 @@ 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 = (browser_time_zone && browser_time_zone != Time.zone) ? browser_time_zone : "Eastern Time (US & Canada)"
return time_date.in_time_zone(time_zone) if time_date.respond_to?(:in_time_zone)

time_date.to_time(time_zone)
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
40 changes: 40 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,42 @@ class MockController < ApplicationController
end
end
end

describe "#to_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 even if browser time zone has an invalid value" do
expected_time = time_date.in_time_zone(default_time_zone)
returned_time = controller.to_user_timezone(time_date.in_time_zone(Time.zone))
expect(returned_time).to eq(expected_time)
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 timezone" do
expect(controller.send(:to_user_timezone, time_date)).to eq(time_date.in_time_zone(default_time_zone))
end
end

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

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

it "returns nil for invalid date string" do
expect(controller.send(:to_user_timezone, "invalid-date")).to eq(nil)
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