piotrmurach / loaf

Manages and displays breadcrumb trails in Rails app - lean & mean.
MIT License
407 stars 24 forks source link

Ability to pass only a string to `breadcrumb` #47

Open richardonrails opened 3 years ago

richardonrails commented 3 years ago

Both crummy (which I'm trying to migrate from) and breadcrumbs_on_rails support similar syntax as loaf for adding breadcrumbs by passing a string followed by a path:

breadcrumb "Categories", blog_categories_path

However the other 2 libraries also support passing only a string for the last item, such as:

def show
  # ...
  breadcrumb "Categories", blog_categories_path
  breadcrumb @category.title
end

It would aid in the migration of large applications from one of those gems to loaf if it supported the string-only syntax for the final crumb rather than throwing ArgumentError (wrong number of arguments (given 1, expected 2..3))

In particular, excluding a path for the final item would be common given Bootstrap 4's template which doesn't even want the last item linked.

piotrmurach commented 3 years ago

Hi there 👋

I'm in favour of making loaf to be more compatible.

As far as making the breadcrumb work with only the string, I have small reservations. Why is that? All the code is currently built on the assumption that a crumb instance has a valid url. From a functional standpoint, this is a rather strong guarantee that simplifies things internally and externally. Since this gem enumerates breadcrumbs and leaves it up to you to decide about markup, you can detect the last breadcrumb and display only the title. Alternatively, I'd consider extending the crumb instance with the last? as suggested in the https://github.com/piotrmurach/loaf/issues/41. These are my quick thoughts and I'd encourage you to submit PR.

richardonrails commented 3 years ago

In my case it's about backwards compatibility to avoid having to modify a bunch of existing code beyond what otherwise would be a simple find/replaceall of the method name.

But perhaps there are general cases... e.g. when you know you'll never try to hyperlink the final breadcrumb anyway (or only link it if there is a URL), and some pages (e.g. a POST confirmation page) may not even have a valid URL that brings you to the same place. Just seems reasonable (like the other 2 libraries) that a URL shouldn't be required when you know it'll never be used, either because you just don't want to link the last item, or because sometimes a valid URL may not even exist for the last item.

Do the gem internals really need to require a URL even if the URL will never be used? Seems like that should only matter when a link is being created on the display side.