ソースを参照

Create assignments controller

Frans Bergman 7 年 前
コミット
654dc6a576

+ 89 - 0
app/controllers/assignments_controller.rb

@@ -0,0 +1,89 @@
+class AssignmentsController < ApplicationController
+  before_action :set_assignment, only: [:show, :edit, :update, :destroy]
+  before_action :check_permissions, only: [:edit, :update]
+
+  # GET /assignments
+  # GET /assignments.json
+  def index
+    @assignments = current_user.assignments
+  end
+
+  # GET /assignments/1
+  # GET /assignments/1.json
+  def show
+  end
+
+  # GET /assignments/new
+  def new
+    @assignment = Assignment.new
+  end
+
+  # GET /assignments/1/edit
+  def edit
+  end
+
+  # POST /assignments
+  # POST /assignments.json
+  def create
+    @assignment = Assignment.new(assignment_params)
+
+    check_permissions
+
+    respond_to do |format|
+      if @assignment.save
+        flash[:success] = 'Assignment was successfully created.'
+        format.html { redirect_to @assignment }
+        format.json { render :show, status: :created, location: @assignment }
+      else
+        format.html { render :new }
+        format.json { render json: @assignment.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # PATCH/PUT /assignments/1
+  # PATCH/PUT /assignments/1.json
+  def update
+    respond_to do |format|
+      if @assignment.update(assignment_params)
+        flash[:success] = 'Assignment was successfully updated.'
+        format.html { redirect_to @assignment }
+        format.json { render :show, status: :ok, location: @assignment }
+      else
+        format.html { render :edit }
+        format.json { render json: @assignment.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /assignments/1
+  # DELETE /assignments/1.json
+  def destroy
+    @assignment.destroy
+    respond_to do |format|
+      flash[:success] = 'Assignment was successfully destroyed.'
+      format.html { redirect_to assignments_url }
+      format.json { head :no_content }
+    end
+  end
+
+  private
+    # Use callbacks to share common setup or constraints between actions.
+    def set_assignment
+      @assignment = Assignment.find(params[:id])
+    end
+
+    # Never trust parameters from the scary internet, only allow the white list through.
+    def assignment_params
+      allowed = [:name, :description, :due_at]
+
+      # Don't allow moving an assignment to a different course
+      allowed += [:course_id] if action_name == 'create'
+
+      params.require(:assignment).permit(*allowed)
+    end
+
+    def check_permissions
+      redirect_to root_url unless current_user.is_course_teacher?(@assignment.course)
+    end
+end

+ 2 - 0
app/helpers/assignments_helper.rb

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

+ 16 - 0
app/views/assignments/_assignment.html.erb

@@ -0,0 +1,16 @@
+<div class="panel panel-danger">
+  <div class="panel-heading">
+    <%= assignment.name %>
+  </div>
+  <div class="panel-body">
+    <p>
+      Course: <%= link_to assignment.course.name, assignment.course %>
+    </p>
+    <p>
+      <%= format_content(assignment.description) %>
+    </p>
+  </div>
+  <div class="panel-footer">
+    Due at: <%= assignment.due_at %>
+  </div>
+</div>

+ 2 - 0
app/views/assignments/_assignment.json.jbuilder

@@ -0,0 +1,2 @@
+json.extract! assignment, :id, :created_at, :updated_at, :name, :description, :due_at
+json.url assignment_url(assignment, format: :json)

+ 17 - 0
app/views/assignments/_form.html.erb

@@ -0,0 +1,17 @@
+<%= bootstrap_form_for(@assignment) do |f| %>
+  <%= render 'shared/error_messages', object: f.object %>
+
+  <% unless action_name == 'create' %>
+    <%= f.select :course_id, options_for_select(current_user.courses.merge(CourseParticipation.teachers).map{ |course| [course.name, course.id] }) %>
+  <% else %>
+    <%= f.text_field :course, value: @assignment.course.name, disabled: true %>
+  <% end %>
+
+  <%= f.text_field :name %>
+
+  <%= f.datetime_field :due_at %>
+
+  <%= f.text_area :description %>
+
+  <%= f.submit yield(:button_text), class: "btn btn-primary" %>
+<% end %>

+ 6 - 0
app/views/assignments/edit.html.erb

@@ -0,0 +1,6 @@
+<% provide(:title, "Edit Assignment") %>
+<% provide(:button_text, "Save") %>
+
+<h1>Edit Assignment</h1>
+
+<%= render 'form' %>

+ 6 - 0
app/views/assignments/index.html.erb

@@ -0,0 +1,6 @@
+<p id="notice"><%= notice %></p>
+
+<h1>Assignments</h1>
+<% @assignments.each do |assignment| %>
+  <%= render assignment %>
+<% end %>

+ 1 - 0
app/views/assignments/index.json.jbuilder

@@ -0,0 +1 @@
+json.array! @assignments, partial: 'assignments/assignment', as: :assignment

+ 5 - 0
app/views/assignments/new.html.erb

@@ -0,0 +1,5 @@
+<% provide(:title, "New Assignment") %>
+<% provide(:button_text, "Create") %>
+<h1>New Assignment</h1>
+
+<%= render 'form' %>

+ 9 - 0
app/views/assignments/show.html.erb

@@ -0,0 +1,9 @@
+<% provide(:title, @assignment.name) %>
+
+<h1>Assignment</h1>
+
+<%= render @assignment %>
+
+<%= link_to edit_assignment_path(@assignment), class: "btn btn-primary" do %>
+  <%= fa_icon 'edit', text: "Edit" %>
+<% end if current_user.is_course_teacher?(@assignment.course) %>

+ 1 - 0
app/views/assignments/show.json.jbuilder

@@ -0,0 +1 @@
+json.partial! "assignments/assignment", assignment: @assignment

+ 3 - 0
app/views/layouts/_navigation.html.erb

@@ -91,6 +91,9 @@
         <li>
           <%= link_to fa_icon("book fw", text: "Courses"), courses_user_path(current_user) %>
         </li>
+        <li>
+          <%= link_to fa_icon("paperclip fw", text: "Assignments"), assignments_path %>
+        </li>
       </ul>
     </div>
   </div>

+ 1 - 0
config/routes.rb

@@ -20,4 +20,5 @@ Rails.application.routes.draw do
   resources :courses
   resources :course_participations
   resources :lectures
+  resources :assignments
 end

+ 58 - 0
test/controllers/assignments_controller_test.rb

@@ -0,0 +1,58 @@
+require 'test_helper'
+
+class AssignmentsControllerTest < ActionDispatch::IntegrationTest
+  setup do
+    @assignment = assignments(:one)
+    @course = courses(:one)
+    @global_admin = users(:admin)
+    log_in_as @global_admin
+  end
+
+  test "should get index" do
+    get assignments_url
+    assert_response :success
+  end
+
+  test "should get new" do
+    get new_assignment_url
+    assert_response :success
+  end
+
+  test "should create assignment" do
+    assert_difference('Assignment.count') do
+      post assignments_url, params: { assignment: { course_id: @course.id, name: "Lorem Ipsum", due_at: 2.days.since } }
+    end
+
+    assert_redirected_to assignment_url(Assignment.last)
+  end
+
+  test "should not create invalid assignment" do
+    assert_no_difference('Assignment.count') do
+      post assignments_url, params: { assignment: { course_id: @course.id, name: "Lorem Ipsum" } }
+    end
+  end
+
+  test "should show assignment" do
+    get assignment_url(@assignment)
+    assert_response :success
+  end
+
+  test "should get edit" do
+    get edit_assignment_url(@assignment)
+    assert_response :success
+  end
+
+  test "should update assignment" do
+    patch assignment_url(@assignment), params: { assignment: { name: "New Name" } }
+    assert_redirected_to assignment_url(@assignment)
+    assert_equal @assignment.reload.name, "New Name"
+  end
+
+  test "should destroy assignment" do
+    assert_difference('Assignment.count', -1) do
+      delete assignment_url(@assignment)
+    end
+
+    assert_redirected_to assignments_url
+  end
+end