goose 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Bruce Williams
1
+ Copyright (c) 2010-2011 Bruce Williams
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -103,6 +103,61 @@ And use `yield` wherever it makes sense in your layout:
103
103
 
104
104
  It's that simple.
105
105
 
106
+ Generating Breadcrumbs
107
+ ----------------------
108
+
109
+ Let's say we had navigation that looked like:
110
+
111
+ <% content_for :main_navigation do %>
112
+ <nav>
113
+ <ul>
114
+ <%= nav_to 'People', people_path %>
115
+ <%= nav_to 'Places', places_path %>
116
+ <%= nav_to 'Things', things_path %>
117
+ </ul>
118
+ </nav>
119
+ <% end %>
120
+
121
+ In our action templates, we would usually use `nav_at` to indicate the
122
+ currently active navigation item:
123
+
124
+ <%= nav_at 'Places' %>
125
+
126
+ We could give it even more detail if we'd like breadcrumbs. In this
127
+ example we're looking at a page for an arrondissement in Paris:
128
+
129
+ <%= nav_at 'Places', 'Paris', 'Enclos-St-Laurent' %>
130
+
131
+ This `nav_at` invocation will still render the `main` partial in
132
+ `app/views`, we just need to add a `content_for` for breadcrumbs.
133
+ We'll make it a simple list that we can style later with CSS:
134
+
135
+ <% content_for :breadcrumbs do %>
136
+ <nav id='breadcrumbs'>
137
+ <ul>
138
+ <% breadcrumbs.each do |crumb| %>
139
+ <li><%= crumb %></li>
140
+ <% end %>
141
+ </ul>
142
+ </nav>
143
+ <% end %>
144
+
145
+ Remember, we need to tell the layout where to put the breadcrumbs.
146
+ We just defined the content for `:breadcrumbs`, so we'll add a
147
+ matching `yield` to our `application.html.erb`:
148
+
149
+ <%= yield :breadcrumbs %>
150
+
151
+ If we run this and pull up the page in a browser, we'll notice that
152
+ only the `Places` breadcrumb is a link (it automatically picked it up
153
+ from the `nav_to` in the main nav, handy!) -- if we want the other
154
+ breadcrumbs to be linked (usually all but the last), we can pass the
155
+ URLs as well:
156
+
157
+ <%= nav_at 'Places', ['Paris', place_path(@paris)], 'Enclos-St-Laurent' %>
158
+
159
+ Now both `Places` and `Paris` are linked.
160
+
106
161
  Customizing Goose
107
162
  -----------------
108
163
 
@@ -111,4 +166,4 @@ See `config/initializers/goose.rb` for options.
111
166
  Copyright
112
167
  ---------
113
168
 
114
- Copyright (c) 2010 Bruce Williams. See LICENSE for details.
169
+ Copyright (c) 2010-2011 Bruce Williams. See LICENSE for details.
@@ -10,4 +10,7 @@ Goose.setup do |config|
10
10
  # Default options added to nav_to wrapper when not current location
11
11
  # config.inactive_options = {}
12
12
 
13
+ # Default method to create links in breadcrumbs
14
+ # config.breadcrumb_linker = lambda { |view, *args| view.link_to(*args) }
15
+
13
16
  end
@@ -0,0 +1,45 @@
1
+ module Goose
2
+
3
+ class Address
4
+ include Enumerable
5
+
6
+ def initialize(head, *tail)
7
+ if head.is_a?(Array)
8
+ @head = head
9
+ else
10
+ @head_name = head
11
+ end
12
+ @tail = tail
13
+ end
14
+
15
+ def at?(head_name)
16
+ if resolved?
17
+ @head.first == head_name
18
+ else
19
+ @head_name == head_name
20
+ end
21
+ end
22
+
23
+ def each(&block)
24
+ list.each(&block)
25
+ end
26
+
27
+ def resolve(url)
28
+ unless resolved?
29
+ @head = [@head_name, url]
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def list
36
+ @list ||= [@head, *@tail].compact
37
+ end
38
+
39
+ def resolved?
40
+ @head
41
+ end
42
+
43
+ end
44
+
45
+ end
data/lib/goose/config.rb CHANGED
@@ -2,7 +2,7 @@ module Goose
2
2
 
