-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from Noelryn/department_sidebar
Department view in hospitals page
- Loading branch information
Showing
23 changed files
with
201 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class DepartmentsController < ApplicationController | ||
def create | ||
department = Department.create(department_params) | ||
if department.persisted? | ||
hospital = Hospital.find(params[:department][:hospital_id]) | ||
html = render_to_string('hospitals/_list', formats: :html, layout: false, locals: { hospital: hospital }) | ||
render json: { message: 'Department has been added', html: html } | ||
else | ||
redirect_to hospitals_path, notice: 'Department already exists. Your department could not be saved' | ||
end | ||
end | ||
|
||
def get_departments | ||
hospital = Hospital.find_by_name(params['hospital']) | ||
html = render_to_string('hospitals/_department_select', formats: :html, layout: false, locals: { hospital: hospital }) | ||
render json: { html: html } | ||
end | ||
|
||
private | ||
|
||
def department_params | ||
params.require(:department).permit(:name, :hospital_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class Department < ApplicationRecord | ||
belongs_to :hospital | ||
validates_presence_of :name | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
class Hospital < ApplicationRecord | ||
has_many :jobs | ||
has_many :jobs | ||
belongs_to :region | ||
has_many :departments | ||
validates :name, uniqueness: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.to-field-wrapper | ||
.to-field-full-width | ||
= label_tag 'job[department]', 'Department', class: "form-label" | ||
= select_tag 'job[department]', options_for_select(hospital.departments.collect {|c| [ c.name ] }, { include_blank: true }), id: "select_department", class: "form-input-field" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
%ul | ||
- hospital.departments.each do |department| | ||
.list-grid | ||
%li.list-name | ||
= department.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#rightbar | ||
#error_messages | ||
.list-card | ||
.form-header Departments | ||
.list-item.flex#list | ||
= render partial: 'list', locals: {hospital: @hospital} | ||
= form_with url: departments_path, scope: :department, id: :create_department do |f| | ||
= f.hidden_field :hospital_id, value: @hospital.id | ||
.list-grid | ||
.list-name | ||
= f.text_field :name, class: "form-input-field" | ||
.list-button-area | ||
= f.submit value: "Add Department", class: "button text-center" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
var templateContent = '<%= escape_javascript(render partial: 'show') %>' | ||
rightTemplate(templateContent) | ||
|
||
var departmentForm = document.getElementById('create_department') | ||
console.log('show panel') | ||
|
||
departmentForm.addEventListener('ajax:success', (event) => { | ||
templateContent = event.detail[0].html | ||
document.getElementById('list').innerHTML = templateContent; | ||
var errorMessageDisplayElement = document.getElementById('messages') | ||
var message = event.detail[0].message | ||
errorMessageDisplayElement.innerHTML = message | ||
}) | ||
|
||
departmentForm.addEventListener('ajax:error', (event) => { | ||
console.log('added listener for error...') | ||
|
||
var errorMessageDisplayElement = document.getElementById('error_messages') | ||
var message = event.detail[0].message | ||
errorMessageDisplayElement.innerHTML = message | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#rightbar | ||
= content_for :rightbar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20181116141742_association_between_hospitals_and_departments.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AssociationBetweenHospitalsAndDepartments < ActiveRecord::Migration[5.2] | ||
def change | ||
add_reference :departments, :hospital, foreign_key: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.