Add attribute Account#locale
This commit is contained in:
parent
768e345423
commit
1a2281c88b
11 changed files with 63 additions and 3 deletions
|
@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base
|
|||
protect_from_forgery with: :exception, prepend: true, unless: :json_request?
|
||||
|
||||
before_action :set_raven_context
|
||||
before_action :set_locale
|
||||
|
||||
after_action :verify_authorized, except: :index
|
||||
after_action :verify_policy_scoped, only: :index
|
||||
|
@ -32,6 +33,10 @@ private
|
|||
Raven.extra_context params: params.to_unsafe_h, url: request.url
|
||||
end
|
||||
|
||||
def set_locale
|
||||
I18n.locale = current_account&.locale || I18n.default_locale
|
||||
end
|
||||
|
||||
def json_request?
|
||||
request.format.json?
|
||||
end
|
||||
|
|
|
@ -13,6 +13,15 @@ module ApplicationHelper
|
|||
[*negative_timezones_collection, *positive_timezones_collection].freeze
|
||||
end
|
||||
|
||||
def locales_collection
|
||||
Account.locales.keys.map(&:to_sym).map do |item|
|
||||
[
|
||||
translate_enum(:locale, item).freeze,
|
||||
item,
|
||||
].freeze
|
||||
end.freeze
|
||||
end
|
||||
|
||||
def positive_timezones_collection
|
||||
0.upto(11).flat_map do |n|
|
||||
s = n.to_s.rjust(2, '0')
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Account < ApplicationRecord
|
||||
pg_enum :locale, %i[en ru]
|
||||
|
||||
################
|
||||
# Associations #
|
||||
################
|
||||
|
@ -42,6 +44,8 @@ class Account < ApplicationRecord
|
|||
|
||||
validates :timezone, timezone: true
|
||||
|
||||
validates :locale, presence: true
|
||||
|
||||
validate :contact_list_corresponds_person
|
||||
validate :person_corresponds_contact_list
|
||||
|
||||
|
|
|
@ -6,6 +6,6 @@ class Settings::AppearancePolicy < ApplicationPolicy
|
|||
end
|
||||
|
||||
def permitted_attributes_for_update
|
||||
%i[timezone]
|
||||
%i[timezone locale].freeze
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,15 @@
|
|||
<%= simple_form_for account, url: settings_appearance_path do |f| %>
|
||||
<%= f.error_notification %>
|
||||
|
||||
<%= f.input :timezone, as: :select, collection: timezones_collection %>
|
||||
<%= f.input :timezone,
|
||||
as: :select,
|
||||
include_blank: false,
|
||||
collection: timezones_collection %>
|
||||
|
||||
<%= f.input :locale,
|
||||
as: :select,
|
||||
include_blank: false,
|
||||
collection: locales_collection %>
|
||||
|
||||
<%= f.button :submit, translate(:save) %>
|
||||
<% end %>
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
en:
|
||||
enums:
|
||||
locale:
|
||||
en: English
|
||||
ru: Russian (Русский)
|
||||
sex:
|
||||
male: Male
|
||||
female: Female
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
ru:
|
||||
enums:
|
||||
locale:
|
||||
en: Английский (English)
|
||||
ru: Русский
|
||||
sex:
|
||||
male: Мужской
|
||||
female: Женский
|
||||
|
|
11
db/migrate/20200408085219_add_locale_to_accounts.rb
Normal file
11
db/migrate/20200408085219_add_locale_to_accounts.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddLocaleToAccounts < ActiveRecord::Migration[6.0]
|
||||
include Partynest::Migration
|
||||
|
||||
def change
|
||||
add_enum :locale, %i[en ru]
|
||||
|
||||
add_column :accounts, :locale, :locale, null: false, default: 'ru'
|
||||
end
|
||||
end
|
|
@ -23,6 +23,16 @@ CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
|
|||
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
|
||||
|
||||
|
||||
--
|
||||
-- Name: locale; Type: TYPE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TYPE public.locale AS ENUM (
|
||||
'en',
|
||||
'ru'
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: person_comment_origin; Type: TYPE; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -489,6 +499,7 @@ CREATE TABLE public.accounts (
|
|||
timezone interval DEFAULT '03:00:00'::interval NOT NULL,
|
||||
person_id bigint,
|
||||
contact_list_id bigint NOT NULL,
|
||||
locale public.locale DEFAULT 'ru'::public.locale NOT NULL,
|
||||
CONSTRAINT biography CHECK (((biography IS NULL) OR public.is_good_big_text(biography))),
|
||||
CONSTRAINT nickname CHECK (public.is_nickname((nickname)::text)),
|
||||
CONSTRAINT public_name CHECK (((public_name IS NULL) OR public.is_good_small_text((public_name)::text)))
|
||||
|
@ -1977,6 +1988,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20191002002101'),
|
||||
('20191002170727'),
|
||||
('20191021060000'),
|
||||
('20191021061920');
|
||||
('20191021061920'),
|
||||
('20200408085219');
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ FactoryBot.define do
|
|||
biography { Faker::Lorem.paragraph }
|
||||
|
||||
timezone { "#{[nil, :-].sample}#{rand(1..11).to_s.rjust(2, '0')}:00:00" }
|
||||
locale { :ru }
|
||||
end
|
||||
|
||||
factory :usual_account, parent: :initial_account do
|
||||
|
|
|
@ -97,6 +97,10 @@ RSpec.describe Account do
|
|||
it { is_expected.not_to allow_value '+01:00:00' }
|
||||
end
|
||||
|
||||
describe '#locale' do
|
||||
it { is_expected.to validate_presence_of :locale }
|
||||
end
|
||||
|
||||
describe '#nickname' do
|
||||
def allow_value(*)
|
||||
super.for :nickname
|
||||
|
|
Reference in a new issue