-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Fix titles in breadcrumbs (particularly belongs_to) #2601
Fix titles in breadcrumbs (particularly belongs_to) #2601
Conversation
module ActiveAdmin
module ViewHelpers
module BreadcrumbHelper
# Returns an array of links to use in a breadcrumb
def breadcrumb_links(path = request.path)
parts = path[1..-1].split('/') # remove leading "/" and split up the URL
parts.pop # remove last since it's used as the page title
parts.each_with_index.map do |part, index|
# 1. try using `display_name` if we can locate a DB object
# 2. try using the model name translation
# 3. default to calling `titlecase` on the URL fragment
if part =~ /\A(\d+|[a-f0-9]{24})\z/ && parts[index-1] I'm confused about what's happening here. If we grab the config = active_admin_config.belongs_to_config.try(:target) || active_admin_config
name = display_name config.find_resource(part)
end
name ||= I18n.t "activerecord.models.#{part.singularize}", :count => 1.1, :default => part.titlecase
link_to name, '/' + parts[0..index].join('/')
end
end
end
end
end |
Is there a way to check if the |
You may be able to compare it to the routes defined in resource/routes.rb, if it is a resource config object. |
I've got this tested manually and it appears to work, but I can't get the specs to pass. I'm pretty new to using test doubles, and here's the error I get:
It's the same for each test lines 157-176. |
That error is being raised because the active admin config double does not respond to the FYI, naming doubles is helpful in finding these types of errors: double("user", display_name: "User 1") |
I think the intent is for the active_admin_config to be an alias of post_config. With this change all the tests pass: let :active_admin_config do
post_config
end |
Thanks @zorab47, that seems to have fixed it. |
if part =~ /\A(\d+|[a-f0-9]{24})\z/ && parts[index-1] | ||
config = active_admin_config.belongs_to_config.try(:target) || active_admin_config | ||
name = display_name config.find_resource(part) | ||
if part =~ /\A(\d+|[a-f0-9]{24})\z/ && route_key = parts[index-1] |
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.
This line has become very complex. How would you describe its purpose and can it be factored out into a predicate method?
Is it checking for parent components of the URL?
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.
Basically, if part
is an id
-like parameter, and the previous part exists (if so, it is the route key), then we have a chain we can use to figure out what the resource is.
I could just as easily not do the assignment in the conditional, changing it to
if part =~ /\A(\d+|[a-f0-9]{24})\z/ && parts[index-1]
route_key = parts[index-1]
# ...
or
config = parent_config if parent_config && parent_config.resource_name.route_key == parts[index-1]
There's a lot of lines changed in the tests, particularly switching from posts to users. What do those changes accomplish? |
Mostly keeps the test running down the same chain of associations. Before the tests were only checking for the first set of associations in the breadcrumbs. The new ones assume that a chain like |
Just want to add my +1 to this PR. I kept getting a I also agree that the Thanks to all for your hard work! |
Anything need changing in this PR @shekibobo? |
Well, I could move the variable assignment out of that first line, but aside from that coding style issue and a squash, I'm good to go on this one. |
I was considering changing it to: if part =~ /\A(\d+|[a-f0-9]{24})\z/ && parts[index-1]
parent = active_admin_config.belongs_to_config.try :target
config = parent && parent.resource_name.route_key == parts[index-1] ? parent : active_admin_config
name = display_name config.find_resource part
end But then you have one really long line in the middle :/ |
That was kind of the struggle I was having. It's either multiple lines or longer lines. Not sure we can shortcut the logic anywhere, really. Either way should work, though. Your call. |
Hmm, yeah let's go the longer line route. I think it more clearly describes what's happening, even if style guides might frown on it. |
Travis sent me an email saying the build was broken before they all passed, so I rebased on master to be sure. If it passes, I'll squash and we should be good to go. |
Squashed 💚 |
Fix titles in breadcrumbs (particularly belongs_to)
Found with `codespell` The typo was there before activeadmin#2601, but according to activeadmin#2601 the intent is to test `/posts/1` and not `comments` anymore
Found with `codespell` The typo was there before activeadmin#2601, but according to activeadmin#2601 the intent is to test `/posts/1` and not `comments` anymore
Fixes #2132. For realz.