Browse Source

Add schools controller

Frans Bergman 7 years ago
parent
commit
f10f37b41d

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

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

+ 56 - 0
app/controllers/schools_controller.rb

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

+ 2 - 0
app/helpers/schools_helper.rb

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

+ 7 - 0
app/views/schools/_form.html.erb

@@ -0,0 +1,7 @@
+<%= bootstrap_form_for(@school) do |f| %>
+  <%= render 'shared/error_messages', object: f.object %>
+
+  <%= f.text_field :name %>
+
+  <%= f.submit yield(:button_text), class: "btn btn-primary" %>
+<% end %>

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

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

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

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

+ 6 - 0
app/views/schools/show.html.erb

@@ -0,0 +1,6 @@
+<% provide(:title, @school.name) %>
+<div class="row">
+  <div class="col-md-12">
+    <h1><%= @school.name %></h1>
+  </div>
+</div>

+ 1 - 0
config/routes.rb

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

+ 46 - 0
test/controllers/schools_controller_test.rb

@@ -0,0 +1,46 @@
+require 'test_helper'
+
+class SchoolsControllerTest < ActionDispatch::IntegrationTest
+  def setup
+    @school = schools(:two)
+    @student_user = users(:billy)
+    @school_admin = users(:daniel)
+    @global_admin = users(:admin)
+  end
+
+  test "should show school" do
+    log_in_as @student_user
+    get school_path @school
+    assert_response :success
+  end
+
+  test "student should not edit school" do
+    log_in_as @student_user
+    get edit_school_path @school
+    assert_redirected_to root_url
+  end
+
+  test "school admin should edit and update school" do
+    log_in_as @school_admin
+    get edit_school_path @school
+    assert_response :success
+    patch school_path(@school), params: { school: { name: "New Name" } }
+    assert_redirected_to @school
+    assert_equal "New Name", @school.reload.name
+  end
+
+  test "school admin should not create new school" do
+    log_in_as @school_admin
+    get new_school_path
+    assert_redirected_to root_url
+  end
+
+  test "global admin should create new school" do
+    log_in_as @global_admin
+    get new_school_path
+    assert_response :success
+    assert_difference "School.count", 1 do
+      post schools_path, params: { school: { name: "New School" } }
+    end
+  end
+end