From decd0da0f3f48a9d64c9e73c2fcb7b092d8d659c Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 10 Apr 2020 07:15:48 +0500 Subject: [PATCH] Add contact network links --- app/models/contact.rb | 8 +++++ app/models/contact_network.rb | 6 ++++ app/views/settings/contacts/_table.html.erb | 9 ++++- .../staffs/contact_networks/_table.html.erb | 4 +++ config/locales/activerecord/en.yml | 2 ++ config/locales/activerecord/ru.yml | 2 ++ db/data/contact_networks.csv | 36 +++++++++---------- ...0410021628_add_link_to_contact_networks.rb | 7 ++++ db/seeds.rb | 4 ++- db/structure.sql | 4 ++- factories/contact_networks.rb | 3 ++ spec/models/contact_network_spec.rb | 17 +++++++++ spec/models/contact_spec.rb | 26 ++++++++++++++ 13 files changed, 107 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20200410021628_add_link_to_contact_networks.rb diff --git a/app/models/contact.rb b/app/models/contact.rb index edb5b1f..9c4f687 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -31,6 +31,14 @@ class Contact < ApplicationRecord validates :send_security_notifications, inclusion: { in: [false], unless: :contact_network_communicable? } + ########### + # Methods # + ########### + + def link + contact_network&.link&.sub('$$', value).freeze + end + private def turn_blanks_into_nils diff --git a/app/models/contact_network.rb b/app/models/contact_network.rb index 5d0a331..967116d 100644 --- a/app/models/contact_network.rb +++ b/app/models/contact_network.rb @@ -15,6 +15,12 @@ class ContactNetwork < ApplicationRecord validates :name, good_small_text: true, uniqueness: true + validates :link, + allow_nil: true, + allow_blank: false, + presence: true, + format: { with: /\A[^\s]+\z/ } + ########### # Methods # ########### diff --git a/app/views/settings/contacts/_table.html.erb b/app/views/settings/contacts/_table.html.erb index 622449d..42bd209 100644 --- a/app/views/settings/contacts/_table.html.erb +++ b/app/views/settings/contacts/_table.html.erb @@ -18,7 +18,14 @@ <% contacts.each do |contact| %> <%= contact.contact_network.name %> - <%= truncate contact.value %> + + <% if contact.link.nil? %> + <%= truncate contact.value %> + <% else %> + <%= link_to truncate(contact.value), contact.link, target: :_blank %> + + <% end %> + <% if policy([ :settings, diff --git a/app/views/staffs/contact_networks/_table.html.erb b/app/views/staffs/contact_networks/_table.html.erb index a28b3be..d3a1597 100644 --- a/app/views/staffs/contact_networks/_table.html.erb +++ b/app/views/staffs/contact_networks/_table.html.erb @@ -7,6 +7,9 @@ <%= ContactNetwork.human_attribute_name :name %> + + <%= ContactNetwork.human_attribute_name :link %> + <%= ContactNetwork.human_attribute_name :contacts %> @@ -19,6 +22,7 @@ <%= contact_network.codename %> <%= contact_network.name %> + <%= contact_network.link %> <%= contact_network.contacts.count %> diff --git a/config/locales/activerecord/en.yml b/config/locales/activerecord/en.yml index 800c9d1..eee03b4 100644 --- a/config/locales/activerecord/en.yml +++ b/config/locales/activerecord/en.yml @@ -77,11 +77,13 @@ en: contact_network: Contact network value: Value send_security_notifications: Security notifications + link: Link contact_network: id: ID codename: Codename contacts: Contacts name: Name + link: Link federal_subject: id: ID english_name: Name diff --git a/config/locales/activerecord/ru.yml b/config/locales/activerecord/ru.yml index 717ccab..9a0022d 100644 --- a/config/locales/activerecord/ru.yml +++ b/config/locales/activerecord/ru.yml @@ -64,11 +64,13 @@ ru: contact_network: Сеть контактов value: Значение send_security_notifications: Уведомления безопасности + link: Ссылка contact_network: id: ID codename: Кодовое имя contacts: Контакты name: Название + link: Ссылка federal_subject: id: ID english_name: Название diff --git a/db/data/contact_networks.csv b/db/data/contact_networks.csv index 8736cb8..a4d73f9 100644 --- a/db/data/contact_networks.csv +++ b/db/data/contact_networks.csv @@ -1,18 +1,18 @@ -1 | phone | Phone -2 | email | Email -3 | telegram_phone | Telegram (phone) -4 | telegram_username | Telegram (username) -5 | telegram_id | Telegram (id) -6 | twitter | Twitter -7 | facebook | Facebook -8 | vkontakte | VK -9 | skype | Skype -10 | xmpp | XMPP -11 | pgp | PGP -12 | google_plus | Google+ -13 | www | www -14 | odnoklassniki | Odnoklassniki -15 | ostatus | OStatus -16 | livejournal | LiveJournal -41 | trello_id | Trello (id) -61 | trello_username | Trello (username) +1 | phone | Phone | | +2 | email | Email | mailto:$$ | +3 | telegram_phone | Telegram (phone) | | +4 | telegram_username | Telegram (username) | https://t.me/$$ | +5 | telegram_id | Telegram (id) | | +6 | twitter | Twitter | https://twitter.com/$$ | +7 | facebook | Facebook | https://fb.com/$$ | +8 | vkontakte | VK | https://vk.com/$$ | +9 | skype | Skype | | +10 | xmpp | XMPP | | +11 | pgp | PGP | | +12 | google_plus | Google+ | | +13 | www | www | $$ | +14 | odnoklassniki | Odnoklassniki | | +15 | ostatus | OStatus | | +16 | livejournal | LiveJournal | | +41 | trello_id | Trello (id) | | +61 | trello_username | Trello (username) | https://trello.com/$$ | diff --git a/db/migrate/20200410021628_add_link_to_contact_networks.rb b/db/migrate/20200410021628_add_link_to_contact_networks.rb new file mode 100644 index 0000000..10b7155 --- /dev/null +++ b/db/migrate/20200410021628_add_link_to_contact_networks.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddLinkToContactNetworks < ActiveRecord::Migration[6.0] + def change + add_column :contact_networks, :link, :string + end +end diff --git a/db/seeds.rb b/db/seeds.rb index 0fab1b7..224a753 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -29,14 +29,16 @@ do |(id, english_name, native_name, number, timezone, centre)| end csv_foreach :contact_networks \ -do |(id, codename, name)| +do |(id, codename, name, link)| id = Integer(id.strip) codename.strip! name.strip! + link = link.strip.presence ContactNetwork.where(id: id).first_or_create! do |new_contact_network| new_contact_network.codename = codename new_contact_network.name = name + new_contact_network.link = link end end diff --git a/db/structure.sql b/db/structure.sql index f09973a..98125a8 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -645,6 +645,7 @@ CREATE TABLE public.contact_networks ( updated_at timestamp(6) without time zone NOT NULL, codename character varying NOT NULL, name character varying NOT NULL, + link character varying, CONSTRAINT codename CHECK (public.is_codename((codename)::text)), CONSTRAINT name CHECK (public.is_good_small_text((name)::text)) ); @@ -1989,6 +1990,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20191002170727'), ('20191021060000'), ('20191021061920'), -('20200408085219'); +('20200408085219'), +('20200410021628'); diff --git a/factories/contact_networks.rb b/factories/contact_networks.rb index 5e9508f..077f744 100644 --- a/factories/contact_networks.rb +++ b/factories/contact_networks.rb @@ -4,6 +4,7 @@ FactoryBot.define do factory :contact_network do codename { Faker::Internet.unique.username 3..36, %w[_] } name { Faker::Company.unique.name } + link { nil } end factory :email_contact_network, class: ContactNetwork do @@ -13,6 +14,7 @@ FactoryBot.define do codename { 'email' } name { 'Email' } + link { 'mailto:$$' } end factory :phone_contact_network, class: ContactNetwork do @@ -22,5 +24,6 @@ FactoryBot.define do codename { 'phone' } name { 'Phone' } + link { nil } end end diff --git a/spec/models/contact_network_spec.rb b/spec/models/contact_network_spec.rb index e6bb093..ce7873f 100644 --- a/spec/models/contact_network_spec.rb +++ b/spec/models/contact_network_spec.rb @@ -77,6 +77,23 @@ RSpec.describe ContactNetwork do it { is_expected.not_to allow_value "Foo\n" } end + describe '#link' do + def allow_value(*) + super.for :link + end + + it { is_expected.to allow_value nil } + it { is_expected.not_to allow_value '' } + it { is_expected.not_to allow_value ' ' } + + it { is_expected.to allow_value 'mailto:$$' } + it { is_expected.to allow_value 'https://t.me/$$' } + + it { is_expected.not_to allow_value ' mailto:$$' } + it { is_expected.not_to allow_value 'mailto:$$ ' } + it { is_expected.not_to allow_value ' mailto:$$ ' } + end + describe '#communicable?' do specify do expect(subject.communicable?).to equal false diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 9962957..f8b24e6 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -114,4 +114,30 @@ RSpec.describe Contact do end end end + + describe '#link' do + context 'for email' do + subject { create :email_contact } + + specify do + expect(subject.link).to be_instance_of String + end + + specify do + expect(subject.link).to be_frozen + end + + specify do + expect(subject.link).to eq "mailto:#{subject.value}" + end + end + + context 'for phone' do + subject { create :phone_contact } + + specify do + expect(subject.link).to equal nil + end + end + end end