From 20b0f748626f8cd66c75a9439f52e915fee3beeb Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 10 Apr 2020 09:37:23 +0500 Subject: [PATCH] Add attribute Contact#show_in_public --- .../contacts/public_switches_controller.rb | 21 +++++++++++++++++++ .../settings/contact_public_switch_policy.rb | 8 +++++++ app/primitives/contact_public_switch.rb | 9 ++++++++ app/views/settings/contacts/_table.html.erb | 16 ++++++++++++++ config/locales/activerecord/en.yml | 1 + config/locales/activerecord/ru.yml | 1 + config/routes.rb | 4 ++++ ...10043002_add_show_in_public_to_contacts.rb | 7 +++++++ db/structure.sql | 4 +++- spec/primitives/contact_public_switch_spec.rb | 7 +++++++ .../contacts/public_switches/create_spec.rb | 7 +++++++ 11 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 app/controllers/settings/contacts/public_switches_controller.rb create mode 100644 app/policies/settings/contact_public_switch_policy.rb create mode 100644 app/primitives/contact_public_switch.rb create mode 100644 db/migrate/20200410043002_add_show_in_public_to_contacts.rb create mode 100644 spec/primitives/contact_public_switch_spec.rb create mode 100644 spec/requests/settings/contacts/public_switches/create_spec.rb diff --git a/app/controllers/settings/contacts/public_switches_controller.rb b/app/controllers/settings/contacts/public_switches_controller.rb new file mode 100644 index 0000000..d497430 --- /dev/null +++ b/app/controllers/settings/contacts/public_switches_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class Settings::Contacts::PublicSwitchesController < ApplicationController + before_action :set_contact + + # POST /settings/contacts/:contact_id/public_switch + def create + authorize [:settings, ContactPublicSwitch.new(@contact)] + + @contact.show_in_public = !@contact.show_in_public + @contact.save! + + redirect_to settings_contacts_url + end + +private + + def set_contact + @contact = current_account&.contact_list&.contacts&.find params[:contact_id] + end +end diff --git a/app/policies/settings/contact_public_switch_policy.rb b/app/policies/settings/contact_public_switch_policy.rb new file mode 100644 index 0000000..033421b --- /dev/null +++ b/app/policies/settings/contact_public_switch_policy.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class Settings::ContactPublicSwitchPolicy < ApplicationPolicy + def create? + account && + record.contact.contact_list.account == account + end +end diff --git a/app/primitives/contact_public_switch.rb b/app/primitives/contact_public_switch.rb new file mode 100644 index 0000000..684ff6f --- /dev/null +++ b/app/primitives/contact_public_switch.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class ContactPublicSwitch + attr_reader :contact + + def initialize(contact) + @contact = contact or raise + end +end diff --git a/app/views/settings/contacts/_table.html.erb b/app/views/settings/contacts/_table.html.erb index 42bd209..0f3227c 100644 --- a/app/views/settings/contacts/_table.html.erb +++ b/app/views/settings/contacts/_table.html.erb @@ -10,6 +10,9 @@ <%= Contact.human_attribute_name :send_security_notifications %> + + <%= Contact.human_attribute_name :show_in_public %> + @@ -39,6 +42,19 @@ <% end %> <% end %> + + <% if policy([ + :settings, + ContactPublicSwitch.new(contact), + ]).create? %> + <%= link_to( + settings_contact_public_switch_path(contact), + method: :post, + ) do %> + <%= bool_badge contact.show_in_public %> + <% end %> + <% end %> + <% if policy([:settings, contact]).destroy? %> <%= link_to( diff --git a/config/locales/activerecord/en.yml b/config/locales/activerecord/en.yml index eee03b4..a0e2521 100644 --- a/config/locales/activerecord/en.yml +++ b/config/locales/activerecord/en.yml @@ -77,6 +77,7 @@ en: contact_network: Contact network value: Value send_security_notifications: Security notifications + show_in_public: Public link: Link contact_network: id: ID diff --git a/config/locales/activerecord/ru.yml b/config/locales/activerecord/ru.yml index 9a0022d..4b6f441 100644 --- a/config/locales/activerecord/ru.yml +++ b/config/locales/activerecord/ru.yml @@ -64,6 +64,7 @@ ru: contact_network: Сеть контактов value: Значение send_security_notifications: Уведомления безопасности + show_in_public: Публичный link: Ссылка contact_network: id: ID diff --git a/config/routes.rb b/config/routes.rb index 723d1f9..33af9ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -40,6 +40,10 @@ Rails.application.routes.draw do resource :security_notification_switch, controller: 'contacts/security_notification_switches', only: :create + + resource :public_switch, + controller: 'contacts/public_switches', + only: :create end end diff --git a/db/migrate/20200410043002_add_show_in_public_to_contacts.rb b/db/migrate/20200410043002_add_show_in_public_to_contacts.rb new file mode 100644 index 0000000..3d9ea42 --- /dev/null +++ b/db/migrate/20200410043002_add_show_in_public_to_contacts.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddShowInPublicToContacts < ActiveRecord::Migration[6.0] + def change + add_column :contacts, :show_in_public, :boolean, null: false, default: false + end +end diff --git a/db/structure.sql b/db/structure.sql index 98125a8..a8c1a98 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -682,6 +682,7 @@ CREATE TABLE public.contacts ( contact_network_id bigint NOT NULL, value character varying NOT NULL, send_security_notifications boolean DEFAULT false NOT NULL, + show_in_public boolean DEFAULT false NOT NULL, CONSTRAINT value CHECK (public.is_good_small_text((value)::text)) ); @@ -1991,6 +1992,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20191021060000'), ('20191021061920'), ('20200408085219'), -('20200410021628'); +('20200410021628'), +('20200410043002'); diff --git a/spec/primitives/contact_public_switch_spec.rb b/spec/primitives/contact_public_switch_spec.rb new file mode 100644 index 0000000..1e5caab --- /dev/null +++ b/spec/primitives/contact_public_switch_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ContactPublicSwitch, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/settings/contacts/public_switches/create_spec.rb b/spec/requests/settings/contacts/public_switches/create_spec.rb new file mode 100644 index 0000000..34a3610 --- /dev/null +++ b/spec/requests/settings/contacts/public_switches/create_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'POST /settings/contacts/:contact_id/public_switch' do + pending "add some examples to (or delete) #{__FILE__}" +end