Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to reach specific training modules and slides via ID numbers #5502

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/training_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -59,6 +60,15 @@ def reload
render plain: e.message
end

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to have a module that is not in any library, and it would render if you visited a path that had any valid training library slug. (You can view a module from one library even if you substitute the slug from another library.)

It would be good to find the library as you do here, but if no library is found, it would be better to use TrainingLibrary.first as a fallback instead of throwing a 404.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm interesting. That behavior is not so intuitive I guess.

I changed the logic for the controllers and updated specs on fbb894f

Note that the new definitions may result in failure if no library exists. In such a scenario, calling TrainingLibrary.first would return nil, causing an error when attempting to access the slug.
Not sure if a world with no libraries makes sense, or it is not worth considering this case and we can suppose TrainingLibrary.first is always well defined.
It looks like modules and slides render even using a non-existing library slug, so we could use a literal string
'mylibrary' as default library slug value if no libraries at all.
What's your opinion here?

redirect_to "/training/#{training_library.slug}/#{training_module.slug}/#{training_slide.slug}"
end

private

def add_training_root_breadcrumb
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/training_modules_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class TrainingModulesController < ApplicationController
include TrainingHelper
respond_to :json

def index
Expand All @@ -11,4 +12,11 @@ def index
def show
@training_module = TrainingModule.find_by(slug: params[:module_id])
end

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
redirect_to "/training/#{training_library.slug}/#{training_module.slug}"
end
end
28 changes: 28 additions & 0 deletions app/helpers/training_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both of these helper methods would make more sense as instance methods of TrainingSlide and TrainingModule respectively.

Copy link
Member Author

@gabina gabina Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, agreed. I made all your suggested changes on 54f8848

TrainingLibrary.all.find_each do |library|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this method, I would suggest implementing TrainingLibrary#training_module_slugs (for example:

  def training_module_slugs
    categories.map do |cat|
      cat['modules'].map { |mod| mod['slug'] }
    end.flatten
  end

)

and then using that to implement TrainingModule#library:

TrainingLibrary.all.detect { |tl| tl.training_module_slugs.include? slug }

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a TrainingSlide instance method, you could implement this as

TrainingModule.all.detect { |tm| tm.slide_slugs.include? slug }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much more idiomatic, I like 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
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@
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'

# To find individual slides by id
get 'find_training_slide/:slide_id' => 'training#find_slide'

# Misc
# get 'courses' => 'courses#index'
Expand Down
68 changes: 68 additions & 0 deletions spec/controllers/training_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,72 @@
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

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
3 changes: 3 additions & 0 deletions spec/factories/training_slides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading