gretel 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Lasse Bunk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,73 @@
1
+ Gretel is a Ruby on Rails plugin that makes it easy yet flexible to create breadcrumbs.
2
+
3
+
4
+ == Installation
5
+
6
+ In Rails 3, in your Gemfile:
7
+
8
+ gem 'gretel'
9
+
10
+
11
+ == Example
12
+
13
+ Start by generating initializer:
14
+
15
+ $ rails generate gretel breadcrumbs
16
+
17
+ In <code>config/initializers/breadcrumbs.rb</code>:
18
+
19
+ Gretel::Crumbs.layout do
20
+
21
+ crumb :root do
22
+ link "Home", root_path
23
+ end
24
+
25
+ crumb :projects do
26
+ link "Projects", projects_path
27
+ end
28
+
29
+ crumb :project do |project|
30
+ link lambda { |project| "#{project.name} (#{project.id.to_s})" }, project_path(project)
31
+ parent :projects
32
+ end
33
+
34
+ crumb :project_issues do |project|
35
+ link "Issues", project_issues_path(project)
36
+ parent :project, project
37
+ end
38
+
39
+ crumb :issue do |issue|
40
+ link issue.name, issue_path(issue)
41
+ parent :project_issues, issue.project
42
+ end
43
+
44
+ end
45
+
46
+ In <code>app/views/layouts/application.html.erb</code>:
47
+
48
+ <%= breadcrumb :pretext => "You are here:",
49
+ :separator => ">",
50
+ :autoroot => true,
51
+ :show_root_alone => true,
52
+ :link_last => false %>
53
+
54
+ In <code>app/views/xx/xx.html.erb</code>:
55
+
56
+ <% breadcrumb :issue, @issue %>
57
+
58
+ This could also be done in the controller, if you prefer:
59
+
60
+ def show
61
+ @project = Project.find(params[:id])
62
+ breadcrumb :project, @project
63
+ end
64
+
65
+ Options for <code><%= breadcrumb %></code>:
66
+
67
+ :pretext Text to be rendered before breadcrumb, if any. Default: none
68
+ :separator Separator between links. Default: &gt;
69
+ :autoroot Whether it should automatically link to :root if no root parent is given. Default: false
70
+ :show_root_alone Whether it should show :root if this is the only link. Default: false
71
+ :link_last Whether the last crumb should be linked to. Default: false
72
+
73
+ Copyright (c) 2010 Lasse Bunk, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the gretel plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the gretel plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Gretel'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/gretel.gemspec ADDED
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "gretel"
3
+ s.version = "1.0.0"
4
+
5
+ s.author = "Lasse Bunk"
6
+ s.email = "lassebunk@gmail.com"
7
+ s.description = "Gretel is a Ruby on Rails plugin that makes it easy yet flexible to create breadcrumbs."
8
+ s.summary = "Flexible Ruby on Rails breadcrumbs plugin."
9
+ s.homepage = "http://github.com/lassebunk/gretel"
10
+ s.files = Dir['**/*']
11
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ ActionController::Base.send :include, Gretel::HelperMethods
@@ -0,0 +1,30 @@
1
+ Gretel::Crumbs.layout do
2
+
3
+ # Remember to restart your application after editing this file.
4
+
5
+ # Example crumbs:
6
+
7
+ # crumb :root do
8
+ # link "Home", root_path
9
+ # end
10
+
11
+ # crumb :projects do
12
+ # link "Projects", projects_path
13
+ # end
14
+
15
+ # crumb :project do |project|
16
+ # link lambda { |project| "#{project.name} (#{project.id.to_s})" }, project_path(project)
17
+ # parent :projects
18
+ # end
19
+
20
+ # crumb :project_issues do |project|
21
+ # link "Issues", project_issues_path(project)
22
+ # parent :project, project
23
+ # end
24
+
25
+ # crumb :issue do |issue|
26
+ # link issue.name, issue_path(issue)
27
+ # parent :project_issues, issue.project
28
+ # end
29
+
30
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates initializer for Gretel.
3
+
4
+ Example:
5
+ rails generate gretel breadcrumbs
6
+
7
+ This will create:
8
+ config/initializers/breadcrumbs.rb
@@ -0,0 +1,7 @@
1
+ class GretelGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def create_initializer
5
+ copy_file "initializer.rb", "config/initializers/#{file_name}.rb"
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ Gretel::Crumbs.layout do
2
+
3
+ # Remember to restart your application after editing this file.
4
+
5
+ # Example crumbs:
6
+
7
+ # crumb :root do
8
+ # link "Home", root_path
9
+ # end
10
+
11
+ # crumb :projects do
12
+ # link "Projects", projects_path
13
+ # end
14
+
15
+ # crumb :project do |project|
16
+ # link lambda { |project| "#{project.name} (#{project.id.to_s})" }, project_path(project)
17
+ # parent :projects
18
+ # end
19
+
20
+ # crumb :project_issues do |project|
21
+ # link "Issues", project_issues_path(project)
22
+ # parent :project, project
23
+ # end
24
+
25
+ # crumb :issue do |issue|
26
+ # link issue.name, issue_path(issue)
27
+ # parent :project_issues, issue.project
28
+ # end
29
+
30
+ end
@@ -0,0 +1,9 @@
1
+ module Gretel
2
+ class Crumb
3
+ attr_accessor :link, :parent
4
+
5
+ def initialize(link, parent)
6
+ @link, @parent = link, parent
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ module Gretel
2
+ class Crumbs
3
+ class << self
4
+ include Rails.application.routes.url_helpers
5
+ include ActionView::Helpers::UrlHelper
6
+ def controller # hack because Rails.application.routes.url_helpers needs a controller method
7
+ end
8
+ def layout(&block)
9
+ instance_eval &block
10
+ end
11
+
12
+ def all
13
+ @crumbs ||= {}
14
+ end
15
+
16
+ def crumb(name, &block)
17
+ all[name] = block
18
+ end
19
+
20
+ def get_crumb(name, object = nil)
21
+ raise "Crumb '#{name}' not found." unless all[name]
22
+
23
+ @object = object # share the object so we can call it from link() and parent()
24
+ @link = nil
25
+ @parent = nil
26
+
27
+ all[name].call(object)
28
+ Gretel::Crumb.new(@link, @parent)
29
+ end
30
+
31
+ def link(text, url)
32
+ text = text.call(@object) if text.is_a?(Proc)
33
+ url = url.call(@object) if url.is_a?(Proc)
34
+
35
+ @link = Gretel::Link.new(text, url)
36
+ end
37
+
38
+ def parent(name, object = nil)
39
+ name = name.call(@object) if name.is_a?(Proc)
40
+ object = object.call(@object) if object.is_a?(Proc)
41
+
42
+ @parent = Gretel::Parent.new(name, object)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,59 @@
1
+ module Gretel
2
+ module HelperMethods
3
+ include ActionView::Helpers::UrlHelper
4
+ def controller # hack because ActionView::Helpers::UrlHelper needs a controller method
5
+ end
6
+
7
+ def self.included(base)
8
+ base.send :helper_method, :breadcrumb_for, :breadcrumb
9
+ end
10
+
11
+ def breadcrumb(*args)
12
+ options = args.extract_options!
13
+ name, object = args[0], args[1]
14
+
15
+ if name
16
+ @_breadcrumb_name = name
17
+ @_breadcrumb_object = object
18
+ else
19
+ if @_breadcrumb_name
20
+ crumb = breadcrumb_for(@_breadcrumb_name, @_breadcrumb_object, options)
21
+ elsif options[:show_root_alone]
22
+ crumb = breadcrumb_for(:root, options)
23
+ end
24
+ end
25
+
26
+ if crumb && options[:pretext]
27
+ crumb = options[:pretext].html_safe + " " + crumb
28
+ end
29
+
30
+ crumb
31
+ end
32
+
33
+ def breadcrumb_for(*args)
34
+ options = args.extract_options!
35
+ link_last = options[:link_last]
36
+ options[:link_last] = true
37
+ separator = (options[:separator] || "&gt;").html_safe
38
+
39
+ name, object = args[0], args[1]
40
+
41
+ crumb = Crumbs.get_crumb(name, object)
42
+ out = link_to_if(link_last, crumb.link.text, crumb.link.url)
43
+
44
+ while parent = crumb.parent
45
+ last_parent = parent.name
46
+ crumb = Crumbs.get_crumb(parent.name, parent.object)
47
+ out = link_to(crumb.link.text, crumb.link.url) + " " + separator + " " + out
48
+ end
49
+
50
+ # TODO: Refactor this
51
+ if options[:autoroot] && name != :root && last_parent != :root
52
+ crumb = Crumbs.get_crumb(:root)
53
+ out = link_to(crumb.link.text, crumb.link.url) + " " + separator + " " + out
54
+ end
55
+
56
+ out
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,9 @@
1
+ module Gretel
2
+ class Link
3
+ attr_accessor :text, :url
4
+
5
+ def initialize(text, url)
6
+ @text, @url = text, url
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Gretel
2
+ class Parent
3
+ attr_accessor :name, :object
4
+
5
+ def initialize(name, object)
6
+ @name, @object = name, object
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class GretelTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gretel
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Lasse Bunk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-10 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Gretel is a Ruby on Rails plugin that makes it easy yet flexible to create breadcrumbs.
22
+ email: lassebunk@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - gretel.gemspec
31
+ - init.rb
32
+ - initializers/breadcrumbs.rb
33
+ - lib/generators/gretel/gretel_generator.rb
34
+ - lib/generators/gretel/templates/initializer.rb
35
+ - lib/generators/gretel/USAGE
36
+ - lib/gretel/crumb.rb
37
+ - lib/gretel/crumbs.rb
38
+ - lib/gretel/helper_methods.rb
39
+ - lib/gretel/link.rb
40
+ - lib/gretel/parent.rb
41
+ - MIT-LICENSE
42
+ - Rakefile
43
+ - README.rdoc
44
+ - test/gretel_test.rb
45
+ - test/test_helper.rb
46
+ homepage: http://github.com/lassebunk/gretel
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.10
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Flexible Ruby on Rails breadcrumbs plugin.
79
+ test_files: []
80
+