Skip to content

Commit

Permalink
Breadcrumb links rewrite feature
Browse files Browse the repository at this point in the history
  • Loading branch information
simonoff authored and gregbell committed Nov 8, 2012
1 parent 27e162f commit a1939ec
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 5 deletions.
27 changes: 27 additions & 0 deletions 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
18 changes: 18 additions & 0 deletions lib/active_admin/dsl.rb
Expand Up @@ -120,6 +120,24 @@ def batch_action(title, 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)
Expand Down
3 changes: 3 additions & 0 deletions lib/active_admin/page.rb
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions lib/active_admin/resource.rb
Expand Up @@ -46,6 +46,9 @@ class Resource

# Set the configuration for the CSV
attr_writer :csv_builder

# Set breadcrumb builder
attr_accessor :breadcrumb

# Store a reference to the DSL so that we can dereference it during garbage collection.
attr_accessor :dsl
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/view_helpers/breadcrumb_helper.rb
Expand Up @@ -19,7 +19,7 @@ def breadcrumb_links(path = nil)
# ignored
end
end

name ||= I18n.t("activerecord.models.#{part.singularize}", :count => 1.1, :default => part.titlecase)

crumbs << link_to( name, "/" + parts[0..index].join('/'))
Expand Down
9 changes: 6 additions & 3 deletions lib/active_admin/views/title_bar.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/views/pages/layout_spec.rb
Expand Up @@ -7,7 +7,7 @@
helpers = mock_action_view

helpers.stub :active_admin_application => active_admin_application,
:active_admin_config => mock('Config', :action_items? => nil, :sidebar_sections? => nil),
:active_admin_config => mock('Config', :action_items? => nil, :breadcrumb => nil, :sidebar_sections? => nil),
:active_admin_namespace => active_admin_namespace,
:breadcrumb_links => [],
:content_for => "",
Expand Down

0 comments on commit a1939ec

Please sign in to comment.