-
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4866 from rubyforgood/elhalvers/4828-local-timezone
Elhalvers/4828 local timezone
- Loading branch information
Showing
11 changed files
with
132 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Users | ||
module TimeZone | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
helper_method :browser_time_zone | ||
end | ||
|
||
def browser_time_zone | ||
browser_tz = ActiveSupport::TimeZone.find_tzinfo(cookies[:browser_time_zone]) | ||
ActiveSupport::TimeZone.all.find { |zone| zone.tzinfo == browser_tz } || Time.zone | ||
rescue TZInfo::UnknownTimezone, TZInfo::InvalidTimezoneIdentifier | ||
Time.zone | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Cookies from 'js-cookie' | ||
import jstz from 'jstz' | ||
|
||
// Rails doesn't support every timezone that Intl supports | ||
export function findTimeZone () { | ||
const oldIntl = window.Intl | ||
try { | ||
window.Intl = undefined | ||
const tz = jstz.determine().name() | ||
window.Intl = oldIntl | ||
return tz | ||
} catch (e) { | ||
// sometimes (on android) you can't override intl | ||
return jstz.determine().name() | ||
} | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
Cookies.set('browser_time_zone', findTimeZone(), { expires: 365, path: '/', secure: true, sameSite: 'strict' }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "rails_helper" | ||
|
||
class MockController < ApplicationController | ||
include Users::TimeZone | ||
end | ||
|
||
RSpec.describe MockController, type: :controller do | ||
let(:browser_time_zone) { "America/Los_Angeles" } | ||
before do | ||
allow(controller).to receive(:cookies).and_return(browser_time_zone: browser_time_zone) | ||
end | ||
|
||
describe "#browser_time_zone" do | ||
it "returns the matching time zone" do | ||
browser_tz = ActiveSupport::TimeZone.find_tzinfo(browser_time_zone) | ||
matching_zone = ActiveSupport::TimeZone.all.find { |zone| zone.tzinfo == browser_tz } | ||
expect(controller.browser_time_zone).to eq(matching_zone || Time.zone) | ||
end | ||
|
||
context "when browser_time_zone cookie is not set" do | ||
before do | ||
allow(controller).to receive(:cookies).and_return({}) | ||
end | ||
|
||
it "returns the default time zone" do | ||
expect(controller.browser_time_zone).to eq(Time.zone) | ||
end | ||
end | ||
|
||
context "when browser_time_zone cookie contains 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.browser_time_zone).to eq(Time.zone) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters