-
Notifications
You must be signed in to change notification settings - Fork 344
/
organization_contacts_controller.rb
70 lines (55 loc) · 1.68 KB
/
organization_contacts_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Admin
class OrganizationContactsController < ApplicationController
before_action :authenticate_admin!
layout 'admin'
def edit
@organization = Organization.find(params[:organization_id])
@contact = Contact.find(params[:id])
authorize @contact
end
def update
@contact = Contact.find(params[:id])
@organization = Organization.find(params[:organization_id])
authorize @contact
if @contact.update(contact_params)
flash[:notice] = 'Contact was successfully updated.'
redirect_to [:admin, @organization, @contact]
else
render :edit
end
end
def new
@organization = Organization.find(params[:organization_id])
authorize @organization
@contact = Contact.new
end
def create
@organization = Organization.find(params[:organization_id])
@contact = @organization.contacts.new(contact_params)
authorize @contact
if @contact.save
flash[:notice] = "Contact '#{@contact.name}' was successfully created."
redirect_to admin_organization_path(@organization)
else
render :new
end
end
def destroy
contact = Contact.find(params[:id])
authorize contact
contact.destroy
redirect_to admin_organization_path(contact.organization),
notice: "Contact '#{contact.name}' was successfully deleted."
end
private
def contact_params
params.require(:contact).permit(
:department, :email, :name, :title,
phones_attributes: %i[
country_prefix department extension number number_type
vanity_number id _destroy
]
)
end
end
end