Skip to content

Commit

Permalink
Next step - add a local_global lambda
Browse files Browse the repository at this point in the history
This let define all variables for crumbs separately.
  • Loading branch information
bsa7 committed Oct 18, 2013
1 parent 1165e7a commit e254525
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/crummy/standard_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ class StandardRenderer
#
# Takes 3 options:
# The output format. Can either be xml or html. Default :html
# :format => (:html|:xml)
# :format => (:html|:xml)
# The separator text. It does not assume you want spaces on either side so you must specify. Default +»+ for :html and +crumb+ for xml
# :separator => string
# :separator => string
# Render links in the output. Default +true+
# :link => boolean
#
# :link => boolean
#
# Examples:
# render_crumbs #=> <a href="/">Home</a> &raquo; <a href="/businesses">Businesses</a>
# render_crumbs :separator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
# render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
# render_crumbs :format => :html_list #=> <ul class="" id=""><li class=""><a href="/">Home</a></li><li class=""><a href="/">Businesses</a></li></ul>
#
#
# With :format => :html_list you can specify additional params: li_class, ul_class, ul_id
# The only argument is for the separator text. It does not assume you want spaces on either side so you must specify. Defaults to +&raquo;+
#
# render_crumbs(" . ") #=> <a href="/">Home</a> . <a href="/businesses">Businesses</a>
#
def render_crumbs(crumbs, options = {})

options[:skip_if_blank] ||= Crummy.configuration.skip_if_blank
return '' if options[:skip_if_blank] && crumbs.count < 1
options[:format] ||= Crummy.configuration.format
Expand All @@ -40,6 +41,8 @@ def render_crumbs(crumbs, options = {})
options[:last_crumb_linked] = Crummy.configuration.last_crumb_linked if options[:last_crumb_linked].nil?
options[:right_side] ||= Crummy.configuration.right_side

local_global = lambda {|crumb, global_options, param_name| crumb[2][param_name].present? ? crumb[2][param_name] : global_options[:param_name]}

case options[:format]
when :html
# crumb_string = ''.html_safe
Expand All @@ -60,7 +63,10 @@ def render_crumbs(crumbs, options = {})
# crumbs.each do |crumb|
# crumb_string << crumb_to_html_list(crumb, options[:links], options[:li_class], options[:first_class], options[:last_class], (crumb == crumbs.first), (crumb == crumbs.last), options[:microdata], options[:last_crumb_linked], options[:truncate], options[:separator])
# end
crumb_string = crumbs.map{|crumb|options[:right_side] ? nil : crumb_to_html_list(crumb, options[:links], options[:li_class], options[:first_class], options[:last_class], (crumb == crumbs.first), (crumb == crumbs.last), options[:microdata], options[:last_crumb_linked], options[:truncate], options[:separator])}.compact.join.html_safe
# Rails.logger.debug "-#= "*88
# Rails.logger.debug crumbs.inspect

crumb_string = crumbs.map{|crumb|local_global.call(crumb, options, :right_side) ? nil : crumb_to_html_list(crumb, options[:links], options[:li_class], options[:first_class], options[:last_class], (crumb == crumbs.first), (crumb == crumbs.last), options[:microdata], options[:last_crumb_linked], options[:truncate], options[:separator])}.compact.join.html_safe
crumb_string = content_tag(:ul, crumb_string, :class => options[:ul_class], :id => options[:ul_id])
crumb_string
when :xml
Expand Down Expand Up @@ -93,7 +99,7 @@ def crumb_to_html(crumb, links, first_class, last_class, is_first, is_last, with
can_link ? link_to((truncate.present? ? name.truncate(truncate) : name), url, link_html_options) : (truncate.present? ? name.truncate(truncate) : name)
end
end

def crumb_to_html_list(crumb, links, li_class, first_class, last_class, is_first, is_last, with_microdata, last_crumb_linked, truncate, separator='')
name, url, options = crumb
options = {} unless options.is_a?(Hash)
Expand All @@ -116,7 +122,7 @@ def crumb_to_html_list(crumb, links, li_class, first_class, last_class, is_first
html_content += separator unless separator.blank? || is_last
content_tag(:li, html_content, html_options)
end

def crumb_to_xml(crumb, links, separator, is_first, is_last)
name, url = crumb
content_tag(separator, name, :href => (url && links ? url : nil))
Expand All @@ -127,3 +133,4 @@ def data_definition_url(type)
end
end
end

0 comments on commit e254525

Please sign in to comment.