Skip to content

Commit

Permalink
Define find_or_default_library instance method to avoid code duplicat…
Browse files Browse the repository at this point in the history
…ion in controllers
  • Loading branch information
gabina committed Oct 10, 2023
1 parent 54f8848 commit 3c2fadc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/controllers/training_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/training_modules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions app/models/training_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3c2fadc

Please sign in to comment.