Skip to content

Commit

Permalink
continue work on restful actions for tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 authored and tynsh committed Sep 18, 2020
1 parent 80383ae commit c379583
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 21 deletions.
6 changes: 4 additions & 2 deletions app/controllers/tutorials_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TutorialsController
class TutorialsController < ApplicationController
before_action :set_tutorial, only: [:edit, :destroy, :update, :cancel_edit]
authorize_resource
# authorize_resource

def new
@tutorial = Tutorial.new
Expand All @@ -12,13 +12,15 @@ def new
def create
@tutorial = Tutorial.new(tutorial_params)
@tutorial.save
@errors = @tutorial.errors
end

def edit
end

def update
@tutorial.update(tutorial_params)
@errors = @tutorial.errors
end

def destroy
Expand All @@ -32,7 +34,7 @@ def cancel_edit

def set_tutorial
@tutorial = Tutorial.find_by_id(params[:id])
return if @tutorial.present?
return if @tutorial
redirect_to :root, alert: I18n.t('controllers.no_tutorial')
end

Expand Down
6 changes: 4 additions & 2 deletions app/models/tutorial.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Tutorial model
class Tutorial < ApplicationRecord
belongs_to :tutor, class_name: 'User', foreign_key: 'tutor_id'
belongs_to :lecture
belongs_to :tutor, class_name: 'User', foreign_key: 'tutor_id', optional: true
belongs_to :lecture, touch: true

validates :title, uniqueness: { scope: [:lecture_id] }
end
2 changes: 1 addition & 1 deletion app/views/lectures/edit/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<%= render partial: 'lectures/edit/organizational_concept',
locals: { lecture: lecture,
inspection: inspection } %>
<%= render partial: 'lectures/edit/exercise_classes',
<%= render partial: 'lectures/edit/tutorials',
locals: { lecture: lecture,
inspection: inspection } %>
<%= render partial: 'lectures/edit/announcements',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="row">
<div class="col-12">
<h5>
<%= t('basics.exercise_classes') %>
<%= t('basics.tutorials') %>
</h5>
</div>
</div>
Expand All @@ -27,8 +27,10 @@
remote: true %>
</div>
</div>
<div class="row border border-botttom py-2 mx-3"
<div class="list-group">
<div class="list-group-item"
id="tutorialListHeader">
<div class="row">
<div class="col-4">
<h6>
<%= t('basics.title') %>
Expand All @@ -45,6 +47,7 @@
</h6>
</div>
</div>
</div>
<% lecture.tutorials.each do |t| %>
<%= render partial: 'tutorials/row',
locals: { tutorial: t } %>
Expand Down
16 changes: 10 additions & 6 deletions app/views/tutorials/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<%= form_with model: tutorial do |f| %>
<div class="form-row tutorialRow py-2 mx-3 border"
<div class="list-group-item tutorialRow"
data-id="<%= tutorial.id.to_i %>">
<%= form_with model: tutorial do |f| %>
<div class="form-row">
<div class="form-group col-4">
<%= f.text_field :title,
class: 'form-control' %>
<div class="invalid-feedback"
id="tutorial-title-error">
</div>
</div>
<div class="form-group col-4">
<%= f.select :tutor_id,
options_for_select(User.select_editors, tutorial.tutor_id),
{},
options_for_select(User.select_editors, tutorial&.tutor_id),
{ prompt: t('basics.select') },
{ class: 'form-control' } %>
</div>
<div class="form-group col-4">
Expand All @@ -21,8 +25,7 @@
remote: true %>
<% else %>
<button class="btn btn-sm btn-secondary"
id="cancelNewTutorial"
role="button">
id="cancelNewTutorial">
<%= t('buttons.cancel') %>
</button>
<% end %>
Expand All @@ -31,3 +34,4 @@
<%= f.hidden_field :lecture_id,
value: tutorial.lecture.id %>
<% end %>
</div>
7 changes: 5 additions & 2 deletions app/views/tutorials/_row.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<div class="row tutorialRow py-2 mx-3 border" data-id="<%= tutorial.id.to_i %>">
<div class="list-group-item tutorialRow"
data-id="<%= tutorial.id.to_i %>">
<div class="row">
<div class="col-4">
<%= tutorial.title %>
</div>
<div class="col-4">
<%= tutorial.tutor.name %>
<%= tutorial.tutor&.name %>
</div>
<div class="col-4">
<%= link_to edit_tutorial_path(tutorial),
Expand All @@ -29,4 +31,5 @@
</i>
<% end %>
</div>
</div>
</div>
16 changes: 15 additions & 1 deletion app/views/tutorials/create.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
<% if @errors.present? %>
# clean up from previous error messages
$('#tutorial_title').removeClass('is-invalid')
$('#tutorial-title-error').empty()

