Permalink
Browse files

add i18n breadcrumbs support

  • Loading branch information...
1 parent 7d2b309 commit ebc1326e2ec36a9ddea247ffee4fcf3904aa6a92 @kosmatov kosmatov committed Dec 8, 2012
Showing with 123 additions and 1 deletion.
  1. +3 −0 Gemfile
  2. +36 −0 README.md
  3. +12 −1 lib/twitter/bootstrap/rails/twitter-bootstrap-breadcrumbs.rb
  4. +64 −0 test/lib/breadcrumbs_test.rb
  5. +8 −0 test/test_helper.rb
View
@@ -5,5 +5,8 @@ gemspec
gem 'less-rails', :path => ENV['LESS_RAILS_SOURCE'] if ENV['LESS_RAILS_SOURCE']
group :test do
+ gem 'minitest'
+ gem 'mocha'
gem 'rake'
+ gem 'turn'
end
View
@@ -203,6 +203,42 @@ jQuery ->
### Flash helper
Add flash helper <%= bootstrap_flash %> to your layout (built-in with layout generator)
+### Breadcrumbs Helpers
+```ruby
+class ApplicationController
+ add_breadcrumb :index, :root_path
+end
+```
+
+```ruby
+class ExapmlesController < ApplicationController
+ add_breadcrumb :index, :examples_path
+
+ def index
+ end
+
+ def show
+ @example = Example.find params[:id]
+ add_breadcrumb @example.name, example_path(@example)
+ # add_breadcrumb :show, example_path(@example)
+ end
+end
+```
+
+Add I18n translations
+
+```yml
+en:
+ breadcrumbs:
+ application:
+ index: "Index"
+ examples:
+ index: "Examples"
+ show: "Example"
+```
+
+Add breadcrumbs helper <%= render_breadcrumbs %> to your layout
+
## Changelog
<ul>
<li>Version 0.0.5 deprecated</li>
@@ -8,19 +8,30 @@ def self.included(base)
module ClassMethods
def add_breadcrumb name, url, options={}
before_filter options do |controller|
+ name = controller.send :translate_breadcrumb, name, self.name if name.is_a?(Symbol)
controller.send :add_breadcrumb, name, url
end
end
end
protected
- def add_breadcrumb name, url = '', options = {}
+ def add_breadcrumb(name, url = '', options = {})
@breadcrumbs ||= []
+ name = translate_breadcrumb(name, self.class_name) if name.is_a?(Symbol)
url = eval(url.to_s) if url =~ /_path|_url|@/
@breadcrumbs << {:name => name, :url => url, :options => options}
end
+ def translate_breadcrumb(name, class_name)
+ scope = [:breadcrumbs]
+ namespace = class_name.underscore.split('/')
+ namespace.last.sub!('_controller', '')
+ scope += namespace
+
+ I18n.t name, scope: scope
+ end
+
def render_breadcrumbs(divider = '/')
s = render :partial => 'twitter-bootstrap/breadcrumbs', :locals => {:divider => divider}
s.first
@@ -0,0 +1,64 @@
+require "test_helper"
+
+module AbstractController
+ module Testing
+
+ class TestHelper
+ BREADCRUMB_NAMES = [:class_level, :object_level, :i18n]
+
+ class << self
+ BREADCRUMB_NAMES.each do |name|
+ define_method "#{name}_name" do
+ "test-breadcrumb-#{name}"
+ end
+
+ define_method "#{name}_path" do
+ "/test/breadcrumb/#{name}"
+ end
+ end
+ end
+ end
+
+ class TestController < AbstractController::Base
+ include AbstractController::Callbacks
+ include Twitter::Bootstrap::BreadCrumbs
+
+ add_breadcrumb TestHelper.class_level_name, TestHelper.class_level_path
+
+ def index
+ add_breadcrumb TestHelper.object_level_name, TestHelper.object_level_path
+ add_breadcrumb :test_breadcrumb_i18n, TestHelper.i18n_path
+ end
+
+ def class_name
+ self.class.name
+ end
+
+ def breadcrumbs
+ @breadcrumbs
+ end
+ end
+
+ class BreadcrumbsTest < MiniTest::Unit::TestCase
+ def setup
+ I18n.stubs(:t).returns(TestHelper.i18n_name)
+
+ @controller = TestController.new
+ @controller.process(:index)
+ end
+
+ def test_should_have_breadcrumbs
+ TestHelper::BREADCRUMB_NAMES.each do |name|
+ assert include_breadcrumb?(name), "#{name} breadcrumb not found"
+ end
+ end
+
+ def include_breadcrumb?(name)
+ selected = @controller.breadcrumbs.select { |b|
+ b[:name] == TestHelper.send("#{name}_name") && b[:url] == TestHelper.send("#{name}_path")
+ }
+ selected.any?
+ end
+ end
+ end
+end
View
@@ -0,0 +1,8 @@
+require "bundler/setup"
+Bundler.require
+
+require "minitest/autorun"
+require "abstract_controller"
+require "action_controller"
+require "active_support/dependencies"
+require "mocha/setup"

0 comments on commit ebc1326

Please sign in to comment.