Skip to content

Commit

Permalink
Add admin & API support for Organization Contacts.
Browse files Browse the repository at this point in the history
  • Loading branch information
monfresh committed Nov 7, 2014
1 parent 6d791a4 commit 6d13382
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 73 deletions.
36 changes: 12 additions & 24 deletions app/controllers/admin/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ def update
@contact = Contact.find(params[:id])
@location = Location.find(params[:location_id])

respond_to do |format|
if @contact.update(params[:contact])
format.html do
redirect_to [:admin, @location, @contact],
notice: 'Contact was successfully updated.'
end
else
format.html { render :edit }
end
if @contact.update(params[:contact])
flash[:notice] = 'Contact was successfully updated.'
redirect_to [:admin, @location, @contact]
else
render :edit
end
end

Expand All @@ -46,27 +42,19 @@ def create
@location = Location.find(params[:location_id])
@contact = @location.contacts.new(params[:contact])

respond_to do |format|
if @contact.save
format.html do
redirect_to admin_location_path(@location),
notice: "Contact '#{@contact.name}' was successfully created."
end
else
format.html { render :new }
end
if @contact.save
flash[:notice] = "Contact '#{@contact.name}' was successfully created."
redirect_to admin_location_path(@location)
else
render :new
end
end

def destroy
contact = Contact.find(params[:id])
contact.destroy
respond_to do |format|
format.html do
redirect_to admin_location_path(contact.location),
notice: "Contact '#{contact.name}' was successfully deleted."
end
end
redirect_to admin_location_path(contact.location),
notice: "Contact '#{contact.name}' was successfully deleted."
end
end
end
60 changes: 60 additions & 0 deletions app/controllers/admin/organization_contacts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Admin
class OrganizationContactsController < ApplicationController
before_action :authenticate_admin!
layout 'admin'

def edit
@organization = Organization.find(params[:organization_id])
@contact = Contact.find(params[:id])
@admin_decorator = AdminDecorator.new(current_admin)

unless @admin_decorator.allowed_to_access_organization?(@organization)
redirect_to admin_dashboard_path,
alert: "Sorry, you don't have access to that page."
end
end

def update
@contact = Contact.find(params[:id])
@organization = Organization.find(params[:organization_id])

if @contact.update(params[:contact])
flash[:notice] = 'Contact was successfully updated.'
redirect_to [:admin, @organization, @contact]
else
render :edit
end
end

def new
@admin_decorator = AdminDecorator.new(current_admin)
@organization = Organization.find(params[:organization_id])

unless @admin_decorator.allowed_to_access_organization?(@organization)
redirect_to admin_dashboard_path,
alert: "Sorry, you don't have access to that page."
end

@contact = Contact.new
end

def create
@organization = Organization.find(params[:organization_id])
@contact = @organization.contacts.new(params[: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])
contact.destroy
redirect_to admin_organization_path(contact.organization),
notice: "Contact '#{contact.name}' was successfully deleted."
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class OrganizationsController < ApplicationController
include CustomErrors

def index
orgs = Organization.page(params[:page]).per(params[:per_page])
orgs = Organization.includes(:contacts).page(params[:page]).per(params[:per_page])
render json: orgs, status: 200
generate_pagination_headers(orgs)
end
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/location_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LocationSerializer < ActiveModel::Serializer
has_many :services
has_many :regular_schedules
has_many :holiday_schedules
has_one :organization
has_one :organization, serializer: SummarizedOrganizationSerializer

def url
api_location_url(object)
Expand Down
13 changes: 13 additions & 0 deletions app/serializers/locations_organization_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class LocationsOrganizationSerializer < ActiveModel::Serializer
attributes :id, :accreditations, :alternate_name, :date_incorporated,
:description, :email, :funding_sources, :licenses, :name, :slug,
:website, :url, :locations_url

def url
api_organization_url(object)
end

def locations_url
api_org_locations_url(object)
end
end
2 changes: 1 addition & 1 deletion app/serializers/locations_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class LocationsSerializer < ActiveModel::Serializer
:updated_at, :urls, :contacts_url, :services_url, :url

has_one :address
has_one :organization
has_one :organization, serializer: LocationsOrganizationSerializer
has_many :phones

def contacts_url
Expand Down
2 changes: 2 additions & 0 deletions app/serializers/organization_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class OrganizationSerializer < ActiveModel::Serializer
:description, :email, :funding_sources, :licenses, :name, :slug,
:website, :url, :locations_url

has_many :contacts

