Skip to content

Commit

Permalink
Merge pull request #15 from Prozak8/job_create
Browse files Browse the repository at this point in the history
RSC can create a new job
  • Loading branch information
angiecstn committed Nov 5, 2018
2 parents 95b8f79 + 374748e commit 88418f9
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 24 deletions.
16 changes: 16 additions & 0 deletions app/controllers/jobs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# frozen_string_literal: true

class JobsController < ApplicationController
def index
@jobs = Job.all
end

def create
job = Job.create(job_params)
if job.persisted?
redirect_to root_path, notice: 'The job was successfully created'
else
errors = job.errors.full_messages
render json: { message: errors }, status: 422
end
end

def job_params
params.require(:job).permit(:name, :profession, :hospital, :department, :license, :caretype, :scope, :working_hours, :date_start, :date_finish)
end
end
19 changes: 11 additions & 8 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# frozen_string_literal: true

class UsersController < ApplicationController
def index
end

def new
end
def index; end

def new; end

def create
user = User.create(user_params.merge(password: Devise.friendly_token[0, 20]))
if user.persisted?
redirect_to root_path, notice: "RSC user #{User.last.email} created, currently logged in as #{current_user.email}."
message = " Currently logged in as #{current_user.email}" if user_signed_in?
redirect_to root_path, notice: "RSC user #{User.last.email} created. #{message}."
else
redirect_to root_path, notice: "Invalid Email"
errors = user.errors.full_messages
render json: { message: errors }, status: 422
end
end

private

def user_params
params.require(:user).permit(:email)
end
end
end
4 changes: 1 addition & 3 deletions app/views/jobs/_index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@
%p= job.name
#form-button
%button.button Remove
.add-field
%input.add-field-placeholder{placeholder: "Add new job"}
%button.button Add
= link_to "Add New Job", new_job_path, class: "button", remote: true
30 changes: 30 additions & 0 deletions app/views/jobs/_new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
- content_for :modal_content do
.card
.inner-card
.card-title
%h1.text-center.mt-6 Create New Job
= form_with scope: :job, url: jobs_path, id: :create_job do |form|
#error_messages
= form.label :name
= form.text_field :name, placeholder: "Name", class: "add-field-placeholder"
= form.label :profession
= form.text_field :profession, placeholder: "Profession", class: "add-field-placeholder"
= form.label :hospital
= form.text_field :hospital, placeholder: "Hospital", class: "add-field-placeholder"
= form.label :department
= form.text_field :department, placeholder: "Department", class: "add-field-placeholder"
= form.label :license
= form.text_field :license, placeholder: "License/s", class: "add-field-placeholder"
= form.label :caretype
= form.text_field :caretype, placeholder: "Caretype", class: "add-field-placeholder"
= form.label :scope
= form.text_field :scope, placeholder: "Scope", class: "add-field-placeholder"
= form.label :working_hours
= form.text_field :working_hours, placeholder: "hours", class: "add-field-placeholder"
= form.label :date_start
= form.text_field :date_start, placeholder: "y/d/m", class: "add-field-placeholder"
= form.label :date_finish
= form.text_field :date_finish, placeholder: "y/d/m", class: "add-field-placeholder"
= form.submit value: "Submit", class: "bg-blue hover:bg-blue-dark text-white font-bold py-2 px-4 my-4 rounded focus:outline-none focus:shadow-outline"
= button_tag 'Cancel', id: 'cancel_modal', class: 'signup-button'
= render partial: 'partials/modal'
13 changes: 13 additions & 0 deletions app/views/jobs/new.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

var modalContent = '<%= escape_javascript(render partial: 'new') %>'
App.mainContent.style.visibility = "hidden";
App.tempContent.innerHTML = modalContent
closeModalListener('cancel_modal')

var form = document.getElementById('create_job')

form.addEventListener('ajax:error', (event) => {
let errorMessageDisplayElement = document.getElementById('error_messages')
let message = event.detail[0].message
errorMessageDisplayElement.innerHTML = message
})
6 changes: 3 additions & 3 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#content
#temp_content
#main_content
= notice
= alert
= flash[:error]
#messages
= notice
= alert
= yield
5 changes: 3 additions & 2 deletions app/views/users/_new.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

- content_for :modal_content do
- content_for :modal_content do
.login-card
.card-background
.card-text
%h2 Add new RSC user
= form_with scope: :user, url: users_path, local: true do |form|
= form_with scope: :user, url: users_path, id: :create_user do |form|
#error_messages
.field
= form.label :email, class: "email-label"
.field
Expand Down
Empty file removed app/views/users/create.html.haml
Empty file.
23 changes: 19 additions & 4 deletions app/views/users/new.js.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@

console.log('registration')

var modalContent = '<%= escape_javascript(render partial: 'new') %>'
App.mainContent.style.visibility = "hidden";
App.tempContent.innerHTML = modalContent
closeModalListener('cancel_modal')
closeModalListener('cancel_modal')


