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 all 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
9 changes: 9 additions & 0 deletions app/controllers/training_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def reload
render plain: e.message
end

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 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

private

def add_training_root_breadcrumb
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/training_modules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ def index
def show
@training_module = TrainingModule.find_by(slug: params[:module_id])
end

def find
training_module = TrainingModule.find(params[:module_id])
# 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_library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions app/models/training_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,17 @@ 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

# 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
Comment on lines +162 to +166
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.

I created this method only to avoid code duplication in controllers.

Alternatively, I guess I could define just something like this:

  # Returns the first library that has a category including the module slug,
  # or a default library if it is not found.
  def find_library_by_slug_or_default
    TrainingLibrary.all.detect(-> { TrainingLibrary.first }) { |tl| tl.training_module_slugs.include? slug }
  end


class ModuleNotFound < StandardError; end
end
6 changes: 6 additions & 0 deletions app/models/training_slide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
69 changes: 69 additions & 0 deletions spec/controllers/training_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,73 @@
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 'uses a default library to build the route' do
subject
expect(response).to redirect_to('/training/instructors/editing-basics')
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) { 103 }

it 'redirects to a training slide page' do
subject
expect(response).to redirect_to('/training/students/wikipedia-essentials/five-pillars')
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 'uses a default library to build the route' do
subject
expect(response).to redirect_to('/training/instructors/editing-basics/welcome-students')
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