breadcrumb_helper 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bbd36c4cc2857a4e4b8c9c60aa0fc56f8f541e36a5cdaaba7e2ff01d227e024
4
- data.tar.gz: ee46ee95fd366635e758df07725d1ffe078cb0c7a7c8ed9d13b078497a9afd50
3
+ metadata.gz: 3931b76ff470a9c63291b8aea824a303adc155c6c238e8318c3192607a9b43f1
4
+ data.tar.gz: 45718826d12b2a2961af56fe250a3927c4aacb72126c26c1a225e3403338eb49
5
5
  SHA512:
6
- metadata.gz: 36867717f2dd6337899e2d70f65ca150c7499f6abb821afd0788651852f135b5779a1dbffe3460a522646f7eaebc1bfed778b34f8f1894162b15d116b16d463f
7
- data.tar.gz: dc8a21f2648139a9c3cc473c904a6793c70334d11e6a372b194a2c07abd468da286129be47f51dcb25017cea0d049acadd37b98f45dfba91cc636995b272bfb9
6
+ metadata.gz: a7d6217f08eea2596a885dc4716dc6d2e538fb6fae02992d1e8adf1f5deb1d896b22369d85d61fe448ce1a30e6ad70705af8a0ada32ec9259be6612a7d269299
7
+ data.tar.gz: ace507d1dad54a246864fe3a22deb1ed00bc5efa760c384cf18f33e00c8324d64c3bd8ebf5604deeabddf167812954afe0e3be5ca34683cd920b26585f8eb657
data/README.md CHANGED
@@ -1,34 +1,105 @@
1
- # BreadcrumbHelper
1
+ # Breadcrumb Helper for Rails
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ ## Installation
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/breadcrumb_helper`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ Add this to your Gemfile:
6
6
 
7
- ## Installation
7
+ ```bash
8
+ gem 'breadcrumb_helper', '~> 0.1'
9
+ ```
10
+
11
+ ## Usage
8
12
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
13
+ ### Generate the files
10
14
 
11
- Install the gem and add to the application's Gemfile by executing:
15
+ To start, use the provided generator for a simplified and streamlined workflow:
12
16
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
17
+ ```bash
18
+ rails generate breadcrumbs namespace
19
+ ```
14
20
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
21
+ For example:
16
22
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
23
+ ```bash
24
+ rails generate breadcrumbs test/namespace
25
+ ```
26
+ will generate the following files in `test/namespace` folder structure respectively.
27
+ 1. `app/helpers/concerns/breadcrumb_helper/test/namespace.rb`
28
+ 2. `app/views/test/namespace/_breadcrumbs.html.erb`
29
+ 3. `app/views/test/namespace/_breadcrumb_items.html.erb`
18
30
 
19
- ## Usage
31
+ ### Structuring the breadcrumbs
32
+
33
+ The generator will provide a file inside `concerns` folder of `helpers`. This should be included (`include`) or prepended (`prepend`) to your helper files. Assume that we have a `Test::Namespace::SomeController` with `show` and `index` actions. There should exist a `Test::Namespace::SomeHelper` file that includes or prepends the `BreadcrumbHelper::Test::Namespace` module.
34
+
35
+ ```ruby
36
+ module Test::Namespace
37
+ module SomeHelper
38
+ include BreadcrumbHelper::Test::Namespace
39
+ # or prepend BreadcrumbHelper::Test::Namespace
40
+
41
+ # ...
42
+ end
43
+ end
44
+ ```
45
+
46
+ Since `SomeController` have `show` and `index` actions, our helper can define breadcrumbs for each action as such:
47
+
48
+ ```ruby
49
+ module Test::Namespace
50
+ module SomeHelper
51
+ include BreadcrumbHelper::Test::Namespace
52
+
53
+ def index_breadcrumbs
54
+ add_breadcrumb name: 'Home'
55
+ end
56
+
57
+ def show_breadcrumbs
58
+ add_breadcrumb name: 'Home', path: root_path
59
+ add_breadcrumb name: 'Specific Data'
60
+ end
61
+ end
62
+ end
63
+ ```
64
+
65
+ With this setup, the controller's `index` and `show` actions will have their respective breadcrumbs rendered as defined in your helper.
66
+
67
+ ### Render the breadcrumbs
68
+
69
+ Somewhere in your `layouts` (anywhere, really), simply call `render 'breadcrumbs'` to render them.
70
+
71
+ ## Gotchas
72
+
73
+ ### Railtie Configuration
74
+
75
+ This gem's Railtie sets the Rails application's `config.action_controller.include_all_helpers` to `false`. This is a non-negotiable setup as the way breadcrumbs are defined in each helper *will have unavoidably similar method names*.
76
+
77
+ By default, Rails includes **all helper files** into a controller. This will result to some method names getting replaced when there are similar ones across all helpers. In order to avoid this, the mentioned config needs to be disabled so that **it only loads the related helper associated with the controller**.
78
+
79
+ ### Crumb Stoppers
80
+
81
+ As of `0.2.0`, this gem now supports a feature called Crumb Stoppers. Suppose we have the given code:
20
82
 
21
- TODO: Write usage instructions here
83
+ ```ruby
84
+ module Some::Namespace
85
+ module MyHelper
86
+ prepend BreadcrumbHelper::Some::Namespace
22
87
 
