loaf 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.rvmrc +28 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +132 -0
- data/LICENSE.txt +20 -0
- data/README.md +128 -0
- data/Rakefile +2 -45
- data/init.rb +3 -0
- data/lib/loaf.rb +14 -8
- data/lib/loaf/configuration.rb +2 -0
- data/lib/loaf/{filters.rb → controller_extensions.rb} +13 -12
- data/lib/loaf/crumb.rb +6 -0
- data/lib/loaf/crumb_formatter.rb +20 -0
- data/lib/loaf/railtie.rb +15 -6
- data/lib/loaf/translation.rb +18 -9
- data/lib/loaf/version.rb +4 -2
- data/lib/loaf/view_extensions.rb +43 -0
- data/loaf.gemspec +28 -0
- data/spec/integration/configuration_spec.rb +13 -0
- data/spec/integration/crumbs_routing_spec.rb +33 -0
- data/spec/integration/nested_crumbs_spec.rb +5 -0
- data/spec/loaf/crumb_formatter_spec.rb +33 -0
- data/spec/loaf/translation_spec.rb +23 -0
- data/spec/loaf_spec.rb +5 -0
- data/spec/rails_app/.gitignore +5 -0
- data/spec/rails_app/Rakefile +8 -0
- data/spec/rails_app/app/assets/images/rails.png +0 -0
- data/spec/rails_app/app/assets/javascripts/application.js +9 -0
- data/spec/rails_app/app/assets/stylesheets/application.css +7 -0
- data/spec/rails_app/app/controllers/application_controller.rb +5 -0
- data/spec/rails_app/app/controllers/comments_controller.rb +4 -0
- data/spec/rails_app/app/controllers/home_controller.rb +4 -0
- data/spec/rails_app/app/controllers/posts_controller.rb +12 -0
- data/spec/rails_app/app/helpers/application_helper.rb +2 -0
- data/spec/rails_app/app/mailers/.gitkeep +0 -0
- data/spec/rails_app/app/models/.gitkeep +0 -0
- data/spec/rails_app/app/views/home/index.html.erb +1 -0
- data/spec/rails_app/app/views/layouts/_breadcrumbs.html.erb +8 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +16 -0
- data/spec/rails_app/app/views/posts/index.html.erb +1 -0
- data/spec/rails_app/app/views/posts/new.html.erb +1 -0
- data/spec/rails_app/config.ru +4 -0
- data/spec/rails_app/config/application.rb +48 -0
- data/spec/rails_app/config/boot.rb +10 -0
- data/spec/rails_app/config/database.yml +25 -0
- data/spec/rails_app/config/environment.rb +5 -0
- data/spec/rails_app/config/environments/development.rb +30 -0
- data/spec/rails_app/config/environments/production.rb +60 -0
- data/spec/rails_app/config/environments/test.rb +42 -0
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails_app/config/initializers/inflections.rb +10 -0
- data/spec/rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails_app/config/initializers/secret_token.rb +7 -0
- data/spec/rails_app/config/initializers/session_store.rb +8 -0
- data/spec/rails_app/config/initializers/wrap_parameters.rb +14 -0
- data/spec/rails_app/config/locales/en.yml +5 -0
- data/spec/rails_app/config/routes.rb +9 -0
- data/spec/rails_app/db/seeds.rb +7 -0
- data/spec/rails_app/lib/assets/.gitkeep +0 -0
- data/spec/rails_app/lib/tasks/.gitkeep +0 -0
- data/spec/rails_app/public/404.html +26 -0
- data/spec/rails_app/public/422.html +26 -0
- data/spec/rails_app/public/500.html +26 -0
- data/spec/rails_app/public/favicon.ico +0 -0
- data/spec/rails_app/public/robots.txt +5 -0
- data/spec/rails_app/script/rails +6 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/capybara.rb +10 -0
- data/spec/support/load_routes.rb +6 -0
- metadata +202 -119
- data/README.rdoc +0 -71
- data/lib/loaf/helpers.rb +0 -49
@@ -1,13 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module Loaf
|
2
|
-
module
|
4
|
+
module ControllerExtensions
|
3
5
|
|
4
|
-
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
base.send :include, InstanceMethods
|
9
|
+
base.send :helper_method, :_breadcrumbs
|
10
|
+
end
|
5
11
|
|
6
12
|
module ClassMethods
|
7
13
|
|
8
14
|
def add_breadcrumb(name, url, options = {})
|
9
15
|
before_filter(options) do |instance|
|
10
|
-
instance.send(:add_breadcrumb, _normalize_name(name), url)
|
16
|
+
# instance.send(:add_breadcrumb, _normalize_name(name), url)
|
17
|
+
instance.send(:add_breadcrumb, name, url, options)
|
11
18
|
end
|
12
19
|
end
|
13
20
|
|
@@ -49,8 +56,8 @@ module Loaf
|
|
49
56
|
end
|
50
57
|
end
|
51
58
|
|
52
|
-
def add_breadcrumb(name, url)
|
53
|
-
_breadcrumbs << Crumb.new(name, url)
|
59
|
+
def add_breadcrumb(name, url, options={})
|
60
|
+
_breadcrumbs << Loaf::Crumb.new(name, url)
|
54
61
|
end
|
55
62
|
|
56
63
|
def _breadcrumbs
|
@@ -69,11 +76,5 @@ module Loaf
|
|
69
76
|
|
70
77
|
end # InstanceMethods
|
71
78
|
|
72
|
-
|
73
|
-
base.extend ClassMethods
|
74
|
-
base.send :include, InstanceMethods
|
75
|
-
base.send :helper_method, :_breadcrumbs
|
76
|
-
end
|
77
|
-
|
78
|
-
end # Filters
|
79
|
+
end # ControllerExtensions
|
79
80
|
end # Loaf
|
data/lib/loaf/crumb.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Loaf
|
2
|
+
module CrumbFormatter
|
3
|
+
|
4
|
+
def format_name(crumb, options={})
|
5
|
+
if !crumb.name.blank?
|
6
|
+
formatted = crumb.name
|
7
|
+
formatted = crumb.name.capitalize if options[:capitalize]
|
8
|
+
formatted = if options[:crumb_length]
|
9
|
+
truncate(formatted, :length => options[:crumb_length])
|
10
|
+
else
|
11
|
+
formatted
|
12
|
+
end
|
13
|
+
formatted
|
14
|
+
else
|
15
|
+
'[name-error]'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end # CrumbFormatter
|
20
|
+
end # Loaf
|
data/lib/loaf/railtie.rb
CHANGED
@@ -1,19 +1,28 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'action_controller'
|
4
|
+
require 'action_view'
|
2
5
|
|
6
|
+
module Loaf
|
3
7
|
if defined? Rails::Railtie
|
4
8
|
class Railtie < Rails::Railtie
|
5
9
|
initializer "loaf.extend_action_controller_base" do |app|
|
6
|
-
ActiveSupport.on_load
|
7
|
-
Loaf::Railtie.
|
10
|
+
ActiveSupport.on_load :action_controller do
|
11
|
+
Loaf::Railtie.insert_controller
|
12
|
+
Loaf::Railtie.insert_view
|
8
13
|
end
|
9
14
|
end
|
10
15
|
end
|
11
16
|
end
|
12
17
|
|
13
18
|
class Railtie
|
14
|
-
|
15
|
-
|
16
|
-
|
19
|
+
class << self
|
20
|
+
def insert_view
|
21
|
+
ActionController::Base.helper Loaf::ViewExtensions
|
22
|
+
end
|
23
|
+
def insert_controller
|
24
|
+
ActionController::Base.send :include, Loaf::ControllerExtensions
|
25
|
+
end
|
17
26
|
end
|
18
27
|
end # Railtie
|
19
28
|
|
data/lib/loaf/translation.rb
CHANGED
@@ -1,15 +1,24 @@
|
|
1
1
|
module Loaf
|
2
2
|
module Translation
|
3
|
-
|
4
|
-
def set_title!
|
5
|
-
namespace = controller.controller_path.gsub("/", ".")
|
6
|
-
action = controller.action_name
|
7
|
-
|
8
|
-
i18n_options = {
|
3
|
+
extend self
|
9
4
|
|
10
|
-
|
5
|
+
# Returns translation lookup
|
6
|
+
def i18n_scope
|
7
|
+
:breadcrumbs
|
8
|
+
end
|
9
|
+
|
10
|
+
# Accepts <tt>:scope</tt> parameter.
|
11
|
+
def breadcrumb_title(title, options={})
|
12
|
+
defaults = []
|
13
|
+
parts = title.to_s.split('.', 2)
|
14
|
+
actions = parts.pop
|
15
|
+
namespace = parts.pop
|
16
|
+
|
17
|
+
defaults << :"#{self.i18n_scope}.#{title}"
|
18
|
+
defaults << options.delete(:default) if options[:default]
|
11
19
|
|
12
|
-
|
20
|
+
options.reverse_merge! :count => 1, :default => defaults
|
21
|
+
I18n.t(title, options)
|
13
22
|
end
|
14
|
-
end
|
23
|
+
end # Translation
|
15
24
|
end # Loaf
|
data/lib/loaf/version.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'loaf/crumb_formatter'
|
4
|
+
|
5
|
+
module Loaf
|
6
|
+
module ViewExtensions
|
7
|
+
include Loaf::CrumbFormatter
|
8
|
+
|
9
|
+
# Adds breadcrumbs in a view.
|
10
|
+
#
|
11
|
+
def add_breadcrumb(name, url)
|
12
|
+
_breadcrumbs.push(name, url)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Renders breadcrumbs inside view.
|
16
|
+
#
|
17
|
+
def breadcrumbs(options={}, &block)
|
18
|
+
#builder = Loaf::Builder.new(options)
|
19
|
+
options = Loaf.config.merge(options)
|
20
|
+
|
21
|
+
_breadcrumbs.each do |crumb|
|
22
|
+
name = format_name crumb, options
|
23
|
+
|
24
|
+
url = url_for _process_url_for(crumb.url)
|
25
|
+
|
26
|
+
styles = current_page?(url) ? "#{options[:style_classes]}" : ''
|
27
|
+
|
28
|
+
block.call(name, url, styles)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def _process_url_for(url)
|
35
|
+
if url.is_a?(String) || url.is_a?(Symbol)
|
36
|
+
return send url
|
37
|
+
else
|
38
|
+
return url
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end # ViewExtensions
|
43
|
+
end # Loaf
|
data/loaf.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'loaf/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "loaf"
|
7
|
+
s.version = Loaf::Version::STRING.dup
|
8
|
+
s.authors = ["Piotr Murach"]
|
9
|
+
s.email = [""]
|
10
|
+
s.homepage = "https://github.com/peter-murach/loaf"
|
11
|
+
s.summary = %q{Loaf is a breadcrumb managing gem.}
|
12
|
+
s.description = %q{Loaf helps you manage breadcrumbs in your rails app. It aims to handle crumb data through easy dsl and expose it through view helpers without any assumptions about markup.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "tytus"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'rails'
|
22
|
+
|
23
|
+
s.add_development_dependency 'rails', '~> 3.1'
|
24
|
+
s.add_development_dependency 'sqlite3'
|
25
|
+
s.add_development_dependency 'rspec-rails'
|
26
|
+
s.add_development_dependency 'capybara'
|
27
|
+
s.add_development_dependency 'bundler'
|
28
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'setting configuration options' do
|
4
|
+
|
5
|
+
context 'setting default css style for current crumb' do
|
6
|
+
it "contains 'selected' inside the breadcrumb markup" do
|
7
|
+
visit root_path
|
8
|
+
within '#breadcrumbs' do
|
9
|
+
page.should have_selector('.selected')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "crumbs routing" do
|
4
|
+
include ActionView::TestCase::Behavior
|
5
|
+
|
6
|
+
context 'setting home breadcrumb' do
|
7
|
+
it 'should inherit crumb for the root path' do
|
8
|
+
visit root_path
|
9
|
+
within '#breadcrumbs' do
|
10
|
+
page.should have_content("Home")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'inheriting breadcrumbs setup' do
|
16
|
+
it "should inherit root crumb for index action posts#index" do
|
17
|
+
visit posts_path
|
18
|
+
within '#breadcrumbs' do
|
19
|
+
page.should have_content 'Home'
|
20
|
+
page.should have_content 'All Posts'
|
21
|
+
page.should_not have_content 'New Post'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should' do
|
26
|
+
visit new_post_path
|
27
|
+
within '#breadcrumbs' do
|
28
|
+
page.should have_content 'All Posts'
|
29
|
+
page.should have_content 'New Post'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Loaf::CrumbFormatter do
|
4
|
+
|
5
|
+
let(:formatter) { stub.extend(described_class) }
|
6
|
+
let(:crumb) { Loaf::Crumb.new('some randome name', stub) }
|
7
|
+
|
8
|
+
context '#format_name' do
|
9
|
+
it 'does not capitalize by default' do
|
10
|
+
formatted = formatter.format_name(crumb)
|
11
|
+
formatted.should eql 'some randome name'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'capitalizes crumb name' do
|
15
|
+
formatted = formatter.format_name(crumb, :capitalize => true)
|
16
|
+
formatted.should eql 'Some randome name'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'shortens crumb to provided length' do
|
20
|
+
name = 'very long name that is more that 30 characters long'
|
21
|
+
crumb = Loaf::Crumb.new(name, stub)
|
22
|
+
formatter.should_receive(:truncate).with(name, :length => 30).
|
23
|
+
and_return name[0..30]
|
24
|
+
formatter.format_name(crumb, :crumb_length => 30).should eql name[0..30]
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'returns name error if breadcrumb name is nil' do
|
28
|
+
crumb = Loaf::Crumb.new('', stub)
|
29
|
+
formatter.format_name(crumb).should eql '[name-error]'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end # Loaf::CrumbFormatter
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Loaf::Translation do
|
4
|
+
|
5
|
+
before do
|
6
|
+
I18n.backend = I18n::Backend::Simple.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'translates breadcrumb title' do
|
10
|
+
I18n.backend.store_translations 'en', :breadcrumbs => { :home => 'Home'}
|
11
|
+
described_class.breadcrumb_title('breadcrumbs.home').should eql 'Home'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'translates breadcrumb name with default scope' do
|
15
|
+
I18n.backend.store_translations 'en', :breadcrumbs => { :home => 'Home'}
|
16
|
+
described_class.breadcrumb_title('home').should eql 'Home'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'translates breadcrumb name using default option' do
|
20
|
+
described_class.breadcrumb_title('home', :default => 'breadcrumb default name').should eql 'breadcrumb default name'
|
21
|
+
end
|
22
|
+
|
23
|
+
end # Loaf::Translation
|
data/spec/loaf_spec.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
3
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
4
|
+
|
5
|
+
require File.expand_path('../config/application', __FILE__)
|
6
|
+
require 'rake'
|
7
|
+
|
8
|
+
RailsApp::Application.load_tasks
|
Binary file
|
@@ -0,0 +1,9 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into including all the files listed below.
|
2
|
+
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
|
3
|
+
// be included in the compiled file accessible from http://example.com/assets/application.js
|
4
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
5
|
+
// the compiled file.
|
6
|
+
//
|
7
|
+
//= require jquery
|
8
|
+
//= require jquery_ujs
|
9
|
+
//= require_tree .
|
@@ -0,0 +1,7 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll automatically include all the stylesheets available in this directory
|
3
|
+
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
|
4
|
+
* the top of the compiled file, but it's generally better to create a new file per style scope.
|
5
|
+
*= require_self
|
6
|
+
*= require_tree .
|
7
|
+
*/
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>Home page</h1>
|