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

add time_zone configutation to users #11

Draft
wants to merge 11 commits into
base: release/0.26-stable
Choose a base branch
from
1 change: 1 addition & 0 deletions decidim-core/app/commands/decidim/update_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def update_personal_data
@user.email = @form.email
@user.personal_url = @form.personal_url
@user.about = @form.about
@user.time_zone = @form.time_zone
end

def update_avatar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def use_organization_time_zone(&action)
#
# Returns a String.
def organization_time_zone
@organization_time_zone ||= current_organization.time_zone
@organization_time_zone ||= current_user&.time_zone.presence || current_organization.time_zone
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions decidim-core/app/forms/decidim/account_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AccountForm < Form
attribute :remove_avatar, Boolean, default: false
attribute :personal_url
attribute :about
attribute :time_zone

validates :name, presence: true
validates :email, presence: true, 'valid_email_2/email': { disposable: true }
Expand All @@ -28,6 +29,7 @@ class AccountForm < Form
validates :password, password: { name: :name, email: :email, username: :nickname }, if: -> { password.present? }
validates :password_confirmation, presence: true, if: :password_present
validates :avatar, passthru: { to: Decidim::User }
validates :time_zone, time_zone: true, if: -> { time_zone.present? }

validate :unique_email
validate :unique_nickname
Expand Down
1 change: 1 addition & 0 deletions decidim-core/app/models/decidim/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def self.all
validates :tos_agreement, acceptance: true, allow_nil: false, on: :create
validates :tos_agreement, acceptance: true, if: :user_invited?
validates :email, :nickname, uniqueness: { scope: :organization }, unless: -> { deleted? || managed? || nickname.blank? }
validates :time_zone, time_zone: true, if: -> { time_zone.present? }

validate :all_roles_are_valid

Expand Down
3 changes: 3 additions & 0 deletions decidim-core/app/views/decidim/account/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
) %>
<p class="help-text"><%= t(".available_locales_helper") %></p>

<%= f.time_zone_select :time_zone, nil, default: organization_time_zone %>
<p class="help-text"><%= t(".time_zone_helper") %></p>

<% if @account.errors[:password].any? || @account.errors[:password_confirmation].any? %>
<%= render partial: "password_fields", locals: { form: f } %>
<% else %>
Expand Down
1 change: 1 addition & 0 deletions decidim-core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ en:
show:
available_locales_helper: Choose the language you want to use to browse and receive notifications in Decidim
change_password: Change password
time_zone_helper: Use your personal time zone to display dates in your local time when you are logged in
update_account: Update account
update:
error: There was a problem updating your account.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddTimeZoneToUsers < ActiveRecord::Migration[6.0]
def change
add_column :decidim_users, :time_zone, :string
end
end
31 changes: 29 additions & 2 deletions decidim-core/spec/commands/decidim/update_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Decidim
describe UpdateAccount do
let(:command) { described_class.new(user, form) }
let(:user) { create(:user, :confirmed) }
let(:time_zone) { "UTC" }
let(:data) do
{
name: user.name,
Expand All @@ -17,7 +18,8 @@ module Decidim
remove_avatar: nil,
personal_url: "https://example.org",
about: "This is a description of me",
locale: "es"
locale: "es",
time_zone: time_zone
}
end

Expand All @@ -32,7 +34,8 @@ module Decidim
remove_avatar: data[:remove_avatar],
personal_url: data[:personal_url],
about: data[:about],
locale: data[:locale]
locale: data[:locale],
time_zone: data[:time_zone]
).with_context(current_organization: user.organization, current_user: user)
end

Expand All @@ -47,6 +50,14 @@ module Decidim
expect { command.call }.to broadcast(:invalid)
expect(user.reload.name).to eq(old_name)
end

context "when timezone is invalid" do
let(:time_zone) { "giberish" }

it "returns invalid" do
expect { command.call }.to broadcast(:invalid)
end
end
end

context "when valid" do
Expand Down Expand Up @@ -77,6 +88,22 @@ module Decidim
expect(user.reload.locale).to eq("es")
end

context "when timezone is defined" do
it "updates the time zone" do
expect { command.call }.to broadcast(:ok)
expect(user.reload.time_zone).to eq("UTC")
end
end

context "when timezone is not defined" do
let(:time_zone) { "" }

it "updates the time zone" do
expect { command.call }.to broadcast(:ok)
expect(user.reload.time_zone).to eq("")
end
end

describe "updating the email" do
before do
form.email = "[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ module Decidim
let(:utc_time_zone) { "UTC" }
let(:alt_time_zone) { "Hawaii" }
let(:organization) { create(:organization, time_zone: time_zone) }
let(:user) { create :user, :confirmed, organization: organization, time_zone: user_time_zone }
let(:user_time_zone) { "" }

before do
request.env["decidim.current_organization"] = organization
allow(controller).to receive(:current_user) { user }
end

context "when time zone is UTC" do
Expand Down Expand Up @@ -85,5 +88,42 @@ module Decidim
end
end
end

context "when time zone is defined by the user" do
let(:time_zone) { utc_time_zone }
let(:user_time_zone) { "London" }

it "controller uses London" do
expect(controller.organization_time_zone).to eq("London")
end

it "Time uses UTC zone within the controller scope" do
controller.use_organization_time_zone do
expect(Time.zone.name).to eq("London")
end
end

it "Time uses Rails timezone outside the controller scope" do
expect(Time.zone.name).to eq("UTC")
end
end

context "when user's time zone in not present" do
let(:time_zone) { utc_time_zone }
let(:user_time_zone) { "" }

it "controller uses time zone of organization" do
expect(controller.organization_time_zone).to eq(utc_time_zone)
end
end

context "when user is not present" do
let(:time_zone) { utc_time_zone }
let(:user) { nil }

it "controller uses time zone of organization" do
expect(controller.organization_time_zone).to eq(utc_time_zone)
end
end
end
end
22 changes: 21 additions & 1 deletion decidim-core/spec/forms/account_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module Decidim
remove_avatar: remove_avatar,
personal_url: personal_url,
about: about,
locale: "es"
locale: "es",
time_zone: time_zone
).with_context(
current_organization: organization,
current_user: user
Expand All @@ -34,6 +35,7 @@ module Decidim
let(:remove_avatar) { false }
let(:personal_url) { "http://example.org" }
let(:about) { "This is a description about me" }
let(:time_zone) { "UTC" }

context "with correct data" do
it "is valid" do
Expand Down Expand Up @@ -154,5 +156,23 @@ module Decidim
end
end
end

describe "time_zone" do
context "when an empty time_zone" do
let(:time_zone) { "" }

it "is invalid" do
expect(subject).to be_valid
end
end

context "when time_zone has more 255 chars" do
let(:time_zone) { [*("A".."Z")].sample(256).join }

it "is invalid" do
expect(subject).not_to be_valid
end
end
end
end
end