23
- ## Development
88
+ def show_breadcrumbs
89
+ add_breadcrumb name: 'Home', path: root_path
90
+ add_breadcrumb name: @product.name # Assume this does not exist because of an unauthorized error that was rendered earlier.
91
+ end
92
+ end
93
+ end
94
+ ```
24
95
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
96
+ With Crumb Stoppers, the breadcrumb trails will still display Home, and will not raise an exception.
26
97
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
98
+ Without Crumb Stoppers, the developer will need to add support for code *in each breadcrumb action*. To avoid this problem, the current default code of the gem is changed so that it stops the breadcrumb trail when it encounters a `NoMethodError` exception. Do note that it will still display the breadcrumbs that has no errors prior for a more beautiful display. However, the implementation is only integrated mostly in the view template, so for those who are updating the gem from `0.1.*`, I would advise to copy the code style found in the template to avail the Crumb Stoppers feature.
28
99
 
29
100
  ## Contributing
30
101
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/breadcrumb_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/breadcrumb_helper/blob/master/CODE_OF_CONDUCT.md).
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tieeeeen1994/breadcrumb_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tieeeeen1994/breadcrumb_helper/blob/master/CODE_OF_CONDUCT.md).
32
103
 
33
104
  ## License
34
105
 
@@ -36,4 +107,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
36
107
 
37
108
  ## Code of Conduct
38
109
 
39
- Everyone interacting in the BreadcrumbHelper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/breadcrumb_helper/blob/master/CODE_OF_CONDUCT.md).
110
+ Everyone interacting in the BreadcrumbHelper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tieeeeen1994/breadcrumb_helper/blob/master/CODE_OF_CONDUCT.md).
@@ -21,6 +21,23 @@ module BreadcrumbHelper
21
21
  render('breadcrumb_items', items: breadcrumb_items)
22
22
  end
23
23
 
24
+ # Tries to render the item name.
25
+ # This is a dynamic value, hence this method exists so that it can become the interface for displaying item names
26
+ # with the benefit of added guard clauses.
27
+ # It is recommended to use this in the view files.
28
+ def render_breadcrumb_item_name(item)
29
+ item[:name].to_s
30
+ rescue NoMethodError
31
+ nil
32
+ end
33
+
34
+ # Tries to render the item path.
35
+ # It does nothing special, but support for it is added just in case there are updates.
36
+ # It is still more recommended to use this in the view files.
37
+ def render_breadcrumb_item_path(item)
38
+ item[:path]
39
+ end
40
+
24
41
  private
25
42
 
26
43
  # Absolute source of truth for breadcrumb items, only modifiable through add_breadcrumb method.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BreadcrumbHelper
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -1,11 +1,15 @@
1
1
  <% items.each do |item| %>
2
- <% if item[:path] %>
2
+ <% item_name = render_breadcrumb_item_name(item) %>
3
+ <% break unless item_name %>
4
+
5
+ <% item_path = render_breadcrumb_item_path(item) %>
6
+ <% if item_path %>
3
7
  <li class="breadcrumb-item">
4
- <a href="<%= item[:path] %>"><%= item[:name] %></a>
8
+ <a href="<%= item_path %>"><%= item_name %></a>
5
9
  </li>
6
10
  <% else %>
7
11
  <li class="breadcrumb-item active" aria-current="page">
8
- <%= item[:name] %>
12
+ <%= item_name %>
9
13
  </li>
10
14
  <% end %>
11
15
  <% end %>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breadcrumb_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tien
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-02 00:00:00.000000000 Z
11
+ date: 2025-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '7'
27
- description:
27
+ description:
28
28
  email:
29
29
  - tieeeeen1994@gmail.com
30
30
  executables: []
@@ -50,7 +50,7 @@ licenses:
50
50
  metadata:
51
51
  homepage_uri: https://github.com/tieeeeen1994/rails-breadcrumb-helpers
52
52
  rubygems_mfa_required: 'true'
53
- post_install_message:
53
+ post_install_message:
54
54
  rdoc_options: []
55
55
  require_paths:
56
56
  - lib
@@ -65,8 +65,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  requirements: []
68
- rubygems_version: 3.5.3
69
- signing_key:
68
+ rubygems_version: 3.3.5
69
+ signing_key:
70
70
  specification_version: 4
71
71
  summary: Provides easy helper methods to generate breadcrumbs in Rails views.
72
72
  test_files: []