Browse Source

Add courses controller

Frans Bergman 7 years ago
parent
commit
7e77ecbc32

+ 3 - 0
app/assets/javascripts/courses.coffee

@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/

+ 3 - 0
app/assets/stylesheets/courses.scss

@@ -0,0 +1,3 @@
+// Place all the styles related to the courses controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/

+ 56 - 0
app/controllers/courses_controller.rb

@@ -0,0 +1,56 @@
+class CoursesController < ApplicationController
+  before_action :set_course, only: [:show, :edit, :update, :destroy]
+  before_action :check_can_edit, only: [:edit, :update]
+  before_action :check_can_create, only: [:create]
+
+  def show
+  end
+
+  def new
+    @course = Course.new
+  end
+
+  def edit
+  end
+
+  def create
+    @course = Course.new(course_params)
+    if @course.save
+      flash[:success] = "Created course"
+      redirect_to @course
+    else
+      render :new
+    end
+  end
+
+  def update
+    if @course.update(course_params)
+      flash[:success] = "Updated course"
+      redirect_to @course
+    else
+      render :edit
+    end
+  end
+
+  private
+    # Use callbacks to share common setup or constraints between actions.
+    def set_course
+      @course = Course.find(params[:id])
+    end
+
+    def course_params
+      params.require(:course).permit(:name, :school_id, :starts_on, :ends_on)
+    end
+
+    def check_can_create
+      unless current_user.is_administrator_at?(School.find(course_params[:school_id])) || current_user.admin?
+        redirect_to root_url
+      end
+    end
+
+    def check_can_edit
+      unless current_user.is_administrator_at?(@course.school) || current_user.admin?
+        redirect_to root_url
+      end
+    end
+end

+ 2 - 0
app/helpers/courses_helper.rb

@@ -0,0 +1,2 @@
+module CoursesHelper
+end

+ 12 - 0
app/views/courses/_form.html.erb

@@ -0,0 +1,12 @@
+<%= bootstrap_form_for(@course) do |f| %>
+  <%= render 'shared/error_messages', object: f.object %>
+
+  <%= f.text_field :name %>
+
+  <%= f.select :school_id, options_for_select(current_user.schools_administering.map{ |school| [school.name, school.id] }) %>
+
+  <%= f.date_field :starts_on %>
+  <%= f.date_field :ends_on %>
+
+  <%= f.submit yield(:button_text), class: "btn btn-primary" %>
+<% end %>

+ 9 - 0
app/views/courses/edit.html.erb

@@ -0,0 +1,9 @@
+<% provide(:title, @course.name) %>
+<% provide(:button_text, 'Save changes') %>
+
+<h1>Update course</h1>
+<div class="row">
+  <div class="col-md-12">
+    <%= render 'form' %>
+  </div>
+</div>

+ 9 - 0
app/views/courses/new.html.erb

@@ -0,0 +1,9 @@
+<% provide(:title, 'New Course') %>
+<% provide(:button_text, 'Create') %>
+
+<h1>Create new course</h1>
+<div class="row">
+  <div class="col-md-12">
+    <%= render 'form' %>
+  </div>
+</div>

+ 42 - 0
app/views/courses/show.html.erb

@@ -0,0 +1,42 @@
+<% provide(:title, @course.name) %>
+<div class="row">
+  <div class="col-md-12">
+    <h1><%= @course.name %></h1>
+  </div>
+</div>
+<div class="row">
+  <div class="col-md-6">
+    <div class="panel panel-primary">
+      <div class="panel-heading">
+        Teachers
+      </div>
+      <table class="panel-body table table-striped table-bordered table-hover">
+        <tbody>
+          <% for teacher in @course.users.merge(CourseParticipation.teachers) %>
+            <tr>
+              <td>
+                <%= render teacher %>
+              </td>
+            </tr>
+          <% end %>
+        </tbody>
+      </table>
+    </div>
+    <div class="panel panel-primary">
+      <div class="panel-heading">
+        Students
+      </div>
+      <table class="panel-body table table-striped table-bordered table-hover">
+        <tbody>
+          <% for student in @course.users.merge(CourseParticipation.students) %>
+            <tr>
+              <td>
+                <%= render student %>
+              </td>
+            </tr>
+          <% end %>
+        </tbody>
+      </table>
+    </div>
+  </div>
+</div>

+ 1 - 0
config/routes.rb

@@ -13,4 +13,5 @@ Rails.application.routes.draw do
   resources :conversation_participations
   resources :messages
   resources :schools
+  resources :courses
 end

+ 56 - 0
test/controllers/courses_controller_test.rb

@@ -0,0 +1,56 @@
+require 'test_helper'
+
+class CoursesControllerTest < ActionDispatch::IntegrationTest
+  def setup
+    @course = courses(:two)
+    @school = schools(:two)
+    @student_user = users(:billy)
+    @school_admin = users(:daniel)
+    @global_admin = users(:admin)
+  end
+
+  test "should show course" do
+    log_in_as @student_user
+    get course_path @course
+    assert_response :success
+  end
+
+  test "student should not edit course" do
+    log_in_as @student_user
+    get edit_course_path @course
+    assert_redirected_to root_url
+  end
+
+  test "school admin should edit and update course" do
+    log_in_as @school_admin
+    get edit_course_path @course
+    assert_response :success
+    patch course_path(@course), params: { course: { name: "New Name" } }
+    assert_redirected_to @course
+    assert_equal "New Name", @course.reload.name
+  end
+
+  test "school admin should create new course" do
+    log_in_as @school_admin
+    get new_course_path
+    assert_response :success
+    assert_difference "Course.count", 1 do
+      post courses_path, params: { course: { name: "New Course",
+                                             school_id: @school.id,
+                                             starts_on: 2.days.ago,
+                                             ends_on: 2.days.since } }
+    end
+  end
+
+  test "global admin should create new course" do
+    log_in_as @global_admin
+    get new_course_path
+    assert_response :success
+    assert_difference "Course.count", 1 do
+      post courses_path, params: { course: { name: "New Course",
+                                             school_id: @school.id,
+                                             starts_on: 2.days.ago,
+                                             ends_on: 2.days.since } }
+    end
+  end
+end