Permalink
Browse files

add Breadcrumbs helper

  • Loading branch information...
WaYdotNET committed Aug 28, 2012
1 parent 3f62f74 commit 0beb004f46b84b84b43d6db5470e48dbb3ceebb9
@@ -32,6 +32,7 @@ class << self
# Padrino::Helpers::FormatHelpers
# Padrino::Helpers::RenderHelpers
# Padrino::Helpers::NumberHelpers
# Padrino::Helpers::Breadcrumbs
#
# @param [Sinatra::Application] app
# The specified padrino application
@@ -52,6 +53,7 @@ def registered(app)
app.helpers Padrino::Helpers::RenderHelpers
app.helpers Padrino::Helpers::NumberHelpers
app.helpers Padrino::Helpers::TranslationHelpers
app.helpers Padrino::Helpers::Breadcrumbs
end
alias :included :registered
end
@@ -0,0 +1,171 @@
module Padrino
module Helpers
class Breadcrumb
attr_accessor :home
attr_accessor :items
DEFAULT_URL = "/"
DEFAULT_CAPTION ="Home Page"
##
# initialize breadcrumbs with default value
#
# @example
# before do
# @breadcrumbs = breadcrumbs.new
# end
#
# @api public
def initialize
self.home = { :url => DEFAULT_URL, :caption => DEFAULT_CAPTION, :name => :home }
reset
end
##
# Set the custom home (Parent) link
#
# @param [String] url
# The url href
#
# @param [String] caption
# The text caption.
#
# @example
# breadcrumbs.set_home "/HomeFoo", "Foo Home"
#
#
# @api public
def set_home(url, caption)
self.home = { :url => url, :caption => caption.to_s.humanize, :name => :home }
reset
end
##
# Reset breadcrumbs to default or personal home
#
# @example
# breadcrumbs.reset
#
# @api public
def reset
self.items=[]
self.items << home
end
##
# Reset breadcrumbs to default home
#
# @example
# breadcrumbs.reset!
#
# @api public
def reset!
self.home = { :url => DEFAULT_URL, :caption => DEFAULT_CAPTION, :name => :home }
reset
end
##
# Add a new breadcrumbs
#
# @param [String] name
# The name of resource
# @param [Symbol] name
# The name of resource
#
# @param [String] url
# The url href.
#
# @param [String] caption
# The text caption
#
# @example
# breadcrumbs.add "foo", "/foo", "Foo Link"
# breadcrumbs.add :foo, "/foo", "Foo Link"
#
# @api public
def add(name, url, caption)
items << { :name => name, :url => url.to_s, :caption => caption.to_s.humanize }
end
alias :<< :add
##
# Remove a Breadcrumbs
#
# @param [String] name
# The name of resource to delete from breadcrumbs list
#
# @param [Symbol] name
# The name of resource to delete from breadcrumbs list
#
# @example
# breadcrumbs.del "foo"
# breadcrumbs.del :foo
#
# @api public
def del(name)
items.each{ |item| item.delete if item[:name] == name.to_sym }
end
end # Breadcrumb
module Breadcrumbs
# Render breadcrumbs to view
#
# @param [Breadcrumbs] breadcrumbs
# The breadcrumbs to render into view
#
# @param [Boolean] bootstrap
# If true, render separation (usefull with Twitter Bootstrap)
#
# @param [String] active
# Css class style set to active breadcrumb
#
# @return [String] Unordered list with breadcrumbs
#
# @example
# = breadcrumbs @breacrumbs
# # Generates:
# # <ul>
# # <li><a herf="/foo" >Foo Link</a></li>
# # <li class="active" ><a herf="/bar">Bar Link</a></li>
# # </ul>
#
#
# @api public
def breadcrumbs(breadcrumbs, bootstrap=false, active="active")
content=""
breadcrumbs.items[0..-2].each do |item|
content << render_item(item, bootstrap)
end
last = link_to(breadcrumbs.items.last[:caption], breadcrumbs.items.last[:url])
content << content_tag(:li, last, :class => active)
content_tag(:ul, content, :class => "breadcrumb" )
end
private
##
# Private method to return list item
#
# @param [Hash] item
# The breadcrumb item
#
# @param [Boolean] bootstrap
# If true, render separation (usefull with Twitter Bootstrap)
#
# @return [String] List item with breacrumb
#
# @api public
def render_item(item, bootstrap)
content = ""
content << link_to(item[:caption], item[:url])
content << content_tag(:span, "/", :class => "divider") if bootstrap
content_tag(:li, content )
end
end # Breadcrumb
end # Helpers
end # Padrino
@@ -97,10 +97,11 @@ def select(field, options={})
# f.check_box :remember_me, :value => 'true', :uncheck_value => '0'
def check_box(field, options={})
html =""
unchecked_value = options.delete(:uncheck_value) || '0'
options.reverse_merge!(:id => field_id(field), :value => '1')
options.reverse_merge!(:checked => true) if values_matches_field?(field, options[:value])
html = @template.hidden_field_tag(options[:name] || field_name(field), :value => unchecked_value, :id => nil)
html << @template.hidden_field_tag(options[:name] || field_name(field), :value => unchecked_value, :id => nil)
html << @template.check_box_tag(field_name(field), options)
end
@@ -409,10 +409,10 @@ def text_field_tag(name, options={})
# # => <input name="age" min="18" max="120" step="1" type="number">
#
# @api public
def number_field_tag(name, options={})
def number_field_tag(name, options={})
input_tag(:number, options.reverse_merge(:name => name))
end
##
# Creates a telephone field input with the given name and options
#
@@ -452,7 +452,7 @@ def telephone_field_tag(name, options={})
def email_field_tag(name, options={})
input_tag(:email, options.reverse_merge(:name => name))
end
##
# Creates a search field input with the given name and options
#
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
module Padrino
module Helpers
###
@@ -31,9 +31,9 @@ def is_type?
# @handler.capture_from_template(&block) => "...html..."
#
def capture_from_template(*args, &block)
self.output_buffer, _buf_was = '', self.output_buffer
self.output_buffer, _buf_was = "", self.output_buffer
block.call(*args)
ret = eval('@_out_buf', block.binding)
ret = eval("@_out_buf", block.binding)
self.output_buffer = _buf_was
ret
end
View
No changes.

0 comments on commit 0beb004

Please sign in to comment.