Skip to content

Commit

Permalink
Breadcrumbs !!
Browse files Browse the repository at this point in the history
  • Loading branch information
WaYdotNET committed Jan 28, 2012
1 parent 2d4175e commit 75be18c
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
Expand Up @@ -33,4 +33,9 @@ class Admin < Padrino::Application

access_control.roles_for :admin do |role|
end

breadcrumbs = Padrino::Helpers::Breadcrumb.new
breadcrumbs.set_home '/admin', 'Admin'
set :breadcrumbs, breadcrumbs

end
@@ -1,5 +1,9 @@
Admin.controllers :base do

before do
settings.breadcrumbs.reset
end

get :index, :map => "/" do
render "base/index"
end
Expand Down
@@ -1,11 +1,17 @@
Admin.controllers :<%= @orm.name_plural %> do

before do
settings.breadcrumbs.reset
settings.breadcrumbs.add :<%= @orm.name_singular %>, url(:<%= @orm.name_plural %>, :index), t_model(:<%= @orm.name_singular %>)
end

get :index do
@<%= @orm.name_plural %> = <%= @orm.all %>
render '<%= @orm.name_plural %>/index'
end

get :new do
settings.breadcrumbs.add :<%= @orm.name_singular %>_new , url(:<%= @orm.name_plural %>, :new), t_admin(:new)
@<%= @orm.name_singular %> = <%= @orm.build %>
render '<%= @orm.name_plural %>/new'
end
Expand All @@ -21,6 +27,7 @@ Admin.controllers :<%= @orm.name_plural %> do
end

get :edit, :with => :id do
settings.breadcrumbs.add :<%= @orm.name_singular %>_edit, params[:id], 'Edit'
@<%= @orm.name_singular %> = <%= @orm.find("params[:id]") %>
render '<%= @orm.name_plural %>/edit'
end
Expand Down
Expand Up @@ -27,6 +27,7 @@ html lang='en' xmlns='http://www.w3.org/1999/xhtml'
div class="fluid-container" class={'sidebar-left' if sidebar}
- if sidebar
div class="fluid-sidebar" == sidebar
div class='fluid-content '== breadcrumbs settings.breadcrumbs, true
div class="fluid-content"
- [:error, :warning, :success].each do |type|
- next if flash[type].blank?
Expand Down
2 changes: 2 additions & 0 deletions padrino-helpers/lib/padrino-helpers.rb
Expand Up @@ -31,6 +31,7 @@ class << self
# Padrino::Helpers::FormatHelpers
# Padrino::Helpers::RenderHelpers
# Padrino::Helpers::NumberHelpers
# Padrino::Helpers::Breadcrumbs
#
# @param [Sinatra::Application] app
# The specified padrino application
Expand All @@ -51,6 +52,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
Expand Down
171 changes: 171 additions & 0 deletions padrino-helpers/lib/padrino-helpers/breadcrumb_helpers.rb
@@ -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, :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 }
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 << tag(:li, :content => 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 << tag(:span, :content => ">", :class => "divider") if bootstrap
tag(:li, :content => content )
end

end # Breadcrumb
end # Helpers
end # Padrino

0 comments on commit 75be18c

Please sign in to comment.