diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6a3bd7a..d39d6c0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index e757d78..66a9c2d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) } diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 4ab2468..43fc77b 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -24,6 +24,8 @@ <%= f.input :password_confirmation, autocomplete: "off" %> + <%= 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" %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 69a0e09..0c5150d 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -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" %> diff --git a/db/migrate/20180613055303_add_locale_to_users.rb b/db/migrate/20180613055303_add_locale_to_users.rb new file mode 100644 index 0000000..b76376e --- /dev/null +++ b/db/migrate/20180613055303_add_locale_to_users.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 31dac43..88f61a4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/test/factories/users.rb b/test/factories/users.rb index d73cba4..4566543 100644 --- a/test/factories/users.rb +++ b/test/factories/users.rb @@ -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 diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 11697d6..05d1509 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -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