From 44a3cabe988df187755a69da3e285b4a6c85be12 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 27 Sep 2017 16:08:56 +0200 Subject: [PATCH 01/10] Scaffold exercise collection routes --- .../exercise_collections_controller.rb | 24 +++++++++++++++++++ app/policies/exercise_collection_policy.rb | 3 +++ .../exercise_collections/index.html.slim | 1 + app/views/exercise_collections/show.html.slim | 1 + config/routes.rb | 2 ++ .../exercise_collections_controller_test.rb | 14 +++++++++++ 6 files changed, 45 insertions(+) create mode 100644 app/controllers/exercise_collections_controller.rb create mode 100644 app/policies/exercise_collection_policy.rb create mode 100644 app/views/exercise_collections/index.html.slim create mode 100644 app/views/exercise_collections/show.html.slim create mode 100644 test/controllers/exercise_collections_controller_test.rb diff --git a/app/controllers/exercise_collections_controller.rb b/app/controllers/exercise_collections_controller.rb new file mode 100644 index 00000000..359ed7b8 --- /dev/null +++ b/app/controllers/exercise_collections_controller.rb @@ -0,0 +1,24 @@ +class ExerciseCollectionsController < ApplicationController + + before_action :set_exercise_collection, only: [:show] + + def index + @exercise_collections = ExerciseCollection.all + authorize! + end + + def show + end + + + private + + def set_exercise_collection + @exercise_collection = ExerciseCollection.find(params[:id]) + authorize! + end + + def authorize! + authorize(@exercise_collection || @exercise_collections) + end +end diff --git a/app/policies/exercise_collection_policy.rb b/app/policies/exercise_collection_policy.rb new file mode 100644 index 00000000..ff150290 --- /dev/null +++ b/app/policies/exercise_collection_policy.rb @@ -0,0 +1,3 @@ +class ExerciseCollectionPolicy < AdminOnlyPolicy + +end diff --git a/app/views/exercise_collections/index.html.slim b/app/views/exercise_collections/index.html.slim new file mode 100644 index 00000000..03f4829c --- /dev/null +++ b/app/views/exercise_collections/index.html.slim @@ -0,0 +1 @@ +h1 = 'ExerciseCollections#index' diff --git a/app/views/exercise_collections/show.html.slim b/app/views/exercise_collections/show.html.slim new file mode 100644 index 00000000..6ed2b5f7 --- /dev/null +++ b/app/views/exercise_collections/show.html.slim @@ -0,0 +1 @@ +h1 = 'ExerciseCollection#show' diff --git a/config/routes.rb b/config/routes.rb index eb243464..7f254f1d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -74,6 +74,8 @@ Rails.application.routes.draw do end end + resources :exercise_collections + resources :proxy_exercises do member do post :clone diff --git a/test/controllers/exercise_collections_controller_test.rb b/test/controllers/exercise_collections_controller_test.rb new file mode 100644 index 00000000..699c9271 --- /dev/null +++ b/test/controllers/exercise_collections_controller_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class ExerciseCollectionsControllerTest < ActionController::TestCase + test "should get index" do + get :index + assert_response :success + end + + test "should get show" do + get :show + assert_response :success + end + +end From 93797a665de07cfd9b8fd70a6a4375a599218798 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 27 Sep 2017 16:37:52 +0200 Subject: [PATCH 02/10] Implement index page --- .../exercise_collections_controller.rb | 2 +- .../exercise_collections/index.html.slim | 25 ++++++++++++++++++- config/locales/de.yml | 8 ++++++ config/locales/en.yml | 8 ++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/controllers/exercise_collections_controller.rb b/app/controllers/exercise_collections_controller.rb index 359ed7b8..fb4e9ba2 100644 --- a/app/controllers/exercise_collections_controller.rb +++ b/app/controllers/exercise_collections_controller.rb @@ -3,7 +3,7 @@ class ExerciseCollectionsController < ApplicationController before_action :set_exercise_collection, only: [:show] def index - @exercise_collections = ExerciseCollection.all + @exercise_collections = ExerciseCollection.all.paginate(:page => params[:page]) authorize! end diff --git a/app/views/exercise_collections/index.html.slim b/app/views/exercise_collections/index.html.slim index 03f4829c..98de8a20 100644 --- a/app/views/exercise_collections/index.html.slim +++ b/app/views/exercise_collections/index.html.slim @@ -1 +1,24 @@ -h1 = 'ExerciseCollections#index' +h1 = ExerciseCollection.model_name.human(count: 2) + +.table-responsive + table.table + thead + tr + th = t('activerecord.attributes.exercise_collections.id') + th = t('activerecord.attributes.exercise_collections.name') + th = t('activerecord.attributes.exercise_collections.updated_at') + th = t('activerecord.attributes.exercise_collections.exercises') + th colspan=3 = t('shared.actions') + tbody + - @exercise_collections.each do |collection| + tr + td = collection.id + td = link_to(collection.name, collection) + td = collection.updated_at + td = collection.exercises.size + td = link_to(t('shared.show'), collection) + td = link_to(t('shared.edit'), edit_exercise_collection_path(collection)) + td = link_to(t('shared.destroy'), collection, data: {confirm: t('shared.confirm_destroy')}, method: :delete) + += render('shared/pagination', collection: @exercise_collections) +p = render('shared/new_button', model: ExerciseCollection) \ No newline at end of file diff --git a/config/locales/de.yml b/config/locales/de.yml index 2ab23322..23232f19 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -111,6 +111,11 @@ de: name: "Name" file_type: "Dateityp" content: "Code" + exercise_collections: + id: "ID" + name: "Name" + updated_at: "Letzte Änderung" + exercises: "Aufgaben" models: code_harbor_link: one: CodeHarbor-Link @@ -127,6 +132,9 @@ de: exercise: one: Aufgabe other: Aufgaben + exercise_collection: + one: Aufgabesammlung + other: Aufgabensammlungen proxy_exercise: one: Proxy Aufgabe other: Proxy Aufgaben diff --git a/config/locales/en.yml b/config/locales/en.yml index e2cba090..543d9cd0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -111,6 +111,11 @@ en: name: "Name" file_type: "File Type" content: "Content" + exercise_collections: + id: "ID" + name: "Name" + updated_at: "Last Update" + exercises: "Exercises" models: code_harbor_link: one: CodeHarbor Link @@ -127,6 +132,9 @@ en: exercise: one: Exercise other: Exercises + exercise_collection: + one: Exercise Collection + other: Exercise Collections proxy_exercise: one: Proxy Exercise other: Proxy Exercises From f0c0621b3114d17ffd7d54f959fb53d866c6d628 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 27 Sep 2017 16:52:42 +0200 Subject: [PATCH 03/10] Implement show route --- app/models/exercise_collection.rb | 4 ++++ app/views/exercise_collections/show.html.slim | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/exercise_collection.rb b/app/models/exercise_collection.rb index 2dca0e9d..f672554d 100644 --- a/app/models/exercise_collection.rb +++ b/app/models/exercise_collection.rb @@ -2,4 +2,8 @@ class ExerciseCollection < ActiveRecord::Base has_and_belongs_to_many :exercises + def to_s + "#{I18n.t('activerecord.models.exercise_collection.one')}: #{name} (#{id})" + end + end \ No newline at end of file diff --git a/app/views/exercise_collections/show.html.slim b/app/views/exercise_collections/show.html.slim index 6ed2b5f7..89081efe 100644 --- a/app/views/exercise_collections/show.html.slim +++ b/app/views/exercise_collections/show.html.slim @@ -1 +1,11 @@ -h1 = 'ExerciseCollection#show' +h1 + = @exercise_collection + = render('shared/edit_button', object: @exercise_collection) + += row(label: 'exercise_collections.name', value: @exercise_collection.name) += row(label: 'exercise_collections.updated_at', value: @exercise_collection.updated_at) + +h4 = t('activerecord.attributes.exercise_collections.exercises') +ul.list-unstyled + - @exercise_collection.exercises.sort_by{|c| c.title}.each do |exercise| + li = exercise \ No newline at end of file From 7cb7146e7ef190f4dcc3fec2c74a615e034e77b3 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 4 Oct 2017 11:35:51 +0200 Subject: [PATCH 04/10] Implement update --- app/controllers/exercise_collections_controller.rb | 13 ++++++++++++- app/views/exercise_collections/_form.html.slim | 11 +++++++++++ app/views/exercise_collections/edit.html.slim | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 app/views/exercise_collections/_form.html.slim create mode 100644 app/views/exercise_collections/edit.html.slim diff --git a/app/controllers/exercise_collections_controller.rb b/app/controllers/exercise_collections_controller.rb index fb4e9ba2..a00f48fd 100644 --- a/app/controllers/exercise_collections_controller.rb +++ b/app/controllers/exercise_collections_controller.rb @@ -1,6 +1,7 @@ class ExerciseCollectionsController < ApplicationController + include CommonBehavior - before_action :set_exercise_collection, only: [:show] + before_action :set_exercise_collection, only: [:show, :edit, :update] def index @exercise_collections = ExerciseCollection.all.paginate(:page => params[:page]) @@ -10,6 +11,12 @@ class ExerciseCollectionsController < ApplicationController def show end + def edit + end + + def update + update_and_respond(object: @exercise_collection, params: exercise_collection_params) + end private @@ -21,4 +28,8 @@ class ExerciseCollectionsController < ApplicationController def authorize! authorize(@exercise_collection || @exercise_collections) end + + def exercise_collection_params + params[:exercise_collection].permit(:name, :exercise_ids) + end end diff --git a/app/views/exercise_collections/_form.html.slim b/app/views/exercise_collections/_form.html.slim new file mode 100644 index 00000000..a8ebf0b2 --- /dev/null +++ b/app/views/exercise_collections/_form.html.slim @@ -0,0 +1,11 @@ +- exercises = Exercise.all + += form_for(@exercise_collection, data: {exercises: exercises}, multipart: true) do |f| + = render('shared/form_errors', object: @exercise_collection) + .form-group + = f.label(:name) + = f.text_field(:name, class: 'form-control', required: true) + .form-group + = f.label(:exercises) + = f.collection_select(:exercise_ids, exercises, :id, :title, {}, {class: 'form-control', multiple: true}) + .actions = render('shared/submit_button', f: f, object: @exercise_collection) \ No newline at end of file diff --git a/app/views/exercise_collections/edit.html.slim b/app/views/exercise_collections/edit.html.slim new file mode 100644 index 00000000..4e864ab1 --- /dev/null +++ b/app/views/exercise_collections/edit.html.slim @@ -0,0 +1 @@ += render('form') \ No newline at end of file From 8e4a6946900b99485d66392271b5c58137db3db2 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 4 Oct 2017 11:37:21 +0200 Subject: [PATCH 05/10] Add title --- app/views/exercise_collections/edit.html.slim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/exercise_collections/edit.html.slim b/app/views/exercise_collections/edit.html.slim index 4e864ab1..a3694a25 100644 --- a/app/views/exercise_collections/edit.html.slim +++ b/app/views/exercise_collections/edit.html.slim @@ -1 +1,3 @@ +h1 = @exercise_collection + = render('form') \ No newline at end of file From e74c25746cb791b3ae8c4ca4dcf260b80fea0049 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 4 Oct 2017 12:40:37 +0200 Subject: [PATCH 06/10] Implement new action --- .../exercise_collections_controller.rb | 20 +++++++++++++++++-- app/views/exercise_collections/new.html.slim | 3 +++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 app/views/exercise_collections/new.html.slim diff --git a/app/controllers/exercise_collections_controller.rb b/app/controllers/exercise_collections_controller.rb index a00f48fd..4861a062 100644 --- a/app/controllers/exercise_collections_controller.rb +++ b/app/controllers/exercise_collections_controller.rb @@ -1,7 +1,7 @@ class ExerciseCollectionsController < ApplicationController include CommonBehavior - before_action :set_exercise_collection, only: [:show, :edit, :update] + before_action :set_exercise_collection, only: [:show, :edit, :update, :destroy] def index @exercise_collections = ExerciseCollection.all.paginate(:page => params[:page]) @@ -11,6 +11,22 @@ class ExerciseCollectionsController < ApplicationController def show end + def new + @exercise_collection = ExerciseCollection.new + authorize! + end + + def create + @exercise_collection = ExerciseCollection.new(exercise_collection_params) + authorize! + create_and_respond(object: @exercise_collection) + end + + def destroy + authorize! + destroy_and_respond(object: @exercise_collection) + end + def edit end @@ -30,6 +46,6 @@ class ExerciseCollectionsController < ApplicationController end def exercise_collection_params - params[:exercise_collection].permit(:name, :exercise_ids) + params[:exercise_collection].permit(:name, :exercise_ids => []) end end diff --git a/app/views/exercise_collections/new.html.slim b/app/views/exercise_collections/new.html.slim new file mode 100644 index 00000000..21a299f2 --- /dev/null +++ b/app/views/exercise_collections/new.html.slim @@ -0,0 +1,3 @@ +h1 = t('shared.new_model', model: ExerciseCollection.model_name.human) + += render('form') \ No newline at end of file From 70eb0d19b89049e16e44b69b51d5624f2f8a83a3 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 4 Oct 2017 12:47:59 +0200 Subject: [PATCH 07/10] Link to exercises contained in collections --- app/views/exercise_collections/show.html.slim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/exercise_collections/show.html.slim b/app/views/exercise_collections/show.html.slim index 89081efe..ac2d0a37 100644 --- a/app/views/exercise_collections/show.html.slim +++ b/app/views/exercise_collections/show.html.slim @@ -8,4 +8,4 @@ h1 h4 = t('activerecord.attributes.exercise_collections.exercises') ul.list-unstyled - @exercise_collection.exercises.sort_by{|c| c.title}.each do |exercise| - li = exercise \ No newline at end of file + li = link_to(exercise, exercise) \ No newline at end of file From 06e99059d494a265711b29662d9a7d7974439a07 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 4 Oct 2017 12:53:09 +0200 Subject: [PATCH 08/10] Add new lines at eof --- app/models/exercise_collection.rb | 2 +- app/views/exercise_collections/_form.html.slim | 2 +- app/views/exercise_collections/edit.html.slim | 2 +- app/views/exercise_collections/index.html.slim | 2 +- app/views/exercise_collections/new.html.slim | 2 +- app/views/exercise_collections/show.html.slim | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/exercise_collection.rb b/app/models/exercise_collection.rb index f672554d..10946962 100644 --- a/app/models/exercise_collection.rb +++ b/app/models/exercise_collection.rb @@ -6,4 +6,4 @@ class ExerciseCollection < ActiveRecord::Base "#{I18n.t('activerecord.models.exercise_collection.one')}: #{name} (#{id})" end -end \ No newline at end of file +end diff --git a/app/views/exercise_collections/_form.html.slim b/app/views/exercise_collections/_form.html.slim index a8ebf0b2..5acfd466 100644 --- a/app/views/exercise_collections/_form.html.slim +++ b/app/views/exercise_collections/_form.html.slim @@ -8,4 +8,4 @@ .form-group = f.label(:exercises) = f.collection_select(:exercise_ids, exercises, :id, :title, {}, {class: 'form-control', multiple: true}) - .actions = render('shared/submit_button', f: f, object: @exercise_collection) \ No newline at end of file + .actions = render('shared/submit_button', f: f, object: @exercise_collection) diff --git a/app/views/exercise_collections/edit.html.slim b/app/views/exercise_collections/edit.html.slim index a3694a25..187f31d2 100644 --- a/app/views/exercise_collections/edit.html.slim +++ b/app/views/exercise_collections/edit.html.slim @@ -1,3 +1,3 @@ h1 = @exercise_collection -= render('form') \ No newline at end of file += render('form') diff --git a/app/views/exercise_collections/index.html.slim b/app/views/exercise_collections/index.html.slim index 98de8a20..75a9d011 100644 --- a/app/views/exercise_collections/index.html.slim +++ b/app/views/exercise_collections/index.html.slim @@ -21,4 +21,4 @@ h1 = ExerciseCollection.model_name.human(count: 2) td = link_to(t('shared.destroy'), collection, data: {confirm: t('shared.confirm_destroy')}, method: :delete) = render('shared/pagination', collection: @exercise_collections) -p = render('shared/new_button', model: ExerciseCollection) \ No newline at end of file +p = render('shared/new_button', model: ExerciseCollection) diff --git a/app/views/exercise_collections/new.html.slim b/app/views/exercise_collections/new.html.slim index 21a299f2..0e48ccd9 100644 --- a/app/views/exercise_collections/new.html.slim +++ b/app/views/exercise_collections/new.html.slim @@ -1,3 +1,3 @@ h1 = t('shared.new_model', model: ExerciseCollection.model_name.human) -= render('form') \ No newline at end of file += render('form') diff --git a/app/views/exercise_collections/show.html.slim b/app/views/exercise_collections/show.html.slim index ac2d0a37..65c28283 100644 --- a/app/views/exercise_collections/show.html.slim +++ b/app/views/exercise_collections/show.html.slim @@ -8,4 +8,4 @@ h1 h4 = t('activerecord.attributes.exercise_collections.exercises') ul.list-unstyled - @exercise_collection.exercises.sort_by{|c| c.title}.each do |exercise| - li = link_to(exercise, exercise) \ No newline at end of file + li = link_to(exercise, exercise) From 2582f55b1eca96f729e716e24ec0bd4053ce2fbf Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 4 Oct 2017 14:12:54 +0200 Subject: [PATCH 09/10] Add navigation link to exercise collections --- app/views/application/_navigation.html.slim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/application/_navigation.html.slim b/app/views/application/_navigation.html.slim index fbeca1c3..19f10a85 100644 --- a/app/views/application/_navigation.html.slim +++ b/app/views/application/_navigation.html.slim @@ -8,7 +8,7 @@ - if current_user.admin? li = link_to(t('breadcrumbs.dashboard.show'), admin_dashboard_path) li.divider - - models = [ExecutionEnvironment, Exercise, ProxyExercise, Tag, Consumer, CodeHarborLink, ExternalUser, FileType, FileTemplate, InternalUser].sort_by { |model| model.model_name.human(count: 2) } + - models = [ExecutionEnvironment, Exercise, ExerciseCollection, ProxyExercise, Tag, Consumer, CodeHarborLink, ExternalUser, FileType, FileTemplate, InternalUser].sort_by { |model| model.model_name.human(count: 2) } - models.each do |model| - if policy(model).index? li = link_to(model.model_name.human(count: 2), send(:"#{model.model_name.collection}_path")) From ee5825c71b7c5e376e53ccd68112ffc42d08618a Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Wed, 4 Oct 2017 15:42:33 +0200 Subject: [PATCH 10/10] Order exercises in form --- app/views/exercise_collections/_form.html.slim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/exercise_collections/_form.html.slim b/app/views/exercise_collections/_form.html.slim index 5acfd466..3c336a62 100644 --- a/app/views/exercise_collections/_form.html.slim +++ b/app/views/exercise_collections/_form.html.slim @@ -1,4 +1,4 @@ -- exercises = Exercise.all +- exercises = Exercise.order(:title) = form_for(@exercise_collection, data: {exercises: exercises}, multipart: true) do |f| = render('shared/form_errors', object: @exercise_collection)