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 6 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
11 changes: 11 additions & 0 deletions app/controllers/training_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Expand Up @@ -11,4 +11,12 @@ def index
def show
@training_module = TrainingModule.find_by(slug: params[:module_id])
end

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)
raise ActionController::RoutingError, 'library not found' unless training_library
redirect_to "/training/#{training_library.slug}/#{training_module.slug}"
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
67 changes: 67 additions & 0 deletions spec/controllers/training_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,71 @@
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
Copy link
Member Author

Choose a reason for hiding this comment

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

Added this because I want to test the behavior for an orphan slide (one that doesn't belong to any module).