3
3
  class Config
4
4
 
5
- attr_writer :wrapper_tag, :active_options, :inactive_options
5
+ attr_writer :wrapper_tag, :active_options, :inactive_options, :breadcrumb_linker
6
6
 
7
7
  def wrapper_tag
8
8
  @wrapper_tag ||= :li
@@ -12,6 +12,10 @@ module Goose
12
12
  @active_options ||= {:class => 'active'}
13
13
  end
14
14
 
15
+ def breadcrumb_linker
16
+ @breadcrumb_linker ||= lambda { |view, *args| view.link_to(*args) }
17
+ end
18
+
15
19
  def inactive_options
16
20
  @inactive_options ||= {}
17
21
  end
data/lib/goose/helper.rb CHANGED
@@ -8,20 +8,33 @@ module Goose
8
8
 
9
9
  def nav_to(place, url, options = {}, &block)
10
10
  if goose.at?(place)
11
+ goose.current_address.resolve(url)
11
12
  options.update(Goose.config.active_options)
12
13
  end
13
- content = link_to(place, url) if url
14
+ content = link_to(place, url) if url
14
15
  content_tag(Goose.config.wrapper_tag, content, options, &block)
15
16
  end
16
17
 
17
- def nav_at(place, options = {})
18
+ def nav_at(*places)
19
+ options = places.last.is_a?(Hash) ? args.pop : {}
18
20
  nav = options[:in] || :main
19
- goose.render(nav, place)
21
+ address = Address.new(*places)
22
+ goose.render(nav, address)
20
23
  end
21
24
 
22
25
  def nav(name = nil)
23
26
  nav_at(nil, :in => name)
24
27
  end
28
+
29
+ def breadcrumbs
30
+ goose.current_address.map do |crumb|
31
+ if crumb.is_a?(Array)
32
+ Goose.config.breadcrumb_linker.call(self, *crumb)
33
+ else
34
+ crumb
35
+ end
36
+ end
37
+ end
25
38
 
26
39
  end
27
40
 
data/lib/goose/state.rb CHANGED
@@ -7,18 +7,26 @@ module Goose
7
7
  @stack ||= []
8
8
  end
9
9
 
10
- def render(nav, place)
11
- at(nav, place) { @view.render(:partial => "nav/#{nav}") }
10
+ def render(nav, address)
11
+ at(nav, address) { @view.render(:partial => "nav/#{nav}") }
12
12
  end
13
13
 
14
14
  def at?(place)
15
- @stack.last == place
15
+ current_address.at?(place)
16
16
  end
17
17
 
18
- private
18
+ def breadcrumbs
19
+ current_address.breadcrumbs
20
+ end
21
+
22
+ def current_address
23
+ @stack.last
24
+ end
19
25
 
20
- def at(nav, place, &block)
21
- @stack << place
26
+ private
27
+
28
+ def at(nav, address, &block)
29
+ @stack << address
22
30
  result = yield
23
31
  @stack.pop
24
32
  result
data/lib/goose/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Goose
2
- VERSION = '0.6.1'
2
+ VERSION = '0.7.0'
3
3
  end
data/lib/goose.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Goose
2
2
 
3
+ autoload :Address, 'goose/address'
3
4
  autoload :Config, 'goose/config'
4
5
  autoload :Helper, 'goose/helper'
5
6
  autoload :State, 'goose/state'
@@ -12,11 +13,6 @@ module Goose
12
13
  def self.setup(&block)
13
14
  yield config
14
15
  end
15
-
16
- # Not recommended for production use.
17
- def self.eject! #:nodoc:
18
- raise NotImplementedError, "BOOM...Crunch"
19
- end
20
16
 
21
17
  end
22
18
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: goose
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.1
5
+ version: 0.7.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Bruce Williams
@@ -26,6 +26,7 @@ files:
26
26
  - lib/generators/goose/install_generator.rb
27
27
  - lib/generators/goose/templates/goose.rb
28
28
  - lib/generators/goose/USAGE
29
+ - lib/goose/address.rb
29
30
  - lib/goose/config.rb
30
31
  - lib/goose/ext/action_view.rb
31
32
  - lib/goose/helper.rb