From 3c2fadc3f52e98c667f7e40620e7969bd7e10f6a Mon Sep 17 00:00:00 2001 From: gabina Date: Tue, 10 Oct 2023 12:33:46 -0300 Subject: [PATCH] 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