21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-04-29 15:30:49 +02:00

Locale can be selected at signup or changed in profile form

This commit is contained in:
Jérémy Lecour 2018-06-13 08:14:33 +02:00
parent 5324957174
commit 1fb78a55f9
8 changed files with 24 additions and 4 deletions

View file

@ -3,16 +3,20 @@ class ApplicationController < ActionController::Base
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
before_action :configure_devise_parameters, if: :devise_controller?
before_action :set_locale
protected
def configure_devise_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:tos_accepted])
devise_parameter_sanitizer.permit(:account_update, keys: [:notifications_enabled])
devise_parameter_sanitizer.permit(:sign_up, keys: [:tos_accepted, :locale])
devise_parameter_sanitizer.permit(:account_update, keys: [:notifications_enabled, :locale])
end
def user_not_authorized
flash[:alert] = I18n.t("user_not_authorized", scope: :flashes)
redirect_to(request.referrer || root_path)
end
def set_locale
I18n.locale = current_user.try(:locale) || I18n.default_locale
end
end

View file

@ -12,6 +12,7 @@
# encrypted_password :string(255) default(""), not null
# last_sign_in_at :datetime
# last_sign_in_ip :string(255)
# locale :string(5) default("en"), not null
# notifications_enabled :boolean default(TRUE), not null
# remember_created_at :datetime
# reset_password_sent_at :datetime
@ -37,6 +38,7 @@ class User < ApplicationRecord
has_many :checks
validates :tos_accepted, acceptance: true
validates :locale, inclusion: { in: I18n.available_locales }
scope :notifications_disabled, -> { where(notifications_enabled: false) }

View file

@ -24,6 +24,8 @@
<%= f.input :password_confirmation, autocomplete: "off" %>
</div>
<%= f.input :locale, label: t('.locale'), collection: I18n.available_locales, selected: resource.locale %>
<%= f.input :notifications_enabled %>
<%= f.button :submit, t('.update'), class: "btn-primary" %>

View file

@ -9,6 +9,8 @@
<%= f.input :password_confirmation, autocomplete: "off" %>
<%= f.input :locale, label: t('.locale'), collection: I18n.available_locales, selected: I18n.default_locale %>
<%= f.input :tos_accepted, label: t('.tos_acceptance_html') %>
<%= f.button :submit, t('.sign_up'), class: "btn-primary" %>

View file

@ -0,0 +1,5 @@
class AddLocaleToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :locale, :string, null: false, default: "en", limit: 5
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_06_02_154319) do
ActiveRecord::Schema.define(version: 2018_06_13_055303) do
create_table "check_logs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.bigint "check_id"
@ -72,6 +72,7 @@ ActiveRecord::Schema.define(version: 2018_06_02_154319) do
t.datetime "updated_at", null: false
t.boolean "tos_accepted", default: false, null: false
t.boolean "notifications_enabled", default: true, null: false
t.string "locale", limit: 5, default: "en", null: false
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

View file

@ -12,6 +12,7 @@
# encrypted_password :string(255) default(""), not null
# last_sign_in_at :datetime
# last_sign_in_ip :string(255)
# locale :string(5) default("en"), not null
# notifications_enabled :boolean default(TRUE), not null
# remember_created_at :datetime
# reset_password_sent_at :datetime
@ -28,6 +29,7 @@
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
#
require "securerandom"
FactoryBot.define do
@ -36,6 +38,7 @@ FactoryBot.define do
password "password"
confirmed_at Time.new(2018, 4, 1, 12, 0, 0, "+02:00")
notifications_enabled true
locale "en"
tos_accepted true
end
end

View file

@ -12,6 +12,7 @@
# encrypted_password :string(255) default(""), not null
# last_sign_in_at :datetime
# last_sign_in_ip :string(255)
# locale :string(5) default("en"), not null
# notifications_enabled :boolean default(TRUE), not null
# remember_created_at :datetime
# reset_password_sent_at :datetime