Skip to content

Instantly share code, notes, and snippets.

@woshidan
Created April 6, 2014 02:24
Show Gist options
  • Save woshidan/10000742 to your computer and use it in GitHub Desktop.
Save woshidan/10000742 to your computer and use it in GitHub Desktop.
make-breadcrumb-list
def get_breadcrumbs
url = request.url
url_piecies = url.split("/")
url_piecies.slice!(0,2)
url_piecies[0].gsub!(/:/,'')
layer_piecies = get_layer_piecies(url_piecies)
layers = get_layers(layer_piecies)
end
def get_category_caption(url_piece, category)
return Department.find(url_piece).name if category == 'department'
return Employee.find(url_piece).name if category == 'employee'
return ProductCategory.find(url_piece).name if category == 'category'
return Product.find(url_piece).name if category == 'product'
end
def get_layer_caption(url_piece)
layer_caption = { 'localhost3000' => "管理画面",
'personal' => "部署一覧",
'department' => '部署',
'employee' => '社員',
'products' => '商品カテゴリ一覧',
'category' => 'タイプ',
'product' => '商品'
}
layer_caption[url_piece]
end
def get_layer_piecies(url_piecies)
layer_piecies = []
url_piecies.each do |url_piece|
layer_caption = get_layer_caption(url_piece)
if layer_caption == nil
category_caption = get_category_caption(url_piece, layer_piecies.last[:url])
layer_piecies.last[:caption] = layer_piecies.last[:caption] + "(#{category_caption})"
layer_piecies.last[:url] = layer_piecies.last[:url] + "/#{url_piece}"
else
url_piece = '/' if url_piece == 'localhost3000'
layer_piecies << { caption: layer_caption, url: url_piece }
end
end
layer_piecies
end
def get_layers(layer_piecies)
layers = []
layer_url_piecies = []
layer_piecies.each do |layer_piece|
layer_url_piecies << layer_piece[:url]
end
layer_piecies.each_with_index do |layer_piece, i|
url = layer_url_piecies[0..i].join('/')
url.slice!(0,1) if url != '/'
caption = layer_piece[:caption]
layers << { caption: caption, url: url }
end
layers
end
<% breadcrumbs = get_breadcrumbs %>
<% breadcrumbs.each do |breadcrumb| %>
<% class_name = 'breadcrumb'%>
<% class_name += ' last' if breadcrumbs.last == breadcrumb %>
<%= link_to "> " + breadcrumb[:caption], breadcrumb[:url], :class => class_name %>
<% end %>
BreadcrumbList::Application.routes.draw do
root 'static_pages#home'
match "/products", to: 'product_categories#index', via: 'get'
match "/products/category/:id", to: 'products#index', via: 'get'
match "/products/category/:category_id/product/:id", to: 'products#show', via: 'get'
match '/personal', to: 'departments#index', via: 'get'
match "/personal/department/:id", to: 'employees#index', via: 'get'
match '/personal/department/:department_id/employee/:id', to: 'employees#show', via: 'get'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment