-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: settings page for user preferences - fixes #8
- Loading branch information
Showing
14 changed files
with
139 additions
and
1 deletion.
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,27 @@ | ||
class Users::PreferencesController < ApplicationController | ||
def edit | ||
@user = current_user | ||
end | ||
|
||
def update | ||
@user = current_user | ||
if @user.update(preferences_params) | ||
redirect_to edit_user_preferences_path, notice: t(".updated") | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def options_for_locale | ||
I18n.available_locales.map do |locale| | ||
[I18n.t("languages.#{locale}"), locale] | ||
end | ||
end | ||
helper_method :options_for_locale | ||
|
||
def preferences_params | ||
params.require(:user).permit(:time_zone, :locale) | ||
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,7 @@ | ||
<%= simple_form_for(user, url: user_preferences_path, method: :patch) do |f| %> | ||
<%= f.input :locale, placeholder: t(".locale.placeholder"), required: true, autofocus: true, prompt: t(".locale.prompt"), as: :select, collection: options_for_locale %> | ||
<%= f.input :time_zone, placeholder: t(".timezone.placeholder"), required: true, autofocus: true %> | ||
<div class="mt-4 flex justify-between items-center"> | ||
<%= f.button :submit, t(".submit_button") %> | ||
</div> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<% title t(".title") %> | ||
<%= render(PageLayouts::Settings::Component.new( | ||
title: t(".title"), | ||
description: t(".description") | ||
)) do %> | ||
<%= render "form", user: @user %> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,13 @@ | |
# note that it will be overwritten if you use your own mailer class | ||
# with default "from" parameter. | ||
config.default_from_email = "[email protected]" | ||
|
||
# ==> Locales | ||
# Configure the available locales in the application. | ||
# This is used to validate the locale of the user. | ||
config.available_locales = [:en, :sv] | ||
|
||
# Default locale | ||
# This is used to set the default locale for the application. | ||
config.default_locale = :en | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Permitted locales available for the application | ||
I18n.available_locales = Kiqr::Config.available_locales | ||
|
||
# Set default locale to something other than :en | ||
I18n.default_locale = Kiqr::Config.default_locale |
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
6 changes: 6 additions & 0 deletions
6
db/migrate/20240408210931_add_locale_and_time_zone_to_users.rb
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,6 @@ | ||
class AddLocaleAndTimeZoneToUsers < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :users, :locale, :string, default: "en" | ||
add_column :users, :time_zone, :string, default: "UTC" | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -12,5 +12,14 @@ class Config | |
# note that it will be overwritten if you use your own mailer class | ||
# with default "from" parameter. | ||
config_accessor :default_from_email, default: "[email protected]" | ||
|
||
# ==> Locales | ||
# Configure the available locales in the application. | ||
# This is used to validate the locale of the user. | ||
config_accessor :available_locales, default: [:en] | ||
|
||
# Default locale | ||
# This is used to set the default locale for the application. | ||
config_accessor :default_locale, default: :en | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "test_helper" | ||
|
||
class Users::PreferencesControllerTest < ActionDispatch::IntegrationTest | ||
test "should get edit page" do | ||
user = create(:user) | ||
sign_in(user) | ||
get edit_user_preferences_path | ||
assert_response :success | ||
end | ||
|
||
test "can update user preferences" do | ||
user = create(:user, time_zone: "UTC", locale: "en") | ||
sign_in(user) | ||
|
||
patch user_preferences_path, params: {user: {time_zone: "Stockholm", locale: "sv"}} | ||
assert_redirected_to edit_user_preferences_path | ||
assert_equal "Stockholm", user.reload.time_zone | ||
assert_equal "sv", user.reload.locale | ||
end | ||
end |