# display error message
<% if @errors[:title].present? %>
$('#tutorial-title-error')
.append('<%= @errors[:title].join(" ") %>').show()
$('#tutorial_title').addClass('is-invalid')
<% end %>

<% else %>
$('.tutorialRow[data-id="0"')
.replaceWith('<%= j render partial: "tutorials/row",
locals: { tutorial: @tutorial } %>')
locals: { tutorial: @tutorial } %>')
<% end %>
16 changes: 15 additions & 1 deletion app/views/tutorials/update.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
<% if @errors.present? %>
# clean up from previous error messages
$('#tutorial_title').removeClass('is-invalid')
$('#tutorial-title-error').empty()

# display error message
<% if @errors[:title].present? %>
$('#tutorial-title-error')
.append('<%= @errors[:title].join(" ") %>').show()
$('#tutorial_title').addClass('is-invalid')
<% end %>

<% else %>
$('.tutorialRow[data-id="<%= @tutorial.id %>')
.replaceWith('<%= j render partial: "tutorials/row",
locals: { tutorial: @tutorial } %>')
locals: { tutorial: @tutorial } %>')
<% end %>
8 changes: 7 additions & 1 deletion config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2486,8 +2486,8 @@ de:
subscribe: 'abonnieren'
programs: 'Studiengänge'
visibility: 'Zusatzmaterialien'
exercise_classes: 'Übungsbetrieb'
tutor: 'TutorIn'
tutorials: 'Tutorien'
access:
unpublished: 'unveröffentlicht'
all: 'frei'
Expand Down Expand Up @@ -2811,6 +2811,12 @@ de:
season:
inclusion: 'Ungültiger Typ.'
taken: 'Dieses Semester existiert bereits.'
tutorial:
attributes:
title:
taken: >
Dieser Tutoriumstitel ist für diese Veranstaltung bereits
vergeben.
user:
attributes:
courses:
Expand Down
7 changes: 6 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ en:
subscribe: 'Subscribe'
programs: 'Programs'
visibility: 'Additional material'
exercise_classes: 'Exercise Classes'
tutorials: 'Tutorials'
tutor: 'Tutor'
warnings:
save_before: 'Before you continue, you need to save or discard your changes'
Expand Down Expand Up @@ -2666,6 +2666,11 @@ en:
season:
inclusion: 'Not a valid type.'
taken: 'This term already exists.'
tutorial:
attributes:
title:
taken: >
This tutorium title is already taken for this event series.
user:
attributes:
courses:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20200909104314_change_column_null_for.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeColumnNullFor < ActiveRecord::Migration[6.0]
def change
change_column_null :tutorials, :tutor_id, true
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_09_08_112756) do
ActiveRecord::Schema.define(version: 2020_09_09_104314) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -705,7 +705,7 @@

create_table "tutorials", force: :cascade do |t|
t.text "title"
t.bigint "tutor_id", null: false
t.bigint "tutor_id"
t.bigint "lecture_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
Expand Down

0 comments on commit c379583

Please sign in to comment.