-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add Time Zone Select #6437
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
base: main
Are you sure you want to change the base?
Add Time Zone Select #6437
Changes from all commits
e498f79
cebd0b9
6b40357
d7951f8
a1e4b82
dd45a71
7bc3146
b703657
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ def index | |
| end | ||
| end | ||
|
|
||
| context "successful request" do | ||
| context "authorized request" do | ||
| before do | ||
| user = create(:admin_user, email: "admin@example.com") | ||
| allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(user) | ||
|
|
@@ -43,6 +43,12 @@ def index | |
| get :index | ||
| expect(response.code).to eq "200" | ||
| end | ||
|
|
||
| it "sets timezone by param" do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| get :index, params: {solidus_timezone: "Hawaii"} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideHashLiteralBraces: Space inside { missing. |
||
| expect(session).to have_key(:solidus_timezone) | ||
| expect(session[:solidus_timezone]).to eq("Hawaii") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| end | ||
| end | ||
|
|
||
| describe "layout rendering" do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ | |
| let(:sign_in_date) { DateTime.now } | ||
|
|
||
| before do | ||
| allow_any_instance_of(Spree.user_class).to receive(:try).with(:timezone) { nil } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [86/80] |
||
| allow_any_instance_of(Spree.user_class).to receive(:try).with(:email).and_call_original | ||
| allow_any_instance_of(Spree.user_class).to receive(:try).with(:last_sign_in_at).and_return(sign_in_date) | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <% available_timezones_for_select = ActiveSupport::TimeZone.all.map(&:name) %> | ||
|
|
||
| <%= form_tag(url_for, method: :get, style: "width: 100%;") do %> | ||
| <label class="admin-navbar-selection admin-timezone-selection"> | ||
| <i class="fa fa-globe fa-fw" title="<%= I18n.t('spree.choose_dashboard_locale') %>"></i> | ||
| <select name="solidus_timezone" class="custom-select fullwidth" onchange="this.form.requestSubmit()"> | ||
| <%= options_for_select(available_timezones_for_select, selected: Time.zone.name) %> | ||
| </select> | ||
| </label> | ||
| <% end %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <% available_timezones_for_select = ActiveSupport::TimeZone.all.map(&:name) %> | ||
|
|
||
| <li> | ||
| <%= form_tag(url_for, method: :get, style: "width: 100%;") do %> | ||
| <label> | ||
| <svg aria-hidden="true"><use xlink:href="<%= image_path('spree/backend/themes/solidus_admin/remixicon.symbol.svg') %>#ri-time-zone-line"></use></svg> | ||
| <select name="solidus_timezone" onchange="this.form.requestSubmit()"> | ||
| <%= options_for_select(available_timezones_for_select, selected: Time.zone.name) %> | ||
| </select> | ||
| <svg aria-hidden="true"><use xlink:href="<%= image_path('spree/backend/themes/solidus_admin/remixicon.symbol.svg') %>#ri-expand-up-down-line"></use></svg> | ||
| </label> | ||
| <% end %> | ||
| </li> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,4 +38,19 @@ def index | |
| end | ||
| end | ||
| end | ||
|
|
||
| context "authorized request" do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| stub_authorization! | ||
|
|
||
| it "allows access" do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| get :index | ||
| expect(response.body).to eq("test") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| end | ||
|
|
||
| it "sets timezone by param" do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| get :index, params: {solidus_timezone: "Hawaii"} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideHashLiteralBraces: Space inside { missing. |
||
| expect(session).to have_key(:solidus_timezone) | ||
| expect(session[:solidus_timezone]).to eq("Hawaii") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ module Admin | |
| let(:stock_item) { variant.stock_items.first } | ||
| let!(:user) { create :user } | ||
|
|
||
| before { expect(controller).to receive(:spree_current_user).and_return(user) } | ||
| before { expect(controller).to receive(:spree_current_user).twice.and_return(user) } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [92/80] |
||
| before { request.env["HTTP_REFERER"] = "product_admin_page" } | ||
|
|
||
| subject do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Spree | ||
| module Core | ||
| module ControllerHelpers | ||
| module Timezone | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| around_action :set_timezone | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Sets the timezone for the current request. | ||
| # | ||
| # Uses the most preferred timezone or falls back to the server default. | ||
| # | ||
| # It respects the server's configured timezone from +config/application.rb+. | ||
| # | ||
| def set_timezone(&action) | ||
| timezone = if timezone_change_needed? | ||
| resolved_timezone || Time.zone.name | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/IndentationWidth: Use 2 (not -9) spaces for indentation. |
||
| else | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/ElseAlignment: Align else with if. |
||
| session[:solidus_timezone] | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EndAlignment: end at 26, 10 is not aligned with if at 22, 21. |
||
| session[:solidus_timezone] = timezone | ||
| Time.use_zone(timezone, &action) | ||
| end | ||
|
|
||
| # Checks if we need to change the timezone or not. | ||
| def timezone_change_needed? | ||
| params[:solidus_timezone].present? || session[:solidus_timezone].blank? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [81/80] |
||
| end | ||
|
|
||
| # Returns the first valid timezone from the priority chain, or nil. | ||
| # | ||
| # The priority order is: | ||
| # | ||
| # * the passed parameter: +params[:solidus_timezone]+ | ||
| # * the user's timezone preference | ||
| # | ||
| def resolved_timezone | ||
| candidates = [params[:solidus_timezone], timezone_from_user].compact | ||
| candidates.detect { |tz| ActiveSupport::TimeZone[tz].present? } | ||
| end | ||
|
|
||
| # Try to get the timezone from user settings. | ||
| def timezone_from_user | ||
| spree_current_user.try(:timezone).presence | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
|
|
||
| RSpec.describe Spree::Core::ControllerHelpers::Timezone, type: :controller do | ||
| controller(ActionController::Base) do | ||
| include Spree::Core::ControllerHelpers::Timezone | ||
|
|
||
| def index | ||
| render plain: Time.zone.name | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :spree_current_user | ||
| end | ||
|
|
||
| let(:original_timezone) { Time.zone.name } | ||
|
|
||
| describe "#set_timezone" do | ||
| context "with params[:solidus_timezone]" do | ||
| it "sets the timezone from the param" do | ||
| get :index, params: {solidus_timezone: "Hawaii"} | ||
| expect(response.body).to eq("Hawaii") | ||
| end | ||
|
|
||
| it "stores the timezone in the session" do | ||
| get :index, params: {solidus_timezone: "Hawaii"} | ||
| expect(session[:solidus_timezone]).to eq("Hawaii") | ||
| end | ||
|
|
||
| it "takes priority over session" do | ||
| get :index, params: {solidus_timezone: "Hawaii"}, session: {solidus_timezone: "Tokyo"} | ||
| expect(response.body).to eq("Hawaii") | ||
| end | ||
| end | ||
|
|
||
| context "with session[:solidus_timezone]" do | ||
| it "uses the timezone from the session" do | ||
| get :index, session: {solidus_timezone: "Tokyo"} | ||
| expect(response.body).to eq("Tokyo") | ||
| end | ||
| end | ||
|
|
||
| context "with spree_current_user timezone" do | ||
| let(:user) { double("User", timezone: "Berlin") } | ||
|
|
||
| before do | ||
| controller.instance_variable_set(:@spree_current_user, user) | ||
| end | ||
|
|
||
| it "uses the user's timezone" do | ||
| get :index | ||
| expect(response.body).to eq("Berlin") | ||
| end | ||
|
|
||
| context "when user does not respond to timezone" do | ||
| let(:user) { double("User") } | ||
|
|
||
| it "falls back to the server default" do | ||
| get :index | ||
| expect(response.body).to eq(original_timezone) | ||
| end | ||
| end | ||
|
|
||
| context "when user's timezone is blank" do | ||
| let(:user) { double("User", timezone: "") } | ||
|
|
||
| it "falls back to the server default" do | ||
| get :index | ||
| expect(response.body).to eq(original_timezone) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "with an invalid timezone" do | ||
| it "falls back to the server default" do | ||
| get :index, params: {solidus_timezone: "Nonexistent/Zone"} | ||
| expect(response.body).to eq(original_timezone) | ||
| end | ||
| end | ||
|
|
||
| context "with no timezone set anywhere" do | ||
| it "uses the server default timezone" do | ||
| get :index | ||
| expect(response.body).to eq(original_timezone) | ||
| end | ||
|
|
||
| it "stores the server default in session" do | ||
| get :index | ||
| expect(session[:solidus_timezone]).to eq(original_timezone) | ||
| end | ||
| end | ||
|
|
||
| it "restores the original timezone after the request" do | ||
| original = Time.zone.name | ||
| get :index, params: {solidus_timezone: "Hawaii"} | ||
| expect(Time.zone.name).to eq(original) | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.