def url
api_organization_url(object)
end
Expand Down
7 changes: 7 additions & 0 deletions app/serializers/summarized_organization_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SummarizedOrganizationSerializer < ActiveModel::Serializer
attributes :id, :alternate_name, :name, :slug, :url

def url
api_organization_url(object)
end
end
8 changes: 8 additions & 0 deletions app/views/admin/contacts/forms/_new_org_contact.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
= render 'admin/contacts/forms/fields', f: f

.save-box.navbar-default
%p
= 'Creating contact for'
%strong
= "#{@organization.name}"
= f.submit 'Create contact', class: 'btn btn-success', data: { disable_with: 'Please wait...' }
25 changes: 0 additions & 25 deletions app/views/admin/locations/forms/_contact_fields.html.haml

This file was deleted.

9 changes: 0 additions & 9 deletions app/views/admin/locations/forms/_contacts.html.haml

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/admin/locations/forms/_phone_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- else
= f.select :number_type, Phone.number_type.options, {}, class: 'form-control'
.form-group
= f.label :extension, 'Extension (for example: x101)'
= f.label :extension, 'Extension (numbers only)'
.row
.col-md-4
= f.text_field :extension, maxlength: 8, class: 'form-control'
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/organization_contacts/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.content-box
%h2= "#{@contact.try(:name)} / #{@organization.name}"
= form_for [:admin, @organization, @contact], html: { class: 'edit_entry' } do |f|
= render 'admin/contacts/form', f: f
5 changes: 5 additions & 0 deletions app/views/admin/organization_contacts/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.content-box
%h1 Create a new contact

= form_for [:admin, @organization, @contact], html: { method: :post, class: 'edit_entry' } do |f|
= render 'admin/contacts/forms/new_org_contact', f: f
11 changes: 11 additions & 0 deletions app/views/admin/organizations/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
= render 'admin/organizations/forms/fields', f: f

.content-box
%h2= 'Contacts'
- if @organization.contacts.present?
= 'Click a Contact below to view and update it:'
%p
- @organization.contacts.each do |contact|
= link_to contact.name, edit_admin_organization_contact_path(@organization, contact)
%br
%p
= link_to 'Add a new contact', new_admin_organization_contact_path(@organization), class: 'btn btn-primary'

.danger-zone
%header
%strong
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
resources :contacts, except: [:show, :index]
end

resources :organizations, except: :show
resources :organizations, except: :show do
resources :contacts, except: [:show, :index], controller: 'organization_contacts'
end
resources :programs, except: :show
resources :services, only: :index

Expand All @@ -32,6 +34,7 @@
get 'locations/:location_id/contacts/:id', to: 'contacts#edit'
get 'locations/:id', to: 'locations#edit'
get 'organizations/:id', to: 'organizations#edit'
get 'organizations/:organization_id/contacts/:id', to: 'organization_contacts#edit'
get 'programs/:id', to: 'programs#edit'
end
end
Expand Down
Binary file modified data/ohana_api_development.dump
Binary file not shown.
11 changes: 1 addition & 10 deletions spec/api/get_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,14 @@

it 'includes the serialized organization association' do
org = @location.organization
locations_url = api_org_locations_url(org)

serialized_organization =
{
'id' => @location.organization.id,
'accreditations' => [],
'alternate_name' => nil,
'date_incorporated' => nil,
'description' => 'Organization created for testing purposes',
'email' => nil,
'funding_sources' => [],
'licenses' => [],
'locations_url' => locations_url,
'name' => 'Parent Agency',
'slug' => 'parent-agency',
'url' => api_organization_url(org),
'website' => nil
'url' => api_organization_url(org)
}

expect(json['organization']).to eq(serialized_organization)
Expand Down
6 changes: 6 additions & 0 deletions spec/api/get_locations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@
expect(json.first.keys).to include('organization')
end

it 'does not include contacts within Organization' do
get api_locations_url(subdomain: ENV['API_SUBDOMAIN'])
org_keys = json.first['organization'].keys
expect(org_keys).to_not include('contacts')
end

it 'includes the correct url attribute' do
loc_url = json.first['url']

Expand Down
19 changes: 19 additions & 0 deletions spec/features/admin/contacts/delete_contact_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

feature 'Delete contact' do
background do
@location = create(:location)
@location.contacts.create!(attributes_for(:contact))
login_super_admin
visit '/admin/locations/vrs-services'
click_link 'Moncef Belyamani'
end

scenario 'when deleting contact' do
find_link('Permanently delete this contact').click
using_wait_time 1 do
expect(current_path).to eq admin_location_path(@location)
expect(page).not_to have_link 'Moncef Belyamani'
end
end
end
Loading

0 comments on commit 6d13382

Please sign in to comment.