var form = document.getElementById('create_user')

form.addEventListener('ajax:error', (event) => {
let errorMessageDisplayElement = document.getElementById('error_messages')
let message = event.detail[0].message
errorMessageDisplayElement.innerHTML = message
})

form.addEventListener('ajax:success', (event) => {
let errorMessageDisplayElement = document.getElementById('messages')
let message = event.detail[0].message
let model = document.getElementById('modal')
errorMessageDisplayElement.innerHTML = message
closeModal(modal)
})

3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
registrations: :registrations,
sessions: :sessions
}
root controller: :jobs, action: :index
root controller: :jobs, action: :index
resources :jobs, only: [:new, :create]
resources :users, only: [:create, :new]
resources :staffcos, only: :index
resources :hospitals, only: :index
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20181105105432_add_profession_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddProfessionToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :profession, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181105105533_add_hospital_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddHospitalToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :hospital, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181105105639_add_department_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDepartmentToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :department, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181105105713_add_license_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLicenseToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :license, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181105105743_add_caretype_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCaretypeToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :caretype, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181105105803_add_scope_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddScopeToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :scope, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181105105826_add_working_hours_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddWorkingHoursToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :working_hours, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181105105903_add_date_start_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDateStartToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :date_start, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181105105919_add_date_finish_to_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDateFinishToJobs < ActiveRecord::Migration[5.2]
def change
add_column :jobs, :date_finish, :string
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_11_02_125717) do
ActiveRecord::Schema.define(version: 2018_11_05_105919) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -25,12 +25,22 @@
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "profession"
t.string "hospital"
t.string "department"
t.string "license"
t.string "caretype"
t.string "scope"
t.string "working_hours"
t.string "date_start"
t.string "date_finish"
end

create_table "staffcos", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email"
end

create_table "users", force: :cascade do |t|
Expand Down
25 changes: 25 additions & 0 deletions features/rsc_can_add_new_job.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@javascript
Feature: Rsc can add new job
As a RSC,
In order to create jobs,
I would like to click on the “+” button and fill in the jobs information.

Background: Landing page, clicking new and being on form page
Given I am on the landing page
And I click on "Add New Job"

Scenario: RSC creates a new job
Then I should see "Create New Job"
And I fill in job form
And I click on "Submit"
Then I should see "The job was successfully created"
And I should see "Heart Surgeon"

Scenario: RSC fills out form incorrectly
And I fill in "Name" with " "
And I click on "Submit"
Then I should see "Name can't be blank"

Scenario: RSC wants to add new job but decides not to
And I click on "Cancel"
Then I should be on the landing page
5 changes: 3 additions & 2 deletions features/rsc_can_register_to_account.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ Feature: RSC creates a new rsc user
Scenario: RSC can sucessfully add another RSC user to access the platform
Given I fill in "Email" with "newrsc@email.com"
And I click on "Create account"
Then I should see the message "RSC user newrsc@email.com created, currently logged in as email@email.com."
Then I should see the message "RSC user newrsc@email.com created."
And I should see "Currently logged in as email@email.com."

Scenario: RSC unsucessfully create another RSC user with invalid email or password
Given I fill in "Email" with "new_rsc"
And I click on "Create account"
Then I should see the message "Invalid Email"
Then I should see the message "Email is invalid"

Scenario: RSC logs out and try to log in with an incorrect email
Given I click on "Cancel"
Expand Down
13 changes: 13 additions & 0 deletions features/step_definitions/assertion_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@
Then("I should be on the jobs index") do
expect(page).to have_current_path(root_path)
end

Then("I fill in job form") do
fill_in 'Name', with: 'Heart Surgeon'
fill_in 'Profession', with: 'doctor'
fill_in 'Hospital', with: 'stockholm sjukhuset'
fill_in 'Department', with: 'Surgery'
fill_in 'License', with: 'Work'
fill_in 'Caretype', with: 'Elderly'
fill_in 'Scope', with: 'Scope'
fill_in 'Working hours', with: '9 hours'
fill_in 'Date start', with: 'Tomorrow'
fill_in 'Date finish', with: 'Next Year'
end
8 changes: 8 additions & 0 deletions spec/models/job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
RSpec.describe Job, type: :model do
describe 'DB table' do
it { is_expected.to have_db_column :name }
it { is_expected.to have_db_column :profession }
it { is_expected.to have_db_column :hospital }
it { is_expected.to have_db_column :department }
it { is_expected.to have_db_column :license }
it { is_expected.to have_db_column :caretype }
it { is_expected.to have_db_column :working_hours }
it { is_expected.to have_db_column :date_start }
it { is_expected.to have_db_column :date_finish }
end

describe 'Validations' do
Expand Down

0 comments on commit 88418f9

Please sign in to comment.