Skip to content

Commit

Permalink
Prefix shared helpers to reduce collision probability (weppos#129)
Browse files Browse the repository at this point in the history
Closes weppos#65
  • Loading branch information
weppos authored Feb 16, 2020
1 parent 2cd4f8c commit 1ded703
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

- Minimum requirements Rails >= 5

### Enhancements

- Prefixed shared helpers to reduce collision probability (GH-65, GH-129). [Thanks @justame, @ngelx]


## Release 3.0.1

Expand Down
28 changes: 21 additions & 7 deletions lib/breadcrumbs_on_rails/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module ActionController
included do |base|
extend ClassMethods
helper HelperMethods
helper_method :add_breadcrumb, :breadcrumbs
helper_method :add_breadcrumb_on_rails, :add_breadcrumb,
:breadcrumbs_on_rails, :breadcrumbs

unless base.respond_to?(:before_action)
base.alias_method :before_action, :before_filter
Expand All @@ -23,30 +24,43 @@ module ActionController

protected

def add_breadcrumb(name, path = nil, options = {})
self.breadcrumbs << Breadcrumbs::Element.new(name, path, options)
# Pushes a new breadcrumb element into the collection.
#
# @param name [String]
# @param path [String, nil]
# @param options [Hash]
# @return [void]
def add_breadcrumb_on_rails(name, path = nil, options = {})
breadcrumbs_on_rails << Breadcrumbs::Element.new(name, path, options)
end
alias add_breadcrumb add_breadcrumb_on_rails

def breadcrumbs
@breadcrumbs ||= []
# Gets the list of all breadcrumb element in the collection.
#
# @return [Array<Breadcrumbs::Element>]
def breadcrumbs_on_rails
@breadcrumbs_on_rails ||= []
end
alias breadcrumbs breadcrumbs_on_rails


module ClassMethods

def add_breadcrumb(name, path = nil, filter_options = {})
def add_breadcrumb_on_rails(name, path = nil, filter_options = {})
element_options = filter_options.delete(:options) || {}

before_action(filter_options) do |controller|
controller.send(:add_breadcrumb, name, path, element_options)
end
end
alias add_breadcrumb add_breadcrumb_on_rails

end

module HelperMethods

def render_breadcrumbs(options = {}, &block)
builder = (options.delete(:builder) || Breadcrumbs::SimpleBuilder).new(self, breadcrumbs, options)
builder = (options.delete(:builder) || Breadcrumbs::SimpleBuilder).new(self, breadcrumbs_on_rails, options)
content = builder.render.html_safe
if block_given?
capture(content, &block)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/action_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ class ExampleHelpersTest < ActionView::TestCase
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper

attr_accessor :breadcrumbs
attr_accessor :breadcrumbs_on_rails

setup do
self.breadcrumbs = []
self.breadcrumbs_on_rails = []
end

def test_render_breadcrumbs
Expand Down

0 comments on commit 1ded703

Please sign in to comment.