From c3795830972a7d9912167acf5010793ca4f1b7f9 Mon Sep 17 00:00:00 2001 From: fosterfarrell9 <28628554+fosterfarrell9@users.noreply.github.com> Date: Wed, 9 Sep 2020 13:23:03 +0200 Subject: [PATCH] continue work on restful actions for tutorials --- app/controllers/tutorials_controller.rb | 6 ++++-- app/models/tutorial.rb | 6 ++++-- app/views/lectures/edit/_form.html.erb | 2 +- ...cise_classes.html.erb => _tutorials.html.erb} | 7 +++++-- app/views/tutorials/_form.html.erb | 16 ++++++++++------ app/views/tutorials/_row.html.erb | 7 +++++-- app/views/tutorials/create.coffee | 16 +++++++++++++++- app/views/tutorials/update.coffee | 16 +++++++++++++++- config/locales/de.yml | 8 +++++++- config/locales/en.yml | 7 ++++++- .../20200909104314_change_column_null_for.rb | 5 +++++ db/schema.rb | 4 ++-- 12 files changed, 79 insertions(+), 21 deletions(-) rename app/views/lectures/edit/{_exercise_classes.html.erb => _tutorials.html.erb} (90%) create mode 100644 db/migrate/20200909104314_change_column_null_for.rb diff --git a/app/controllers/tutorials_controller.rb b/app/controllers/tutorials_controller.rb index f840cd1a2..ab3d24c5a 100644 --- a/app/controllers/tutorials_controller.rb +++ b/app/controllers/tutorials_controller.rb @@ -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 @@ -12,6 +12,7 @@ def new def create @tutorial = Tutorial.new(tutorial_params) @tutorial.save + @errors = @tutorial.errors end def edit @@ -19,6 +20,7 @@ def edit def update @tutorial.update(tutorial_params) + @errors = @tutorial.errors end def destroy @@ -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 diff --git a/app/models/tutorial.rb b/app/models/tutorial.rb index 0d79fdbac..7b4845321 100644 --- a/app/models/tutorial.rb +++ b/app/models/tutorial.rb @@ -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 diff --git a/app/views/lectures/edit/_form.html.erb b/app/views/lectures/edit/_form.html.erb index b3d659d92..f682a77b1 100644 --- a/app/views/lectures/edit/_form.html.erb +++ b/app/views/lectures/edit/_form.html.erb @@ -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', diff --git a/app/views/lectures/edit/_exercise_classes.html.erb b/app/views/lectures/edit/_tutorials.html.erb similarity index 90% rename from app/views/lectures/edit/_exercise_classes.html.erb rename to app/views/lectures/edit/_tutorials.html.erb index e9614b40f..ae9fe1ef6 100644 --- a/app/views/lectures/edit/_exercise_classes.html.erb +++ b/app/views/lectures/edit/_tutorials.html.erb @@ -8,7 +8,7 @@
- <%= t('basics.exercise_classes') %> + <%= t('basics.tutorials') %>
@@ -27,8 +27,10 @@ remote: true %> -
+
+
<%= t('basics.title') %> @@ -45,6 +47,7 @@
+
<% lecture.tutorials.each do |t| %> <%= render partial: 'tutorials/row', locals: { tutorial: t } %> diff --git a/app/views/tutorials/_form.html.erb b/app/views/tutorials/_form.html.erb index 50aa6e2be..6f4be5d42 100644 --- a/app/views/tutorials/_form.html.erb +++ b/app/views/tutorials/_form.html.erb @@ -1,14 +1,18 @@ -<%= form_with model: tutorial do |f| %> -
+<%= form_with model: tutorial do |f| %> +
<%= f.text_field :title, class: 'form-control' %> +
+
<%= 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' } %>
@@ -21,8 +25,7 @@ remote: true %> <% else %> <% end %> @@ -31,3 +34,4 @@ <%= f.hidden_field :lecture_id, value: tutorial.lecture.id %> <% end %> +
\ No newline at end of file diff --git a/app/views/tutorials/_row.html.erb b/app/views/tutorials/_row.html.erb index 96a01cc5a..027a86d35 100644 --- a/app/views/tutorials/_row.html.erb +++ b/app/views/tutorials/_row.html.erb @@ -1,9 +1,11 @@ -
+
+
<%= tutorial.title %>
- <%= tutorial.tutor.name %> + <%= tutorial.tutor&.name %>
<%= link_to edit_tutorial_path(tutorial), @@ -29,4 +31,5 @@ <% end %>
+
\ No newline at end of file diff --git a/app/views/tutorials/create.coffee b/app/views/tutorials/create.coffee index 2a736b32d..14aa45b96 100644 --- a/app/views/tutorials/create.coffee +++ b/app/views/tutorials/create.coffee @@ -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 } %>') \ No newline at end of file + locals: { tutorial: @tutorial } %>') +<% end %> \ No newline at end of file diff --git a/app/views/tutorials/update.coffee b/app/views/tutorials/update.coffee index f6a1a3518..0b2a2e9c7 100644 --- a/app/views/tutorials/update.coffee +++ b/app/views/tutorials/update.coffee @@ -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 } %>') \ No newline at end of file + locals: { tutorial: @tutorial } %>') +<% end %> \ No newline at end of file diff --git a/config/locales/de.yml b/config/locales/de.yml index c132b9db4..39fe27c20 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -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' @@ -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: diff --git a/config/locales/en.yml b/config/locales/en.yml index d991056ae..396fd0d93 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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' @@ -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: diff --git a/db/migrate/20200909104314_change_column_null_for.rb b/db/migrate/20200909104314_change_column_null_for.rb new file mode 100644 index 000000000..9ece53e4b --- /dev/null +++ b/db/migrate/20200909104314_change_column_null_for.rb @@ -0,0 +1,5 @@ +class ChangeColumnNullFor < ActiveRecord::Migration[6.0] + def change + change_column_null :tutorials, :tutor_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 5e194ae91..30d03cb28 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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