-
Notifications
You must be signed in to change notification settings - Fork 641
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
Changes from 9 commits
dcfd285
75129a5
13705ae
edc0873
a626846
ef2c7c5
5918b20
a1d2a54
dfcfd8c
fbb894f
54f8848
3c2fadc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
) and then using that to implement TrainingModule#library:
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a TrainingSlide instance method, you could implement this as
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 returnnil
, 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?