Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Breadcrumb links rewrite feature #1078

Closed
wants to merge 2 commits into from

6 participants

@simonoff

Example with awesome_nested_set:

ActiveAdmin.register ProductCategory do
  menu :label => 'Taxonomy'
  breadcrumb do
    if params[:id].present?
      resource.self_and_ancestors.map do |c|
        link_to c.title, list_admin_product_category_path(c)
      end
    end
  end
end
@simonoff

Any updates?

@oki

+1

@gregbell
Owner

Sorry for how long this took to review and merge!

I rebased and merged it in at a1939ec

@gregbell gregbell closed this
@bishma-stornelli

I just updated to last revision and started to use this feature. It's awesome. Thank you very much.

@arthurchui arthurchui referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@arthurchui arthurchui referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 27, 2012
  1. @simonoff
  2. @simonoff

    Fix example for breadcrumb

    simonoff authored
This page is out of date. Refresh to see the latest.
View
27 features/breadcrumb.feature
@@ -0,0 +1,27 @@
+Feature: Breadcrumb
+
+ Background:
+ Given I am logged in
+
+ Scenario: Default breadcrumb links
+ Given a configuration of:
+ """
+ ActiveAdmin.register Post do
+ end
+ """
+ When I am on the new post page
+ Then I should see a link to "Post" in the breadcrumb
+
+ Scenario: Rewritten breadcrumb links
+ Given a configuration of:
+ """
+ ActiveAdmin.register Post do
+ breadcrumb do
+ [
+ link_to('test', '/admin/test/url')
+ ]
+ end
+ end
+ """
+ When I am on the new post page
+ Then I should see a link to "test" in the breadcrumb
View
18 lib/active_admin/dsl.rb
@@ -63,6 +63,24 @@ def action_item(options = {}, &block)
def menu(options = {})
config.menu(options)
end
+
+ # Rewrite breadcrumb links.
+ # Block will be executed inside controller.
+ # Block must return an array if you want to rewrite breadcrumb links.
+ #
+ # Example:
+ # ActiveAdmin.register Post do
+ #
+ # breadcrumb do
+ # [
+ # link_to('my piece', '/my/link/to/piece')
+ # ]
+ # end
+ # end
+ #
+ def breadcrumb(&block)
+ config.breadcrumb = block
+ end
def sidebar(name, options = {}, &block)
config.sidebar_sections << ActiveAdmin::SidebarSection.new(name, options, &block)
View
3  lib/active_admin/page.rb
@@ -17,6 +17,9 @@ class Page
# An array of custom actions defined for this page
attr_reader :page_actions
+
+ # Set breadcrumb builder
+ attr_accessor :breadcrumb
module Base
def initialize(namespace, name, options)
View
3  lib/active_admin/resource.rb
@@ -46,6 +46,9 @@ class Resource
# Set the configuration for the CSV
attr_writer :csv_builder
+
+ # Set breadcrumb builder
+ attr_accessor :breadcrumb
module Base
def initialize(namespace, resource_class, options = {})
View
52 lib/active_admin/view_helpers/breadcrumb_helper.rb
@@ -1,32 +1,32 @@
module ActiveAdmin
module ViewHelpers
module BreadcrumbHelper
-
- # Returns an array of links to use in a breadcrumb
- def breadcrumb_links(path = nil)
- path ||= request.fullpath
- parts = path.gsub(/^\//, '').split('/')
- parts.pop unless %w{ create update }.include?(params[:action])
- crumbs = []
- parts.each_with_index do |part, index|
- name = ""
- if part =~ /^\d/ && parent = parts[index - 1]
- begin
- parent_class = parent.singularize.camelcase.constantize
- obj = parent_class.find(part.to_i)
- name = obj.display_name if obj.respond_to?(:display_name)
- rescue
- end
- end
- name = part.titlecase if name == ""
- begin
- crumbs << link_to( I18n.translate!("activerecord.models.#{part.singularize}", :count => 2), "/" + parts[0..index].join('/'))
- rescue I18n::MissingTranslationData
- crumbs << link_to( name, "/" + parts[0..index].join('/'))
- end
- end
- crumbs
- end
+
+ # Returns an array of links to use in a breadcrumb
+ def breadcrumb_links(path = nil)
+ path ||= request.fullpath
+ parts = path.gsub(/^\//, '').split('/')
+ parts.pop unless %w{ create update }.include?(params[:action])
+ crumbs = []
+ parts.each_with_index do |part, index|
+ name = nil
+ if part =~ /^\d/ && parent = parts[index - 1]
+ begin
+ parent_class = parent.singularize.camelcase.constantize
+ obj = parent_class.find(part.to_i)
+ name = obj.display_name if obj.respond_to?(:display_name)
+ rescue
+ end
+ end
+ name ||= part.titlecase
+ begin
+ crumbs << link_to( I18n.translate!("activerecord.models.#{part.singularize}", :count => 2), "/" + parts[0..index].join('/'))
+ rescue I18n::MissingTranslationData
+ crumbs << link_to( name, "/" + parts[0..index].join('/'))
+ end
+ end
+ crumbs
+ end
end
end
View
9 lib/active_admin/views/title_bar.rb
@@ -6,7 +6,6 @@ def build(title, action_items)
super(:id => "title_bar")
@title = title
@action_items = action_items
-
build_titlebar_left
build_titlebar_right
end
@@ -27,8 +26,12 @@ def build_titlebar_right
end
def build_breadcrumb(separator = "/")
- links = breadcrumb_links
- return if links.empty?
+ links = if active_admin_config && active_admin_config.breadcrumb.present?
+ instance_exec(controller, &active_admin_config.breadcrumb)
+ else
+ breadcrumb_links
+ end
+ return unless links.present? && links.is_a?(::Array)
span :class => "breadcrumb" do
links.each do |link|
text_node link
Something went wrong with that request. Please try again.