Browse Source

Add course participations controller

Frans Bergman 7 years ago
parent
commit
728a0ed3f5

+ 3 - 0
app/assets/javascripts/course_participations.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/course_participations.scss

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

+ 31 - 0
app/controllers/course_participations_controller.rb

@@ -0,0 +1,31 @@
+class CourseParticipationsController < ApplicationController
+
+  def create
+    @course = Course.find(participation_params[:course_id])
+    check_permissions
+    @participation = CourseParticipation.new(participation_params)
+    @participation.save
+    redirect_to @course
+  end
+
+  def destroy
+    @participation = CourseParticipation.find(params[:id])
+    @course = @participation.course
+    check_permissions
+    @participation.destroy
+    redirect_to @course
+  end
+
+  private
+    def participation_params
+      params.require(:course_participation).permit(:course_id,
+                                                   :user_id,
+                                                   :role)
+    end
+
+    def check_permissions
+      unless current_user.is_administrator_at?(@course.school) || current_user.admin?
+        redirect_to root_url
+      end
+    end
+end

+ 2 - 0
app/helpers/course_participations_helper.rb

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

+ 9 - 0
app/views/course_participations/_form.html.erb

@@ -0,0 +1,9 @@
+<%= bootstrap_form_for(@course.course_participations.build, html: { class: "form-inline" }) do |f| %>
+  <%= render 'shared/error_messages', object: f.object %>
+  <%= f.hidden_field :course_id, value: @course.id %>
+  <%= f.hidden_field :role, value: role %>
+
+  <%= f.select :user_id, options_for_select(User.where.not(id: @course.users.map(&:id)).map{ |user| [user.name, user.id] }) %>
+
+  <%= f.submit "Add user", class: "btn btn-primary" %>
+<% end %>

+ 28 - 0
app/views/courses/_participants_panel.html.erb

@@ -0,0 +1,28 @@
+<div class="panel panel-primary">
+  <div class="panel-heading">
+    <%= role.to_s.capitalize.pluralize %>
+  </div>
+  <table class="panel-body table table-striped table-bordered table-hover">
+    <tbody>
+      <% for user in users %>
+        <tr>
+          <td>
+            <%= render user %>
+          </td>
+          <% if current_user.is_administrator_at?(@course.school) || current_user.admin? %>
+            <td>
+              <%= form_for @course.course_participations.find_by(user_id: user.id), html: { method: :delete } do |f| %>
+                <%= f.submit "Remove", class: "btn btn-danger" %>
+              <% end %>
+            </td>
+          <% end %>
+        </tr>
+      <% end %>
+    </tbody>
+  </table>
+  <div class="panel-footer">
+    <% if current_user.is_administrator_at?(@course.school) || current_user.admin? %>
+      <%= render partial: 'course_participations/form', locals: { role: role } %>
+    <% end %>
+  </div>
+</div>

+ 2 - 32
app/views/courses/show.html.erb

@@ -6,37 +6,7 @@
 </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>
+    <%= render partial: 'participants_panel', locals: { users: @course.users.merge(CourseParticipation.teachers), role: :teacher } %>
+    <%= render partial: 'participants_panel', locals: { users: @course.users.merge(CourseParticipation.students), role: :student } %>
   </div>
 </div>

+ 1 - 0
config/routes.rb

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

+ 29 - 0
test/controllers/course_participations_controller_test.rb

@@ -0,0 +1,29 @@
+require 'test_helper'
+
+class CourseParticipationsControllerTest < ActionDispatch::IntegrationTest
+  def setup
+    @admin = users(:admin)
+    @student = users(:billy)
+    @course = courses(:two)
+    @course_participation = course_participations(:two)
+    log_in_as @admin
+  end
+
+  test "should create valid course participation" do
+    get course_path @course
+    assert_difference 'CourseParticipation.count', 1 do
+      post course_participations_path,
+        params: { course_participation: {
+          course_id: @course.id, user_id: @student.id
+        } }
+    end
+    assert_redirected_to course_path @course
+  end
+
+  test "should delete course participation" do
+    assert_difference 'CourseParticipation.count', -1 do
+      delete course_participation_path @course_participation
+    end
+    assert_redirected_to course_path courses(:one)
+  end
+end