Skip to content

Commit

Permalink
Feature/#155 course file view (#189)
Browse files Browse the repository at this point in the history
* add prototype for filelist

* add download link

*  Tests for #155, remove double tests for show_courses

* Differntiate student and lecturer files and add scroll bar

* Missing test case

* fix merge

Co-authored-by: Adrian Jost <22987140+adrianjost@users.noreply.github.com>
  • Loading branch information
caustt and adrianjost authored Jan 14, 2020
1 parent d1529e3 commit 516337c
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 52 deletions.
7 changes: 6 additions & 1 deletion app/assets/stylesheets/courses.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Place all the styles related to the courses controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
// You can use Sass (SCSS) here: https://sass-lang.com/

.tab-content {
max-height:270px;
overflow-y: auto;
}
2 changes: 2 additions & 0 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def index
# GET /courses/1
def show
@current_user = current_user
@student_files = @course.uploaded_files.student_files
@lecturer_files = @course.uploaded_files.lecturer_files
end

# GET /courses/new
Expand Down
21 changes: 21 additions & 0 deletions app/models/uploaded_file.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
class UploadedFile < ApplicationRecord
belongs_to :allowsUpload, polymorphic: true
belongs_to :author, class_name: :User


def self.student_files
@files = []
all.each do |file|
if file.author.is_student
@files << file
end
end
@files
end

def self.lecturer_files
@files = []
all.each do |file|
if !file.author.is_student
@files << file
end
end
@files
end
end
15 changes: 15 additions & 0 deletions app/views/courses/_filelist.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<ul class="list-group mb-2">
<% files.each do |file| %>
<li class="list-group-item d-flex justify-content-between align-items-center">
<div>
<%= fa_icon 'file', :class => "text-info" %>
<%= link_to file.filename, uploaded_file_path(file), target: "_self", class:"btn btn-link" %>
</div>
<div>
<% if @current_user == file.author || @current_user == @course.creator %>
<button class="btn btn-sm btn-outline-danger">TODO ADD DELETE ACTION</button>
<% end %>
</div>
</li>
<% end %>
</ul>
23 changes: 23 additions & 0 deletions app/views/courses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@

<br>

<section>
<h2>Files</h2>
<div class="d-flex justify-content-between nav-tabs">
<ul class="nav" id="fileTabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="files-course-tab" data-toggle="tab" href="#files-course" role="tab" aria-controls="files-course" aria-selected="true">From Course</a>
</li>
<li class="nav-item">
<a class="nav-link" id="files-students-tab" data-toggle="tab" href="#files-students" role="tab" aria-controls="files-students" aria-selected="false">From Students</a>
</li>
</ul>
</div>
<div class="tab-content" id="fileTabsContent">
<div class="tab-pane fade show active" id="files-course" role="tabpanel" aria-labelledby="files-course-tab">
<%= render 'filelist', files: @lecturer_files %>
</div>
<div class="tab-pane fade" id="files-students" role="tabpanel" aria-labelledby="files-students-tab">
<%= render 'filelist', files: @student_files %>
</div>
</div>
</section>


<% if !@course.lectures.empty? %>
<h2>All Lectures</h2>
<%= render 'table_not_running', course: @course %>
Expand Down
41 changes: 40 additions & 1 deletion spec/features/courses/show_course_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

it "should have a \"Start\" button for not started lectures" do
visit(course_path(@course))
expect(page).to have_link("Start")
expect(page).to have_link("Start", href: start_lecture_path(course_id: @lecture.course.id) + "?id=" + @lecture.id.to_s)
end

it "should not have a \"View\" link for not started lectures" do
Expand Down Expand Up @@ -72,5 +72,44 @@
visit(course_lecture_path(course_id: @course.id, id: @lecture.id))
expect(page).to have_current_path(course_path(@course))
end

it "should show a file that was uploaded to the course by a lecturer" do
@file = FactoryBot.create(:uploaded_file, author: @lecturer, allowsUpload_id: @course.id, allowsUpload_type: "Course")
visit(course_path(@course))
expect(page).to have_link(@file.filename, href: uploaded_file_path(@file))
end

it "should show a file that was uploaded to the course by a student" do
@student = FactoryBot.create(:user, :student)
@file = FactoryBot.create(:uploaded_file, author: @student, allowsUpload_id: @course.id, allowsUpload_type: "Course")
visit(course_path(@course))
expect(page).to have_link(@file.filename, href: uploaded_file_path(@file))
end

