Skip to content

Commit

Permalink
RSpec for Supervisor Course View for destroy action 49775
Browse files Browse the repository at this point in the history
  • Loading branch information
aamehedi committed Dec 28, 2015
1 parent 65205e0 commit 84906a1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
12 changes: 10 additions & 2 deletions app/controllers/supervisor/courses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class Supervisor::CoursesController < ApplicationController
before_action :authenticate_user!
before_action :load_course, except: [:index, :new, :create]
before_action :check_course, only: [:update, :destroy]
before_action :check_course_before_update, only: :update
before_action :check_course_before_destroy, only: :destroy
load_and_authorize_resource only: [:edit, :update, :destroy]

def index
Expand Down Expand Up @@ -63,10 +64,17 @@ def course_params
:update_status, subject_ids: []
end

def check_course
def check_course_before_update
if @course.invalid_for_update? course_params.has_key?(:update_status)
flash[:danger] = t ".danger"
redirect_to supervisor_courses_path
end
end

def check_course_before_destroy
unless @course.finished?
flash[:danger] = t ".danger"
redirect_to supervisor_courses_path
end
end
end
8 changes: 4 additions & 4 deletions app/views/supervisor/courses/_course.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<td><%= (I18n.localize course.end_date, format: :short) if course.end_date %></td>
<td><%= course.supervisors.count %></td>
<td><%= course.trainees.count %></td>
<% if can? :manage, @course %>
<% if @course.created? %>
<% if can? :manage, course %>
<% if course.created? %>
<td><%= link_to t(".edit"), edit_supervisor_course_path(course) %></td>
<% elsif @course.finished? %>
<td><%= link_to t(".delete"), [:supervisor, @course], method: :delete,
<% elsif course.finished? %>
<td><%= link_to t(".delete"), supervisor_course_path(course), method: :delete,
class: "btn btn-danger", data: { confirm: t(:confirm_message) } %></td>
<% end %>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ en:
edit: Edit
destroy:
course_delete: Successfully Deleted
danger: Cannot delet this course. Course is not finished yet.
edit:
danger: Cannot edit course now.
submit: Update Course
Expand Down
5 changes: 4 additions & 1 deletion spec/controllers/supervisor/courses_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@
describe "DELETE destroy" do
let!(:num_of_courses) {Course.count}

before {delete :destroy, {id: english, course: english.attributes}}
before do
english.finished!
delete :destroy, {id: english, course: english.attributes}
end

context "delete successfully" do
it {expect(response).to have_http_status 302}
Expand Down
27 changes: 27 additions & 0 deletions spec/features/destroy_course_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "rails_helper"

feature "Destroy course" do
subject {page}
let(:supervisor) {FactoryGirl.create :supervisor}
let!(:course){FactoryGirl.create :course, user_ids: [supervisor.id]}
before :each do
course.finished!
login_as supervisor, scope: :user
visit supervisor_courses_path
end

scenario "has delete button" do
Course.finished.each do |course|
is_expected.to have_link(I18n.t("supervisor.courses.course.delete"), href:
"/supervisor/courses/#{course.id}")
end
end

scenario "destroy course feature", js: true do
click_link(I18n.t("supervisor.courses.course.delete"), href:
"/supervisor/courses/#{course.id}")
sleep 3
page.driver.browser.switch_to.alert.accept
is_expected.to have_text I18n.t "supervisor.courses.destroy.course_delete"
end
end

0 comments on commit 84906a1

Please sign in to comment.