Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

"config" object for setting defaults #41

Open
wants to merge 2 commits into from

4 participants

Nicholas Zaillian Romain Lalaut Dane Natoli Simone Carletti
Nicholas Zaillian

I was using breadcrumbs_on_rails along with a twitter bootstrap-friendly builder (https://gist.github.com/riyad/1933884) and it felt tedious and wrong explicitly passing the "builder" and "separator" options to every "render_breadcrumbs" invocation when these things weren't changing, so I added a Breadcrumbs "config" module member (and supporting logic to the view helpers and SimpleBuilder) so that I could just stick this in an initializer:

# config/initializers/breadcrumbs_on_rails.rb
BreadcrumbsOnRails::Breadcrumbs::config.builder = ::BootstrapBreadcrumbsBuilder
BreadcrumbsOnRails::Breadcrumbs::config.separator = "»"    

Merge if you see fit.

Nicholas Zaillian nzaillian Adding "config" module member (OpenStruct) to
Breadcrumbs module for setting default configuration
options (for example, from a Rails initializer).  Also,
updated view helpers and SimpleBuilder to respect defaults set in the
config object (but giving priority to options explicitly passed in).
d45c273
Romain Lalaut

+1

Dane Natoli

+1

Dane Natoli link664 commented on the diff
lib/breadcrumbs_on_rails/action_controller.rb
@@ -77,8 +77,14 @@ def add_breadcrumb(name, path = nil, filter_options = {})
module HelperMethods
def render_breadcrumbs(options = {}, &block)
- builder = (options.delete(:builder) || Breadcrumbs::SimpleBuilder).new(self, breadcrumbs, options)
+ builder = (
+ options.delete(:builder) ||
+ Breadcrumbs::config.builder ||
+ Breadcrumbs::SimpleBuilder
+ ).new(self, breadcrumbs, options)
Dane Natoli
link664 added a note

Why not make Breadcrumbs::config.builder equal Breadcrumbs::SimpleBuilder by default? That way you wouldn't have to have the double ors here, it'd just become

builder = (options.delete(:builder) || Breadcrumbs::config.builder).new(self, breadcrumbs, options)
Simone Carletti Owner
weppos added a note

I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Dane Natoli link664 commented on the diff
lib/breadcrumbs_on_rails/breadcrumbs.rb
@@ -81,7 +87,7 @@ class SimpleBuilder < Builder
def render
@elements.collect do |element|
render_element(element)
- end.join(@options[:separator] || " &raquo; ")
+ end.join(@options[:separator] || Breadcrumbs::config.separator || " &raquo; ")
Dane Natoli
link664 added a note

Same here, just default Breadcrumbs::config.separator to " » ".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Simone Carletti
Owner

Wow, it took almost 1 year to check this PR. Sorry for the long time.

I'm not a big fan of using OpenStruct in this case. In fact, the set of defined options should be constrained to prevent typos or simply passing anything into the config object.

Moreover, I would prefer Breadcrumbs::config to be referenced as Breadcrumbs::config.

Here's a possible implementation

class BreadcrumbsOnRails
  class Configuration
    attr_accessor :default_separator, :default_builder

    def initialize
      self.default_separator = " &raquo; "
      self.default_builder = Breadcrumbs::SimpleBuilder
    end
  end

  class << self
    def configure
      @config ||= Configuration.new
      block_given? ? yield(@config) : @config
    end
    alias :config :configure
  end
end

This will make possible to write

BreadcrumbsOnRails.configure do |config|
  config.default_separator = ' ... '
end
Simone Carletti weppos was assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 2, 2013
  1. Nicholas Zaillian

    Adding "config" module member (OpenStruct) to

    nzaillian authored
    Breadcrumbs module for setting default configuration
    options (for example, from a Rails initializer).  Also,
    updated view helpers and SimpleBuilder to respect defaults set in the
    config object (but giving priority to options explicitly passed in).
Commits on Jun 16, 2013
  1. Nicholas Zaillian
This page is out of date. Refresh to see the latest.
8 lib/breadcrumbs_on_rails/action_controller.rb
View
@@ -77,8 +77,14 @@ def add_breadcrumb(name, path = nil, filter_options = {})
module HelperMethods
def render_breadcrumbs(options = {}, &block)
- builder = (options.delete(:builder) || Breadcrumbs::SimpleBuilder).new(self, breadcrumbs, options)
+ builder = (
+ options.delete(:builder) ||
+ Breadcrumbs::config.builder ||
+ Breadcrumbs::SimpleBuilder
+ ).new(self, breadcrumbs, options)
Dane Natoli
link664 added a note

Why not make Breadcrumbs::config.builder equal Breadcrumbs::SimpleBuilder by default? That way you wouldn't have to have the double ors here, it'd just become

builder = (options.delete(:builder) || Breadcrumbs::config.builder).new(self, breadcrumbs, options)
Simone Carletti Owner
weppos added a note

I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
content = builder.render.html_safe
+
if block_given?
capture(content, &block)
else
12 lib/breadcrumbs_on_rails/breadcrumbs.rb
View
@@ -1,3 +1,5 @@
+require 'ostruct'
+
#--
# Breadcrumbs On Rails
#
@@ -9,6 +11,10 @@
module BreadcrumbsOnRails
module Breadcrumbs
+
+ def self.config
+ @config ||= OpenStruct.new
+ end
# The Builder class represents the abstract class for any custom Builder.
#
@@ -81,7 +87,7 @@ class SimpleBuilder < Builder
def render
@elements.collect do |element|
render_element(element)
- end.join(@options[:separator] || " &raquo; ")
+ end.join(@options[:separator] || Breadcrumbs::config.separator || " &raquo; ")
Dane Natoli
link664 added a note

Same here, just default Breadcrumbs::config.separator to " » ".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
def render_element(element)
@@ -90,8 +96,8 @@ def render_element(element)
else
content = @context.link_to_unless_current(compute_name(element), compute_path(element), element.options)
end
- if @options[:tag]
- @context.content_tag(@options[:tag], content)
+ if @options[:tag] || Breadcrumbs::config.tag
+ @context.content_tag((@options[:tag] || Breadcrumbs::config.tag), content)
else
content
end
Something went wrong with that request. Please try again.