Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added an easy to use path-based breadcrumbs and history management sy…

…stem.

There're some default tests too, but it's still a hack, no tests
whatsoever.

Other problem is I use semantic-menu with Sven Fuch's routing-filter
(the locale one), which sets root page url's to /<localename>/, and it's
still hard coded.

Signed-off-by: Balazs Nagy <julsevern@gmail.com>
  • Loading branch information...
commit f65c318d1bea6e8f28833577e80abf5a8cd6f4c6 1 parent 5651b9b
@julian7 authored
View
54 README
@@ -8,19 +8,51 @@ Inspired by and based on Daniel Haran's semantic-menu.
Example
=======
-def AdminMenu
- build_menu do |root|
- add "User functions" do |user|
- add "New user..." new_user_path
- add "Manage users" users_path
- add "Manage group access" groups_path
- end
- add "Messages" do |msg|
- add "New message" new_message_path
- add "INBOX" messages_path
+ def AdminMenu
+ build_menu do |root|
+ add "User functions" do |user|
+ add "New user..." new_user_path
+ add "Manage users" users_path
+ add "Manage group access" groups_path
+ end
+ add "Messages" do |msg|
+ add "New message" new_message_path
+ add "INBOX" messages_path
+ end
end
end
-end
+You can create a menu in your view like this:
+
+ <%= AdminMenu %>
+
+A path-based breadcrumbs can be generated by this line:
+
+ <%= AdminMenu.to_breadcrumb %>
+
+This menu stores all the path/pagetitle pairs which were visited by the user. It cuts loops,
+eg. if you return to a page which is already in the breadcrumb, it will return to the first
+occurence. If the session doesn't contain a breadcrumb or it's zero sized (eg. on first visit), a
+breadcrumb will be created from the menu structure.
+
+In order to get page title, this breadcrumb generator relies on a method called 'title()'.
+
+Return to previous page
+=======================
+
+By using the breadcrumb functionality, a new feature becomes available: the ability of returning to
+any page in our history. To use it in a controller:
+
+ class AcmeController < ApplicationController
+ include SemanticMenuHelper
+ def action
+ ...
+ redirect_to previous_page(2)
+ end
+
+where previous_page(n) returns the local URL of nth previous page, or to the first URL (which is the
+root page).
+
+*** TEMP HACK: now the root url is /<lang>/.
Copyright (c) 2009 Balazs Nagy, released under the MIT license
View
62 lib/semantic_menu.rb
@@ -52,17 +52,6 @@ def get_breadcrumb
flatten.compact.unshift(self)
end
- def to_s_breadcrumb
- ret = ""
- unless @link.nil?
- ret = view.link_to(@title, @link, @opts)
- end
- if ret.empty?
- ret = view.content_tag(:span, @title)
- end
- ret
- end
-
def to_s_children
if (@children.empty?)
return ''
@@ -125,27 +114,40 @@ def to_s
end
def to_breadcrumb
- bc = get_breadcrumb
- ret = [ MenuItem.new(I18n.t(:menu_root), "/", 0) ]
- if bc.nil?
- ret += [view.content_tag :li, view.title]
+ thispage = @@controller.session[:thispage]
+ crumbs = @@controller.session[:crumbs]
+ if @@controller.session.has_key?(:crumbs) and crumbs.size > 0
+ if ((key = crumbs.assoc(thispage)))
+ crumbs.slice!(crumbs.index(key)+1..-1)
+ else
+ crumbs.push([thispage, @@view.title])
+ end
else
- bc = bc.dup
- last = bc.pop.dup
- last.link = nil
- bc.push(last)
- ret += bc.collect{|item| item.to_s_breadcrumb}.
- reject{|item| item.nil? or item.empty?}.
- collect { |item| view.content_tag :li, item }
- end
- view.content_tag :ul, ret.join,
- :class => "breadcrumbs" + (active? ? "" : " current")
+ crumbs = path_to_breadcrumb
+ end
+ scrumbs = (crumbs[0..-2] << [nil, crumbs[-1][1]]).map do |link, title|
+ title = @@view.send(:h, title)
+ link.nil? ? title : @@view.link_to(title, link)
+ end
+ scrumbs = scrumbs.join(" &raquo; ")
+ if (crumbs.length == 1)
+ scrumbs += " &raquo;"
+ end
+ scrumbs
end
-
- protected
-
- def to_s_breadcrumb
- nil
+
+ def path_to_breadcrumb
+ bc = get_breadcrumb
+ ret = [[ "/#{I18n.locale.to_s}/", I18n.t(:menu_root) ]]
+ if bc.nil?
+ return ret
+ end
+ bc = bc.dup
+ last = bc.pop.dup
+ last.link = nil
+ bc.push(last)
+ ret + bc.collect{ |item| [item.link, item.title] }.
+ reject { |item| item[0].empty? }
end
end
end
View
4 lib/semantic_menu_helper.rb
@@ -2,4 +2,8 @@ module SemanticMenuHelper
def semantic_menu(opts = {}, &block)
::SemanticMenu::Menu.new(controller, self, opts, &block)
end
+
+ def previous_page(by = 1)
+ (session[:crumbs][-by] || session[:crumbs[0]])[0]
+ end
end
View
8 test/semantic_menus_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class SemanticMenusTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
3  test/test_helper.rb
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
Please sign in to comment.
Something went wrong with that request. Please try again.