Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add admin & API support for Organization Contacts. #264

Merged
merged 2 commits into from
Nov 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Contact < ActiveRecord::Base
attr_accessible :department, :email, :name, :title, :phones_attributes

belongs_to :location, touch: true
belongs_to :organization

has_many :phones, dependent: :destroy
accepts_nested_attributes_for :phones,
Expand Down
1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Organization < ActiveRecord::Base

has_many :locations, dependent: :destroy
has_many :programs, dependent: :destroy
has_many :contacts, dependent: :destroy

validates :name,
presence: { message: I18n.t('errors.messages.blank_for_org') },
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.
5 changes: 5 additions & 0 deletions db/migrate/20141106215928_add_organization_ref_to_contacts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOrganizationRefToContacts < ActiveRecord::Migration
def change
add_reference :contacts, :organization, index: true
end
end
12 changes: 11 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ CREATE TABLE contacts (
email text,
created_at timestamp without time zone,
updated_at timestamp without time zone,
department character varying(255)
department character varying(255),
organization_id integer
);


Expand Down Expand Up @@ -977,6 +978,13 @@ CREATE UNIQUE INDEX index_categories_services_on_service_id_and_category_id ON c
CREATE INDEX index_contacts_on_location_id ON contacts USING btree (location_id);


--
-- Name: index_contacts_on_organization_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--

CREATE INDEX index_contacts_on_organization_id ON contacts USING btree (organization_id);


--
-- Name: index_friendly_id_slugs_on_slug_and_sluggable_type; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
Expand Down Expand Up @@ -1319,3 +1327,5 @@ INSERT INTO schema_migrations (version) VALUES ('20141030012617');

INSERT INTO schema_migrations (version) VALUES ('20141030204742');

INSERT INTO schema_migrations (version) VALUES ('20141106215928');

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