tabs_on_rails 2.0.2 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,300 +0,0 @@
1
- = Tabs on Rails
2
-
3
- *TabsOnRails* is a simple Rails plugin for creating tabs and navigation menus.
4
- It provides helpers for generating navigation menus with a flexible interface.
5
-
6
-
7
- == Requirements
8
-
9
- * Rails 3
10
-
11
- Please note
12
-
13
- * TabsOnRails 2.x requires Rails 3. Use TabsOnRails 1.3.x with Rails 2.
14
- * TabsOnRails doesn't work with Rails 2.1 or lower
15
- ("comment":http://www.simonecarletti.com/blog/2009/04/tabsonrails/#comment-2901 and "commit":http://github.com/weppos/tabs_on_rails/commit/d5ae9f401e3d0acc87251fa8957a8625e90ba4b3).
16
-
17
-
18
- == Installation
19
-
20
- "RubyGems":http://rubygems.org is the preferred way to install *TabsOnRails* and the best way if you want install a stable version.
21
-
22
- $ gem install tabs_on_rails
23
-
24
- Specify the Gem dependency in the "Bundler":http://gembundler.com Gemfile.
25
-
26
- gem "tabs_on_rails"
27
-
28
- Use "Bundler":http://gembundler.com and the ":git option":http://gembundler.com/v1.0/git.html if you want to grab the latest version from the Git repository.
29
-
30
-
31
- == Usage
32
-
33
- In your template use the <tt>tabs_tag</tt> helper to create your tab.
34
-
35
- <%= tabs_tag do |tab| %>
36
- <%= tab.home 'Homepage', root_path %>
37
- <%= tab.dashboard 'Dashboard', dashboard_path %>
38
- <%= tab.account 'Account', account_path %>
39
- <% end %>
40
-
41
- The example above produces the following HTML output.
42
-
43
- <ul>
44
- <li><a href="/">Homepage</a></li>
45
- <li><a href="/dashboard">Dashboard</a></li>
46
- <li><a href="/account">Account</a></li>
47
- </ul>
48
-
49
- The usage is similar to the Rails route file.
50
- You create named tabs with the syntax <tt>tab.name_of_tab</tt>.
51
-
52
- The name you use creating a tab is the same you're going to refer to
53
- in your controller when you want to mark a tab as the current tab.
54
-
55
- class DashboardController < ApplicationController
56
- set_tab :dashboard
57
- end
58
-
59
- Now, if the action belongs to <tt>DashboardController</tt>,
60
- the template will automatically render the following HTML code.
61
-
62
- <ul>
63
- <li><a href="/">Homepage</a></li>
64
- <li class="custom"><span>Dashboard</span></li>
65
- <li><a href="/account">Account</a></li>
66
- </ul>
67
-
68
- Use the <tt>current_tab</tt> helper method if you need to access
69
- the value of current tab in your controller or template.
70
-
71
- class DashboardController < ApplicationController
72
- set_tab :dashboard
73
- end
74
-
75
- # In your view
76
- <p>The name of current tab is <%= current_tab %>.</p>
77
-
78
- === Customizing a Tab
79
-
80
- You can pass a hash of options to customize the style and the behavior of the tab item.
81
- Behind the scenes, each time you create a tab, the <tt>#tab_for</tt>
82
- method is invoked.
83
-
84
- <%= tabs_tag do |tab| %>
85
- <%= tab.home 'Homepage', root_path, :style => "padding: 10px" %>
86
- <%= tab.dashboard 'Dashboard', dashboard_path %>
87
- <% end %>
88
-
89
- <ul>
90
- <li style="padding: 10px"><a href="/">Homepage</a></li>
91
- <li class="custom"><span>Dashboard</span></li>
92
- <li><a href="/account">Account</a></li>
93
- </ul>
94
-
95
- See <tt>TabsOnRails::Tabs::TabsBuilder#tab_for</tt> for more details.
96
-
97
- === Customizing open_tabs and close_tabs
98
-
99
- The open_tabs and the close_tabs methods can be customized with the <tt>:open_tabs</tt> and <tt>:close_tabs</tt> option.
100
-
101
- <%= tabs_tag :open_tabs => { :id => "tabs", :class => "cool" } do |tab| %>
102
- <%= tab.home 'Homepage', root_path %>
103
- <%= tab.dashboard 'Dashboard', dashboard_path %>
104
- <%= tab.account 'Account', account_path %>
105
- <% end %>
106
-
107
- <ul id="tabs" class="cool">
108
- <li><a href="/">Homepage</a></li>
109
- <li><a href="/dashboard">Dashboard</a></li>
110
- <li><a href="/account">Account</a></li>
111
- </ul>
112
-
113
-
114
- Further customizations require a custom <tt>Builder</tt> (see below).
115
-
116
-
117
- == Restricting set_tab scope
118
-
119
- The <tt>set_tab</tt> method understands all options you are used to pass to a Rails controller filter.
120
- In fact, behind the scenes this method uses a <tt>before_filter</tt> to store the tab in the <tt>@tab_stack</tt> variable.
121
-
122
- Taking advantage of Rails filter options, you can restrict a tab to a selected group of actions in the same controller.
123
-
124
- class PostsController < ApplicationController
125
- set_tab :admin
126
- set_tab :posts, :only => %w(index show)
127
- end
128
-
129
- class ApplicationController < ActionController::Base
130
- set_tab :admin, :if => :admin_controller?
131
-
132
- def admin_controller?
133
- self.class.name =~ /^Admin(::|Controller)/
134
- end
135
- end
136
-
137
-
138
- == Using Namespaces to create Multiple Tabs
139
-
140
- Namespaces enable you to create and manage tabs in parallels. The best way to demonstrate namespace usage is with an example.
141
-
142
- Let's assume your application provides a first level navigation menu with 3 elements: :home, :dashboard, :projects. The relationship between your tabs and your controllers is 1:1 so you should end up with the following source code.
143
-
144
- class HomeController
145
- set_tab :home
146
- end
147
-
148
- class DashboardController
149
- set_tab :dashboard
150
- end
151
-
152
- class ProjectsController
153
- set_tab :projects
154
-
155
- def first; end
156
- def second; end
157
- def third; end
158
- end
159
-
160
- The project controller contains 3 actions and you might want to create a second-level navigation menu. This menu should reflect the navigation status of the user in the project page.
161
-
162
- Without namespaces, you wouldn't be able to accomplish this task because you already set the current tab value to :projects. You need to create a parallel navigation menu and uniquely identify it with a custom namespace.
163
- Let's call it :navigation.
164
-
165
- class ProjectsController
166
- set_tab :projects
167
-
168
- # Create an other tab navigation level
169
- set_tab :first, :navigation, :only => %w(first)
170
- set_tab :second, :navigation, :only => %w(second)
171
- set_tab :third, :navigation, :only => %w(third)
172
-
173
- def first; end
174
- def second; end
175
- def third; end
176
- end
177
-
178
- Voilà! That's all you need to do. And you can create an unlimited number of namespaces as long as you use an unique name to identify them.
179
-
180
- The default namespace is called :default. Passing :default as name is the same as don't using any namespace at all. The following lines are equivalent.
181
-
182
- set_tab :projects
183
- set_tab :projects, :default
184
-
185
-
186
- === Rendering Tabs with Namespaces
187
-
188
- To switch namespace in your template, just pass the :namespace option to the <tt>tabs_tag</tt> helper method.
189
-
190
- <%= tabs_tag do |tab| %>
191
- <%= tab.home 'Homepage', root_path %>
192
- <%= tab.dashboard 'Dashboard', dashboard_path %>
193
- <%= tab.projects 'Projects', projects_path %>
194
- <% end %>
195
-
196
- <%= tabs_tag :namespace => :navigation do |tab| %>
197
- <%= tab.first 'First', first_project_path %>
198
- <%= tab.second 'Second', second_project_path %>
199
- <%= tab.third 'Account', third_project_path %>
200
- <% end %>
201
-
202
- === Namespace scope
203
-
204
- As a bonus feature, the namespace needs to be unique within current request scope, not necessarily across the entire application.
205
-
206
- Back to the previous example, you can reuse the same namespace in the other controllers. In this way, you can reuse your templates as well.
207
-
208
- class HomeController
209
- set_tab :home
210
- end
211
-
212
- class DashboardController
213
- set_tab :dashboard
214
-
215
- set_tab :index, :navigation, :only => %w(index)
216
- set_tab :common, :navigation, :only => %w(foo bar)
217
-
218
- # ...
219
- end
220
-
221
- class ProjectsController
222
- set_tab :projects
223
-
224
- set_tab :first, :navigation, :only => %w(first)
225
- set_tab :second, :navigation, :only => %w(second)
226
- set_tab :third, :navigation, :only => %w(third)
227
-
228
- # ...
229
- end
230
-
231
- == Tab Builders
232
-
233
- The <tt>Builder</tt> is responsible for creating the tabs HTML code. This library is bundled with two <tt>Builders</tt>:
234
-
235
- <tt>Tabs::Builder</tt>:: this is the abstract interface for any custom builder.
236
- <tt>Tabs::TabsBuilder</tt>:: this is the default builder.
237
-
238
- === Understanding the Builder
239
-
240
- Builders act as formatters. A Builder encapsulates all the logic behind the tab creation including the code required to toggle tabs status.
241
-
242
- When the <tt>tabs_tag</tt> helper is called, it creates a new <tt>Tabs</tt> instance with selected Builder.
243
- If you don't provide a custom builder, then <tt>Tabs::TabsBuilder</tt> is used by default.
244
-
245
- === Creating a custom Builder
246
-
247
- All builders must extend the base <tt>Tabs::Builder</tt> class and implement at least the <tt>tab_for</tt> method.
248
- Additional overridable methods include:
249
-
250
- <tt>open_tabs</tt>:: the method called before the tab set
251
- <tt>close_tabs</tt>:: the method called after the tab set
252
- <tt>tab_for</tt>:: the method called to create a single tab item
253
-
254
- The following example creates a custom tab builder called <tt>MenuTabBuilder</tt>.
255
-
256
- class MenuTabBuilder < TabsOnRails::Tabs::Builder
257
- def tab_for(tab, name, options, item_options = {})
258
- item_options[:class] = (current_tab?(tab) ? 'active' : '')
259
- @context.content_tag(:li, item_options) do
260
- @context.link_to(name, options)
261
- end
262
- end
263
- end
264
-
265
- === Using a custom Builder
266
-
267
- In your view, simply pass the builder class to the <tt>tabs_tag</tt> method.
268
-
269
- <%= tabs_tag(:builder => MenuTabBuilder) do |tab| %>
270
- <%= tab.home 'Homepage', root_path %>
271
- <%= tab.dashboard, 'Dashboard', dashboard_path %>
272
- <%= tab.account 'Account', account_path, :style => 'float: right;' %>
273
- <% end %>
274
-
275
- This is the final result.
276
-
277
- <ul>
278
- <li class=""><a href="/">Homepage</a></li>
279
- <li class="active"><a href="/dashboard">Dashboard</a></li>
280
- <li class="" style="float: right;"><a href="/account">Account</a></li>
281
- </ul>
282
-
283
-
284
- == Author
285
-
286
- * {Simone Carletti}[http://www.simonecarletti.com] <weppos@weppos.net>
287
-
288
-
289
- == Resources
290
-
291
- * {Homepage}[http://www.simonecarletti.com/code/tabs_on_rails]
292
- * {Source}[http://github.com/weppos/tabs_on_rails]
293
- * {API Documentation}[http://www.simonecarletti.com/code/tabs_on_rails/api/]
294
- * {Bugs & Features}[http://github.com/weppos/tabs_on_rails/issues]
295
-
296
-
297
- == License
298
-
299
- TabsOnRails is Copyright (c) 2009-2011 Simone Carletti.
300
- This is Free Software distributed under the MIT license.
@@ -1,42 +0,0 @@
1
- require 'test_helper'
2
-
3
- class BlockBuilderTest < ActionView::TestCase
4
- tests TabsOnRails::ActionController::HelperMethods
5
-
6
- include ActionView::Helpers::TagHelper
7
- include ActionView::Helpers::UrlHelper
8
-
9
- def current_tab(namespace)
10
- case namespace
11
- when nil, :default
12
- :dashboard
13
- when :foospace
14
- :footab
15
- else
16
- :elsetab
17
- end
18
- end
19
-
20
- class BlockBuilder < TabsOnRails::Tabs::TabsBuilder
21
- def tab_for(tab, name, options, item_options = {}, &block)
22
- item_options[:class] = item_options[:class].to_s.split(" ").push("current").join(" ") if current_tab?(tab)
23
- content = @context.link_to(name, options)
24
- content += @context.capture(&block) if block_given?
25
- @context.content_tag(:li, content, item_options)
26
- end
27
- end
28
-
29
- def setup
30
- @klass = BlockBuilder
31
- @builder = @klass.new(self)
32
- end
33
-
34
- def test_tab_for_with_block
35
- expected = %Q{<li class="current"><a href="http://dashboard.com/">Foo Bar</a><p>More Content</p></li>}
36
- actual = @builder.tab_for(:dashboard, 'Foo Bar', 'http://dashboard.com/') do
37
- content_tag(:p, "More Content")
38
- end
39
-
40
- assert_dom_equal expected, actual
41
- end
42
- end