From 5c9a5f319cf6bae3c9c64c0d59b86d349e1f44e6 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 4 Sep 2019 23:51:23 +0500 Subject: [PATCH] Add action Settings::Contacts::SecurityNotificationSwitches#create --- ...curity_notification_switches_controller.rb | 22 +++++++++++++++++++ ...act_security_notification_switch_policy.rb | 9 ++++++++ .../contact_security_notification_switch.rb | 9 ++++++++ app/views/settings/contacts/_table.html.erb | 14 +++++++++++- config/routes.rb | 7 +++++- ...ecurity_notification_switch_policy_spec.rb | 7 ++++++ .../create_spec.rb | 9 ++++++++ 7 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 app/controllers/settings/contacts/security_notification_switches_controller.rb create mode 100644 app/policies/settings/contact_security_notification_switch_policy.rb create mode 100644 app/primitives/contact_security_notification_switch.rb create mode 100644 spec/policies/settings/contact_security_notification_switch_policy_spec.rb create mode 100644 spec/requests/settings/contacts/security_notification_switches/create_spec.rb diff --git a/app/controllers/settings/contacts/security_notification_switches_controller.rb b/app/controllers/settings/contacts/security_notification_switches_controller.rb new file mode 100644 index 0000000..c4b8546 --- /dev/null +++ b/app/controllers/settings/contacts/security_notification_switches_controller.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class Settings::Contacts::SecurityNotificationSwitchesController \ + < ApplicationController + before_action :set_contact + + # POST /settings/contacts/:contact_id/security_notification_switch + def create + authorize [:settings, ContactSecurityNotificationSwitch.new(@contact)] + + @contact.send_security_notifications = !@contact.send_security_notifications + @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_security_notification_switch_policy.rb b/app/policies/settings/contact_security_notification_switch_policy.rb new file mode 100644 index 0000000..d7a4578 --- /dev/null +++ b/app/policies/settings/contact_security_notification_switch_policy.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class Settings::ContactSecurityNotificationSwitchPolicy < ApplicationPolicy + def create? + account && + record.contact.contact_list.account == account && + record.contact.contact_network_communicable? + end +end diff --git a/app/primitives/contact_security_notification_switch.rb b/app/primitives/contact_security_notification_switch.rb new file mode 100644 index 0000000..048e28a --- /dev/null +++ b/app/primitives/contact_security_notification_switch.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class ContactSecurityNotificationSwitch + 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 b732fb5..622449d 100644 --- a/app/views/settings/contacts/_table.html.erb +++ b/app/views/settings/contacts/_table.html.erb @@ -19,7 +19,19 @@ <%= contact.contact_network.name %> <%= truncate contact.value %> - <%= bool_badge contact.send_security_notifications %> + + <% if policy([ + :settings, + ContactSecurityNotificationSwitch.new(contact), + ]).create? %> + <%= link_to( + settings_contact_security_notification_switch_path(contact), + method: :post, + ) do %> + <%= bool_badge contact.send_security_notifications %> + <% end %> + <% end %> + <% if policy([:settings, contact]).destroy? %> <%= link_to( diff --git a/config/routes.rb b/config/routes.rb index e35a224..ee43e8a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -34,8 +34,13 @@ Rails.application.routes.draw do resource :profile, only: %i[edit update] resource :appearance, only: %i[edit update] resource :person, only: %i[show new] - resources :contacts, only: %i[index create destroy] resources :sessions, only: :index + + resources :contacts, only: %i[index create destroy] do + resource :security_notification_switch, + controller: 'contacts/security_notification_switches', + only: :create + end end ######################### diff --git a/spec/policies/settings/contact_security_notification_switch_policy_spec.rb b/spec/policies/settings/contact_security_notification_switch_policy_spec.rb new file mode 100644 index 0000000..7617c65 --- /dev/null +++ b/spec/policies/settings/contact_security_notification_switch_policy_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Settings::ContactSecurityNotificationSwitchPolicy do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/settings/contacts/security_notification_switches/create_spec.rb b/spec/requests/settings/contacts/security_notification_switches/create_spec.rb new file mode 100644 index 0000000..9fb925b --- /dev/null +++ b/spec/requests/settings/contacts/security_notification_switches/create_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe( + 'POST /settings/contacts/:contact_id/security_notification_switch', +) do + pending "add some examples to (or delete) #{__FILE__}" +end