it "should not show a file that is not linked to the course" do
@other_course = FactoryBot.create(:course, creator: @lecturer)
@file = FactoryBot.create(:uploaded_file, author: @lecturer, allowsUpload_id: @other_course.id, allowsUpload_type: "Course")
visit(course_path(@course))
expect(page).not_to have_link(@file.filename, href: uploaded_file_path(@file))
end

it "should show student and lecturer files in according tabs" do
@student = FactoryBot.create(:user, :student)
@lecturer_file = FactoryBot.create(:uploaded_file, author: @lecturer, allowsUpload_id: @course.id, allowsUpload_type: "Course")
@student_file = FactoryBot.create(:uploaded_file, author: @student, allowsUpload_id: @course.id, allowsUpload_type: "Course")
visit(course_path(@course))
expect(page).to have_xpath(".//div[@id='files-course']//ul//li//div//a[@href='#{uploaded_file_path(@lecturer_file)}']")
expect(page).to_not have_xpath(".//div[@id='files-course']//ul//li//div//a[@href='#{uploaded_file_path(@student_file)}']")
expect(page).to have_xpath(".//div[@id='files-students']//ul//li//div//a[@href='#{uploaded_file_path(@student_file)}']")
expect(page).to_not have_xpath(".//div[@id='files-students']//ul//li//div//a[@href='#{uploaded_file_path(@lecturer_file)}']")
end

it "should download a course file when clicking on it" do
@lecturer_file = FactoryBot.create(:uploaded_file, author: @lecturer, allowsUpload_id: @course.id, allowsUpload_type: "Course")
visit(course_path(@course))
click_on @lecturer_file.filename
expect(page.body).to eql @lecturer_file.data
expect(page.response_headers["Content-Type"]).to eql @lecturer_file.content_type
end
end
end
54 changes: 4 additions & 50 deletions spec/views/courses/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
@course = FactoryBot.create(:course, creator: @lecturer)
@lecture = FactoryBot.create(:lecture, name: "Name", enrollment_key: "Enrollment", status: "created", course: @course, lecturer: @lecturer)
@current_user = FactoryBot.create(:user, :lecturer)
@student_files = []
@lecturer_files = []
sign_in @lecturer
end

it "renders title and description of the course " do
render
expect(rendered).to have_text("SWT2")
expect(rendered).to have_text("ruby")
expect(rendered).to match(@course.name)
expect(rendered).to match(@course.description)
end

it "renders a list of lectures" do
Expand All @@ -22,52 +24,4 @@
assert_select "tr>td", text: @lecture.enrollment_key, count: 1
assert_select "tr>td", text: @lecture.status, count: 1
end

it "should have a \"Start\" link for not started lecture" do
visit(course_path(id: @lecture.course.id))

expect(page).to have_link("Start", href: start_lecture_path(course_id: @lecture.course.id) + "?id=" + @lecture.id.to_s)
end

it "should not have a \"View\" link for not started lecture" do
visit(course_path(id: @lecture.course.id))
expect(page).to_not have_link("View", href: course_lecture_path(course_id: @lecture.course.id, id: @lecture.id))
end

it "should have a \"Create Lecture\" button" do
visit(course_path(id: @lecture.course.id))
expect(page).to have_link("Create Lecture")
end

it "should set the lecture active on clicking \"Start\"" do
visit(course_path(id: @lecture.course.id))
click_on("Start")
@lecture.reload
expect(@lecture.status).to eq("running")
end

it "should redirect to the show path after clicking \"Start\"" do
visit(course_path(id: @lecture.course.id))
click_on("Start")
expect(current_path).to eq(course_lecture_path(course_id: @lecture.course.id, id: @lecture.id))
end

it "should not show the \"Start\" button after a lecture was started" do
visit(course_path(id: @lecture.course.id))
click_on("Start")
expect(page).not_to have_selector("input[type=submit][value='Start']")
end

it "should show a \"View\" link after the lecture is started" do
@lecture.update(status: "running")
visit(course_path(id: @lecture.course.id))
expect(page).to have_link("View", href: course_lecture_path(course_id: @lecture.course.id, id: @lecture.id))
end

it "should not show lectures of other lecturers" do
@lecture2 = FactoryBot.create(:lecture)
@lecture2.update(status: "running")
visit(course_path(id: @lecture.course.id))
expect(page).to_not have_link("View", href: course_lecture_path(course_id: @lecture2.course.id, id: @lecture2.id))
end
end

0 comments on commit 516337c

Please sign in to comment.