1
0
Fork 0

Add action Settings::Contacts::SecurityNotificationSwitches#create

This commit is contained in:
Alex Kotov 2019-09-04 23:51:23 +05:00
parent ac78717d6b
commit 5c9a5f319c
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
7 changed files with 75 additions and 2 deletions

View file

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

View file

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

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
class ContactSecurityNotificationSwitch
attr_reader :contact
def initialize(contact)
@contact = contact or raise
end
end

View file

@ -19,7 +19,19 @@
<tr>
<td><%= contact.contact_network.name %></td>
<td><%= truncate contact.value %></td>
<td><%= bool_badge contact.send_security_notifications %></td>
<td>
<% 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 %>
</td>
<td>
<% if policy([:settings, contact]).destroy? %>
<%= link_to(

View file

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

View file

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

View file

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