From dcfd285eab03939165ef51ee2a87e6491077d832 Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 8 Oct 2023 00:34:05 -0300 Subject: [PATCH 01/12] Add a new find_training_module route and its corresponding controller to be able to reach a training module by their id --- app/controllers/training_modules_controller.rb | 13 +++++++++++++ config/routes.rb | 2 ++ 2 files changed, 15 insertions(+) diff --git a/app/controllers/training_modules_controller.rb b/app/controllers/training_modules_controller.rb index a921fe962b..dfc3bf72dd 100644 --- a/app/controllers/training_modules_controller.rb +++ b/app/controllers/training_modules_controller.rb @@ -11,4 +11,17 @@ def index def show @training_module = TrainingModule.find_by(slug: params[:module_id]) end + + def find + training_module = TrainingModule.find_by(id: params[:module_id]) + + raise ActiveRecord::RecordNotFound unless training_module + + training_module_slug = "%slug: " + training_module.slug + "\n%" + training_library = TrainingLibrary.find_by("categories LIKE ?", training_module_slug) + + raise ActiveRecord::RecordNotFound unless training_library + + redirect_to "/training/#{training_library.slug}/#{training_module.slug}" + end end diff --git a/config/routes.rb b/config/routes.rb index 71de99b67e..a021f0b8a7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -328,6 +328,8 @@ get 'training_modules' => 'training_modules#index' get 'training_module' => 'training_modules#show' + # To find training modules by id + get 'find_training_module/:module_id' => 'training_modules#find' # Misc # get 'courses' => 'courses#index' From 75129a562881e26ed6935af8101635de2a8a8f31 Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 8 Oct 2023 01:39:01 -0300 Subject: [PATCH 02/12] Small stylistic change in controller --- app/controllers/training_modules_controller.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/app/controllers/training_modules_controller.rb b/app/controllers/training_modules_controller.rb index dfc3bf72dd..679ebe17f4 100644 --- a/app/controllers/training_modules_controller.rb +++ b/app/controllers/training_modules_controller.rb @@ -13,15 +13,10 @@ def show end def find - training_module = TrainingModule.find_by(id: params[:module_id]) - - raise ActiveRecord::RecordNotFound unless training_module - - training_module_slug = "%slug: " + training_module.slug + "\n%" + training_module = TrainingModule.find(params[:module_id]) + training_module_slug = "%slug: #{training_module.slug}\n%" training_library = TrainingLibrary.find_by("categories LIKE ?", training_module_slug) - raise ActiveRecord::RecordNotFound unless training_library - redirect_to "/training/#{training_library.slug}/#{training_module.slug}" end end From 13705ae62ee73d578f7af0627e6d1226edbbd820 Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 8 Oct 2023 03:35:33 -0300 Subject: [PATCH 03/12] Minor change in controller and addition of new spec for it --- .../training_modules_controller.rb | 4 +-- spec/controllers/training_controller_spec.rb | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/controllers/training_modules_controller.rb b/app/controllers/training_modules_controller.rb index 679ebe17f4..a5f4e032f7 100644 --- a/app/controllers/training_modules_controller.rb +++ b/app/controllers/training_modules_controller.rb @@ -14,9 +14,9 @@ def show def find training_module = TrainingModule.find(params[:module_id]) - training_module_slug = "%slug: #{training_module.slug}\n%" + training_module_slug = "%- slug: #{training_module.slug}\n%" training_library = TrainingLibrary.find_by("categories LIKE ?", training_module_slug) - raise ActiveRecord::RecordNotFound unless training_library + raise ActionController::RoutingError, 'library not found' unless training_library redirect_to "/training/#{training_library.slug}/#{training_module.slug}" end end diff --git a/spec/controllers/training_controller_spec.rb b/spec/controllers/training_controller_spec.rb index 9e3ba520ab..ac9455d64f 100644 --- a/spec/controllers/training_controller_spec.rb +++ b/spec/controllers/training_controller_spec.rb @@ -140,4 +140,33 @@ end end end + + describe '#find' do + subject { get "/find_training_module/#{module_id}" } + + context 'module_id is found' do + let(:module_id) { 12 } + + it 'redirects to a training module page' do + subject + expect(response).to redirect_to("/training/students/peer-review") + end + end + + context 'module_id is found but it is in no library' do + let(:module_id) { 2 } + + it 'raises a routing error' do + expect { subject }.to raise_error ActionController::RoutingError + end + end + + context 'module_id is not found' do + let(:module_id) { 128456 } + + it 'raises a module not found error' do + expect { subject }.to raise_error ActiveRecord::RecordNotFound + end + end + end end From edc0873cc6956030279d3c8eab594f22f7fb5fdc Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 8 Oct 2023 17:05:14 -0300 Subject: [PATCH 04/12] Add new find_training_slide route and its corresponding controller to be able to reach individual slides by id --- app/controllers/training_controller.rb | 11 +++++++++++ config/routes.rb | 3 +++ 2 files changed, 14 insertions(+) diff --git a/app/controllers/training_controller.rb b/app/controllers/training_controller.rb index f92afcc69e..b2ee8dc8c6 100644 --- a/app/controllers/training_controller.rb +++ b/app/controllers/training_controller.rb @@ -59,6 +59,17 @@ def reload render plain: e.message end + def find_slide + training_slide = TrainingSlide.find(params[:slide_id]) + training_slide_slug = "%- #{training_slide.slug}\n%" + training_module = TrainingModule.find_by("slide_slugs LIKE ?", training_slide_slug) + raise ActionController::RoutingError, 'module not found' unless training_module + training_module_slug = "%- slug: #{training_module.slug}\n%" + training_library = TrainingLibrary.find_by("categories LIKE ?", training_module_slug) + raise ActionController::RoutingError, 'library not found' unless training_library + redirect_to "/training/#{training_library.slug}/#{training_module.slug}/#{training_slide.slug}" + end + private def add_training_root_breadcrumb diff --git a/config/routes.rb b/config/routes.rb index a021f0b8a7..c705fdf84c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -331,6 +331,9 @@ # To find training modules by id get 'find_training_module/:module_id' => 'training_modules#find' + # To find individual slides by id + get 'find_training_slide/:slide_id' => 'training#find_slide' + # Misc # get 'courses' => 'courses#index' get 'explore' => 'explore#index' From a626846848965696a378c138fdfcbbcdb22d28ba Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 8 Oct 2023 17:06:27 -0300 Subject: [PATCH 05/12] Add spec to test new find_slide method in training controller --- spec/controllers/training_controller_spec.rb | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/spec/controllers/training_controller_spec.rb b/spec/controllers/training_controller_spec.rb index ac9455d64f..52606d87c5 100644 --- a/spec/controllers/training_controller_spec.rb +++ b/spec/controllers/training_controller_spec.rb @@ -169,4 +169,42 @@ end end end + + describe '#find_slide' do + subject { get "/find_training_slide/#{slide_id}" } + + context 'slide_id is found' do + let(:slide_id) { 332 } + + it 'redirects to a training slide page' do + subject + expect(response).to redirect_to("/training/instructors/new-instructor-orientation/new-instructor-orientation-complete") + end + end + + context 'slide_id is found but it is in no module' do + let(:slide) { create(:training_slide) } + let(:slide_id) { slide.id } + + it 'raises a routing error' do + expect { subject }.to raise_error ActionController::RoutingError, "module not found" + end + end + + context 'slide_id is found but its module is in no library' do + let(:slide_id) { 201 } + + it 'raises a routing error' do + expect { subject }.to raise_error ActionController::RoutingError, "library not found" + end + end + + context 'slide_id is not found' do + let(:slide_id) { 128456 } + + it 'raises a module not found error' do + expect { subject }.to raise_error ActiveRecord::RecordNotFound + end + end + end end From ef2c7c5267e8ed36b6093273b037ed38cfb576e3 Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 8 Oct 2023 17:07:17 -0300 Subject: [PATCH 06/12] Upload missing file from previous commit --- spec/factories/training_slides.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/factories/training_slides.rb b/spec/factories/training_slides.rb index 113e1682c3..992f395a1e 100644 --- a/spec/factories/training_slides.rb +++ b/spec/factories/training_slides.rb @@ -19,5 +19,8 @@ FactoryBot.define do factory :training_slide do + id { 456875} + title { 'How to create a slide' } + slug { 'how-to-create-a-slide' } end end From 5918b20123991fa1ec0d7c4a4c439fe4fa016d9d Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 8 Oct 2023 23:17:24 -0300 Subject: [PATCH 07/12] Add a TrainingHelper class to include functions to find 1) a libary from module slug, and 2) a module from slide slug --- app/controllers/training_controller.rb | 7 ++--- .../training_modules_controller.rb | 4 +-- app/helpers/training_helper.rb | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 app/helpers/training_helper.rb diff --git a/app/controllers/training_controller.rb b/app/controllers/training_controller.rb index b2ee8dc8c6..ef8ff57321 100644 --- a/app/controllers/training_controller.rb +++ b/app/controllers/training_controller.rb @@ -5,6 +5,7 @@ require_dependency "#{Rails.root}/lib/training/training_resource_query_object" class TrainingController < ApplicationController + include TrainingHelper layout 'training' before_action :init_query_object, only: :index @@ -61,11 +62,9 @@ def reload def find_slide training_slide = TrainingSlide.find(params[:slide_id]) - training_slide_slug = "%- #{training_slide.slug}\n%" - training_module = TrainingModule.find_by("slide_slugs LIKE ?", training_slide_slug) + training_module = find_module_from_slide_slug(training_slide.slug) raise ActionController::RoutingError, 'module not found' unless training_module - training_module_slug = "%- slug: #{training_module.slug}\n%" - training_library = TrainingLibrary.find_by("categories LIKE ?", training_module_slug) + training_library = find_library_from_module_slug(training_module.slug) raise ActionController::RoutingError, 'library not found' unless training_library redirect_to "/training/#{training_library.slug}/#{training_module.slug}/#{training_slide.slug}" end diff --git a/app/controllers/training_modules_controller.rb b/app/controllers/training_modules_controller.rb index a5f4e032f7..0080fc3588 100644 --- a/app/controllers/training_modules_controller.rb +++ b/app/controllers/training_modules_controller.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class TrainingModulesController < ApplicationController + include TrainingHelper respond_to :json def index @@ -14,8 +15,7 @@ def show def find training_module = TrainingModule.find(params[:module_id]) - training_module_slug = "%- slug: #{training_module.slug}\n%" - training_library = TrainingLibrary.find_by("categories LIKE ?", training_module_slug) + training_library = find_library_from_module_slug(training_module.slug) raise ActionController::RoutingError, 'library not found' unless training_library redirect_to "/training/#{training_library.slug}/#{training_module.slug}" end diff --git a/app/helpers/training_helper.rb b/app/helpers/training_helper.rb new file mode 100644 index 0000000000..a0b4e2b653 --- /dev/null +++ b/app/helpers/training_helper.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +#= Helpers for training views +module TrainingHelper + # Given a module slug, it returns the first library that has a + # category including that module slug. It returns nil if no such + # library is found. + def find_library_from_module_slug(module_slug) + TrainingLibrary.all.find_each do |library| + library.categories.each do |category| + if category.key?('modules') + category['modules'].each do |mod| + return library if mod['slug'] == module_slug + end + end + end + end + end + + # Given a slide slug, it returns the first module including it. + # It returns nil if no such module is found. + def find_module_from_slide_slug(slide_slug_to_find) + TrainingModule.all.find_each do |mod| + mod.slide_slugs.each do |slide_slug| + return mod if slide_slug == slide_slug_to_find + end + end + end +end \ No newline at end of file From a1d2a542dbc4c70c7dd66afad4a266471567e6a7 Mon Sep 17 00:00:00 2001 From: gabina Date: Sun, 8 Oct 2023 23:20:53 -0300 Subject: [PATCH 08/12] Minor stylistic change --- app/helpers/training_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/training_helper.rb b/app/helpers/training_helper.rb index a0b4e2b653..cc015d6e50 100644 --- a/app/helpers/training_helper.rb +++ b/app/helpers/training_helper.rb @@ -26,4 +26,4 @@ def find_module_from_slide_slug(slide_slug_to_find) end end end -end \ No newline at end of file +end From dfcfd8cae5191cc43d170ea75e39047f0cdd95fb Mon Sep 17 00:00:00 2001 From: gabina Date: Mon, 9 Oct 2023 19:30:42 -0300 Subject: [PATCH 09/12] Apply RuboCop on the changed files --- app/helpers/training_helper.rb | 7 +++---- spec/controllers/training_controller_spec.rb | 9 +++++---- spec/factories/training_slides.rb | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/helpers/training_helper.rb b/app/helpers/training_helper.rb index cc015d6e50..d126bbd2a0 100644 --- a/app/helpers/training_helper.rb +++ b/app/helpers/training_helper.rb @@ -8,10 +8,9 @@ module TrainingHelper def find_library_from_module_slug(module_slug) TrainingLibrary.all.find_each do |library| library.categories.each do |category| - if category.key?('modules') - category['modules'].each do |mod| - return library if mod['slug'] == module_slug - end + next unless category.key?('modules') + category['modules'].each do |mod| + return library if mod['slug'] == module_slug end end end diff --git a/spec/controllers/training_controller_spec.rb b/spec/controllers/training_controller_spec.rb index 52606d87c5..6fb9bba3f6 100644 --- a/spec/controllers/training_controller_spec.rb +++ b/spec/controllers/training_controller_spec.rb @@ -149,7 +149,7 @@ it 'redirects to a training module page' do subject - expect(response).to redirect_to("/training/students/peer-review") + expect(response).to redirect_to('/training/students/peer-review') end end @@ -178,7 +178,8 @@ it 'redirects to a training slide page' do subject - expect(response).to redirect_to("/training/instructors/new-instructor-orientation/new-instructor-orientation-complete") + expect(response).to redirect_to('/training/instructors/new-instructor-orientation/' \ + 'new-instructor-orientation-complete') end end @@ -187,7 +188,7 @@ let(:slide_id) { slide.id } it 'raises a routing error' do - expect { subject }.to raise_error ActionController::RoutingError, "module not found" + expect { subject }.to raise_error ActionController::RoutingError, 'module not found' end end @@ -195,7 +196,7 @@ let(:slide_id) { 201 } it 'raises a routing error' do - expect { subject }.to raise_error ActionController::RoutingError, "library not found" + expect { subject }.to raise_error ActionController::RoutingError, 'library not found' end end diff --git a/spec/factories/training_slides.rb b/spec/factories/training_slides.rb index 992f395a1e..128f720502 100644 --- a/spec/factories/training_slides.rb +++ b/spec/factories/training_slides.rb @@ -19,7 +19,7 @@ FactoryBot.define do factory :training_slide do - id { 456875} + id { 456875 } title { 'How to create a slide' } slug { 'how-to-create-a-slide' } end From fbb894f8765a8f9a1537ed3ea056bdcdcc714eea Mon Sep 17 00:00:00 2001 From: gabina Date: Mon, 9 Oct 2023 23:46:57 -0300 Subject: [PATCH 10/12] Stop raising RoutingError for modules that are not in any library. Use TrainingLibrary.first as a default library instead. --- app/controllers/training_controller.rb | 4 ++-- app/controllers/training_modules_controller.rb | 4 ++-- spec/controllers/training_controller_spec.rb | 15 ++++++++------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/controllers/training_controller.rb b/app/controllers/training_controller.rb index ef8ff57321..d8b3088e3a 100644 --- a/app/controllers/training_controller.rb +++ b/app/controllers/training_controller.rb @@ -64,8 +64,8 @@ def find_slide training_slide = TrainingSlide.find(params[:slide_id]) training_module = find_module_from_slide_slug(training_slide.slug) raise ActionController::RoutingError, 'module not found' unless training_module - training_library = find_library_from_module_slug(training_module.slug) - raise ActionController::RoutingError, 'library not found' unless training_library + # Use the specific training library for the module, or a default library if it is not found + training_library = find_library_from_module_slug(training_module.slug) || TrainingLibrary.first redirect_to "/training/#{training_library.slug}/#{training_module.slug}/#{training_slide.slug}" end diff --git a/app/controllers/training_modules_controller.rb b/app/controllers/training_modules_controller.rb index 0080fc3588..da7a5ee612 100644 --- a/app/controllers/training_modules_controller.rb +++ b/app/controllers/training_modules_controller.rb @@ -15,8 +15,8 @@ def show def find training_module = TrainingModule.find(params[:module_id]) - training_library = find_library_from_module_slug(training_module.slug) - raise ActionController::RoutingError, 'library not found' unless training_library + # Use the specific training library for the module, or a default library if it is not found + training_library = find_library_from_module_slug(training_module.slug) || TrainingLibrary.first redirect_to "/training/#{training_library.slug}/#{training_module.slug}" end end diff --git a/spec/controllers/training_controller_spec.rb b/spec/controllers/training_controller_spec.rb index 6fb9bba3f6..2bed9106ac 100644 --- a/spec/controllers/training_controller_spec.rb +++ b/spec/controllers/training_controller_spec.rb @@ -156,8 +156,9 @@ context 'module_id is found but it is in no library' do let(:module_id) { 2 } - it 'raises a routing error' do - expect { subject }.to raise_error ActionController::RoutingError + it 'uses a default library to build the route' do + subject + expect(response).to redirect_to('/training/instructors/editing-basics') end end @@ -174,12 +175,11 @@ subject { get "/find_training_slide/#{slide_id}" } context 'slide_id is found' do - let(:slide_id) { 332 } + let(:slide_id) { 103 } it 'redirects to a training slide page' do subject - expect(response).to redirect_to('/training/instructors/new-instructor-orientation/' \ - 'new-instructor-orientation-complete') + expect(response).to redirect_to('/training/students/wikipedia-essentials/five-pillars') end end @@ -195,8 +195,9 @@ context 'slide_id is found but its module is in no library' do let(:slide_id) { 201 } - it 'raises a routing error' do - expect { subject }.to raise_error ActionController::RoutingError, 'library not found' + it 'uses a default library to build the route' do + subject + expect(response).to redirect_to('/training/instructors/editing-basics/welcome-students') end end From 54f8848c51f076f94f27507d07ab1afa2f82e037 Mon Sep 17 00:00:00 2001 From: gabina Date: Tue, 10 Oct 2023 12:25:37 -0300 Subject: [PATCH 11/12] Remove TrainingHelper module and define methods to find module and library from slug as instance methods instead --- app/controllers/training_controller.rb | 5 ++-- .../training_modules_controller.rb | 3 +- app/helpers/training_helper.rb | 28 ------------------- app/models/training_library.rb | 6 ++++ app/models/training_module.rb | 6 ++++ app/models/training_slide.rb | 6 ++++ 6 files changed, 21 insertions(+), 33 deletions(-) delete mode 100644 app/helpers/training_helper.rb diff --git a/app/controllers/training_controller.rb b/app/controllers/training_controller.rb index d8b3088e3a..dd0b1cc9ea 100644 --- a/app/controllers/training_controller.rb +++ b/app/controllers/training_controller.rb @@ -5,7 +5,6 @@ require_dependency "#{Rails.root}/lib/training/training_resource_query_object" class TrainingController < ApplicationController - include TrainingHelper layout 'training' before_action :init_query_object, only: :index @@ -62,10 +61,10 @@ def reload def find_slide training_slide = TrainingSlide.find(params[:slide_id]) - training_module = find_module_from_slide_slug(training_slide.slug) + training_module = training_slide.find_module_by_slug raise ActionController::RoutingError, 'module not found' unless training_module # Use the specific training library for the module, or a default library if it is not found - training_library = find_library_from_module_slug(training_module.slug) || TrainingLibrary.first + training_library = training_module.find_library_by_slug || TrainingLibrary.first redirect_to "/training/#{training_library.slug}/#{training_module.slug}/#{training_slide.slug}" end diff --git a/app/controllers/training_modules_controller.rb b/app/controllers/training_modules_controller.rb index da7a5ee612..4962407199 100644 --- a/app/controllers/training_modules_controller.rb +++ b/app/controllers/training_modules_controller.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true class TrainingModulesController < ApplicationController - include TrainingHelper respond_to :json def index @@ -16,7 +15,7 @@ def show def find training_module = TrainingModule.find(params[:module_id]) # Use the specific training library for the module, or a default library if it is not found - training_library = find_library_from_module_slug(training_module.slug) || TrainingLibrary.first + training_library = training_module.find_library_by_slug || TrainingLibrary.first redirect_to "/training/#{training_library.slug}/#{training_module.slug}" end end diff --git a/app/helpers/training_helper.rb b/app/helpers/training_helper.rb deleted file mode 100644 index d126bbd2a0..0000000000 --- a/app/helpers/training_helper.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -#= Helpers for training views -module TrainingHelper - # Given a module slug, it returns the first library that has a - # category including that module slug. It returns nil if no such - # library is found. - def find_library_from_module_slug(module_slug) - TrainingLibrary.all.find_each do |library| - library.categories.each do |category| - next unless category.key?('modules') - category['modules'].each do |mod| - return library if mod['slug'] == module_slug - end - end - end - end - - # Given a slide slug, it returns the first module including it. - # It returns nil if no such module is found. - def find_module_from_slide_slug(slide_slug_to_find) - TrainingModule.all.find_each do |mod| - mod.slide_slugs.each do |slide_slug| - return mod if slide_slug == slide_slug_to_find - end - end - end -end diff --git a/app/models/training_library.rb b/app/models/training_library.rb index 8df5f82ad4..2cc1240039 100644 --- a/app/models/training_library.rb +++ b/app/models/training_library.rb @@ -97,4 +97,10 @@ def translated_categories def translated(key) translations.dig(I18n.locale.to_s, key) end + + def training_module_slugs + categories.map do |cat| + cat['modules'].map { |mod| mod['slug'] } + end.flatten + end end diff --git a/app/models/training_module.rb b/app/models/training_module.rb index 1826bae857..ad99b2f533 100644 --- a/app/models/training_module.rb +++ b/app/models/training_module.rb @@ -153,5 +153,11 @@ def sandbox_location settings['sandbox_location'] end + # Returns the first library that has a category including the module slug. + # It returns nil if no such library is found. + def find_library_by_slug + TrainingLibrary.all.detect { |tl| tl.training_module_slugs.include? slug } + end + class ModuleNotFound < StandardError; end end diff --git a/app/models/training_slide.rb b/app/models/training_slide.rb index 071fa815b8..42a59a05db 100644 --- a/app/models/training_slide.rb +++ b/app/models/training_slide.rb @@ -65,4 +65,10 @@ def self.inflate(all_content, slug, wiki_page = nil) puts "There's a problem with file '#{slug}'" raise e end + + # Returns the first module including the slide slug. + # It returns nil if no such module is found. + def find_module_by_slug + TrainingModule.all.detect { |tm| tm.slide_slugs.include? slug } + end end From 3c2fadc3f52e98c667f7e40620e7969bd7e10f6a Mon Sep 17 00:00:00 2001 From: gabina Date: Tue, 10 Oct 2023 12:33:46 -0300 Subject: [PATCH 12/12] Define find_or_default_library instance method to avoid code duplication in controllers --- app/controllers/training_controller.rb | 4 ++-- app/controllers/training_modules_controller.rb | 4 ++-- app/models/training_module.rb | 6 ++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/controllers/training_controller.rb b/app/controllers/training_controller.rb index dd0b1cc9ea..569ca835ed 100644 --- a/app/controllers/training_controller.rb +++ b/app/controllers/training_controller.rb @@ -63,8 +63,8 @@ def find_slide training_slide = TrainingSlide.find(params[:slide_id]) training_module = training_slide.find_module_by_slug raise ActionController::RoutingError, 'module not found' unless training_module - # Use the specific training library for the module, or a default library if it is not found - training_library = training_module.find_library_by_slug || TrainingLibrary.first + # Use a specific training library for the module, or a default library if it is not found + training_library = training_module.find_or_default_library redirect_to "/training/#{training_library.slug}/#{training_module.slug}/#{training_slide.slug}" end diff --git a/app/controllers/training_modules_controller.rb b/app/controllers/training_modules_controller.rb index 4962407199..f7677658a7 100644 --- a/app/controllers/training_modules_controller.rb +++ b/app/controllers/training_modules_controller.rb @@ -14,8 +14,8 @@ def show def find training_module = TrainingModule.find(params[:module_id]) - # Use the specific training library for the module, or a default library if it is not found - training_library = training_module.find_library_by_slug || TrainingLibrary.first + # Use a specific training library for the module, or a default library if it is not found + training_library = training_module.find_or_default_library redirect_to "/training/#{training_library.slug}/#{training_module.slug}" end end diff --git a/app/models/training_module.rb b/app/models/training_module.rb index ad99b2f533..e6280a50bf 100644 --- a/app/models/training_module.rb +++ b/app/models/training_module.rb @@ -159,5 +159,11 @@ def find_library_by_slug TrainingLibrary.all.detect { |tl| tl.training_module_slugs.include? slug } end + # Returns a specific training library for the module, + # or a default library if it is not found. + def find_or_default_library + find_library_by_slug || TrainingLibrary.first + end + class ModuleNotFound < StandardError; end end