1
0
Fork 0

Add attribute Account#locale

This commit is contained in:
Alex Kotov 2020-04-08 13:56:09 +05:00
parent 768e345423
commit 1a2281c88b
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
11 changed files with 63 additions and 3 deletions

View file

@ -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

View file

@ -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')

View file

@ -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

View file

@ -6,6 +6,6 @@ class Settings::AppearancePolicy < ApplicationPolicy
end
def permitted_attributes_for_update
%i[timezone]
%i[timezone locale].freeze
end
end

View file

@ -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 %>

View file

@ -1,5 +1,8 @@
en:
enums:
locale:
en: English
ru: Russian (Русский)
sex:
male: Male
female: Female

View file

@ -1,5 +1,8 @@
ru:
enums:
locale:
en: Английский (English)
ru: Русский
sex:
male: Мужской
female: Женский

View 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

View file

@ -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');

View file

@ -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

View file

@ -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