gretel-erb 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module Gretel
2
+ module Generators
3
+ class InitializerGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_initializer
7
+ copy_file "initializer.rb", "config/initializers/#{file_name}.rb"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Gretel
2
+ module Generators
3
+ class NewGenerator < Rails::Generators::NamedBase
4
+
5
+ # this generator simply runs our two other generators (wrapping what
6
+ # would otherwise be 2 invocations into 1)
7
+ def run_all
8
+ generate("gretel:initializer", file_name)
9
+ generate("gretel:partial")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module Gretel
2
+ module Generators
3
+ class PartialGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_partial
7
+ copy_file "_gretel_breadcrumb.html.erb", "app/views/common/_gretel_breadcrumb.html.erb"
8
+ end
9
+
10
+ def copy_assets
11
+ copy_file "breadcrumb.css.erb", "app/assets/stylesheets/common/breadcrumb.css.erb"
12
+ copy_file "images/breadcrumb_bg.png", "app/assets/images/common/breadcrumb/breadcrumb_bg.png"
13
+ copy_file "images/breadcrumb_separator.png", "app/assets/images/common/breadcrumb/breadcrumb_separator.png"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ Gretel::Crumbs.layout do
2
+
3
+ # Remember to restart your application after editing this file.
4
+
5
+ # Declare the path to your custom breadcrumb partial template (optional):
6
+ # template 'common/gretel_breadcrumb.html.erb'
7
+
8
+ # Example crumbs:
9
+
10
+ # crumb :root do
11
+ # link "Home", root_path
12
+ # end
13
+
14
+ # crumb :projects do
15
+ # link "Projects", projects_path
16
+ # end
17
+
18
+ # crumb :project do |project|
19
+ # link lambda { |project| "#{project.name} (#{project.id.to_s})" }, project_path(project)
20
+ # parent :projects
21
+ # end
22
+
23
+ # crumb :project_issues do |project|
24
+ # link "Issues", project_issues_path(project)
25
+ # parent :project, project
26
+ # end
27
+
28
+ # crumb :issue do |issue|
29
+ # link issue.name, issue_path(issue)
30
+ # parent :project_issues, issue.project
31
+ # end
32
+
33
+ end
@@ -0,0 +1,7 @@
1
+ require 'gretel-erb/crumb'
2
+ require 'gretel-erb/crumbs'
3
+ require 'gretel-erb/helper_methods'
4
+ require 'gretel-erb/link'
5
+ require 'gretel-erb/parent'
6
+
7
+ ActionController::Base.send :include, Gretel::HelperMethods
@@ -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,66 @@
1
+ module Gretel
2
+ class Crumbs
3
+ #holds the path (as a string) for the partial that will be used
4
+ #to render the breadcrumb (should the user set it).
5
+ #note: the template name should be prefixed with an underscore, and the provided path
6
+ #should not include this underscore (in accordance with rails' convention for naming and rendering partials)
7
+ @@template = 'common/gretel_breadcrumb.html.erb' #...the default breadcrumb template
8
+ class << self
9
+ def controller # hack because Rails.application.routes.url_helpers needs a controller method
10
+ end
11
+
12
+ def layout(&block)
13
+ # needs to be done here because Rails.application isn't set when this file is required
14
+ self.class.send :include, Rails.application.routes.url_helpers
15
+ self.class.send :include, ActionView::Helpers::UrlHelper
16
+
17
+ instance_eval &block
18
+ end
19
+
20
+ #functions both as an accessor for the class' template and also
21
+ #as a setter. If you want to associate a template with your breadcrumb,
22
+ #we suggest you add a call to this method in the block you pass to the layout method (above)
23
+ #and pass it the path (as a string) to the partial you want used for rendering the breadcrumb.
24
+ def template(*template)
25
+ @@template = template[0] if template.length > 0
26
+ @@template
27
+ end
28
+
29
+ def all
30
+ @crumbs ||= {}
31
+ end
32
+
33
+ def crumb(name, &block)
34
+ all[name] = block
35
+ end
36
+
37
+ def get_crumb(name, *params)
38
+ raise "Crumb '#{name}' not found." unless all[name]
39
+
40
+ @params = params # share the params so we can call it from link() and parent()
41
+ @link = nil
42
+ @parent = nil
43
+
44
+ all[name].call(*params)
45
+ Gretel::Crumb.new(@link, @parent)
46
+ end
47
+
48
+ def link(text, url, options = {})
49
+ text = text.call(*@params) if text.is_a?(Proc)
50
+ url = url.call(*@params) if url.is_a?(Proc)
51
+
52
+ @link = Gretel::Link.new(text, url, options)
53
+ end
54
+
55
+ def parent(name, *params)
56
+ name = name.call(*@params) if name.is_a?(Proc)
57
+
58
+ params.each_with_index do |param, i|
59
+ params[i] = param.call(&@params) if param.is_a?(Proc)
60
+ end
61
+
62
+ @parent = Gretel::Parent.new(name, *params)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,53 @@
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, params = args[0], args[1..-1]
14
+
15
+ options[:link_last] ||= false
16
+
17
+ if name
18
+ @_breadcrumb_name = name
19
+ @_breadcrumb_params = params
20
+ crumbs = breadcrumb_for(@_breadcrumb_name, *@_breadcrumb_params, options)
21
+ else
22
+ if @_breadcrumb_name
23
+ crumbs = breadcrumb_for(@_breadcrumb_name, *@_breadcrumb_params, options)
24
+ elsif options[:show_root_alone]
25
+ crumbs = breadcrumb_for(:root, options)
26
+ end
27
+ end
28
+ render_to_string(:partial => Crumbs.template, :locals => {:crumbs => crumbs.reverse, :options => options }, :layout => false).html_safe
29
+ end
30
+
31
+ def breadcrumb_for(*args)
32
+ options = args.extract_options!
33
+
34
+ name, params = args[0], args[1..-1]
35
+
36
+ crumb = Crumbs.get_crumb(name, *params)
37
+ out = [crumb]
38
+
39
+ while parent = crumb.parent
40
+ last_parent = parent.name
41
+ crumb = Crumbs.get_crumb(parent.name, *parent.params)
42
+ out << crumb
43
+ end
44
+
45
+ # TODO: Refactor this
46
+ if options[:autoroot] && name != :root && last_parent != :root
47
+ crumb = Crumbs.get_crumb(:root)
48
+ out << crumb
49
+ end
50
+ out
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ module Gretel
2
+ class Link
3
+ attr_accessor :options, :text, :url
4
+
5
+ def initialize(text, url, options = {})
6
+ @text, @url, @options = text, url, options
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Gretel
2
+ class Parent
3
+ attr_accessor :name, :params
4
+
5
+ def initialize(name, *params)
6
+ @name, @params = name, params
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gretel-erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nzaillian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: EOS This is a significant fork of Lasse Bunk's rails gem 'gretel' (http://github.com/lassebunk/gretel)
15
+ that retains the convenient page hierarchy specification DSL of the original gem,
16
+ but does all rendering using a user-specifiable partial template.
17
+ email: nzaillian@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/generators/gretel/initializer_generator.rb
23
+ - lib/generators/gretel/new_generator.rb
24
+ - lib/generators/gretel/partial_generator.rb
25
+ - lib/generators/gretel/templates/initializer.rb
26
+ - lib/gretel-erb/crumb.rb
27
+ - lib/gretel-erb/crumbs.rb
28
+ - lib/gretel-erb/helper_methods.rb
29
+ - lib/gretel-erb/link.rb
30
+ - lib/gretel-erb/parent.rb
31
+ - lib/gretel-erb.rb
32
+ homepage: http://github.com/nzaillian/gretel-erb
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.24
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Lasse Bunk's 'gretel' (http://github.com/lassebunk/gretel), but with erb
56
+ templates for breadcrumb rendering!
57
+ test_files: []