weppos-tabs_on_rails 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,6 +1,19 @@
1
1
  = Changelog
2
2
 
3
3
 
4
+ == Release 0.3.0
5
+
6
+ * ADDED: Support for namespaces in order to manage concurrent tab menus (closes #144).
7
+
8
+ * FIXED: `Uninitialized constant RAILS_DEFAULT_LOGGER (NameError)' error message when running tests outside a Rails environment.
9
+
10
+ * FIXED: Tests complains when Rails 2.3.2 is installed. This library is 100% compatible with Rails 2.3.2 but for now let's force tests to use Rails 2.2.2.
11
+
12
+ * CHANGED: current_tab= controller instance/class methods are now deprecated. Use set_tab instead.
13
+
14
+ * CHANGED: Calling tabs_tag with a custom builder as first parameter is now deprecated. Use :builder option instead.
15
+
16
+
4
17
  == Release 0.2.0
5
18
 
6
19
  * ADDED: The README file is definitely more useful now, filled up with some basic documentation.
data/Manifest ADDED
@@ -0,0 +1,23 @@
1
+ CHANGELOG.rdoc
2
+ init.rb
3
+ install.rb
4
+ lib/tabs_on_rails/controller_mixin.rb
5
+ lib/tabs_on_rails/tabs.rb
6
+ lib/tabs_on_rails/version.rb
7
+ lib/tabs_on_rails.rb
8
+ LICENSE.rdoc
9
+ Manifest
10
+ rails/init.rb
11
+ Rakefile
12
+ README.rdoc
13
+ tabs_on_rails.gemspec
14
+ tasks/tabs_on_rails_tasks.rake
15
+ test/builder_test.rb
16
+ test/controller_mixin_helpers_test.rb
17
+ test/controller_mixin_test.rb
18
+ test/controller_test.rb
19
+ test/fixtures/mixin/standard.html.erb
20
+ test/tabs_builder_test.rb
21
+ test/tabs_on_rails_test.rb
22
+ test/test_helper.rb
23
+ uninstall.rb
data/README.rdoc CHANGED
@@ -9,6 +9,9 @@ It provides helpers for creating tabs with a flexible interface.
9
9
  === As a Gem
10
10
 
11
11
  This is the preferred way to install TabsOnRails and the best way if you want install a stable version.
12
+
13
+ $ gem install weppos-tabs_on_rails --source http://gems.github.com
14
+
12
15
  You can specify the GEM dependency in your application environment.rb file:
13
16
 
14
17
  Rails::Initializer.run do |config|
@@ -18,7 +21,6 @@ You can specify the GEM dependency in your application environment.rb file:
18
21
  config.gem "weppos-tabs_on_rails", :lib => "tabs_on_rails", :source => "http://gems.github.com"
19
22
  end
20
23
 
21
-
22
24
  === As a Plugin
23
25
 
24
26
  This is the preferred way if you want to live on the edge and install a development version.
@@ -30,10 +32,10 @@ This is the preferred way if you want to live on the edge and install a developm
30
32
 
31
33
  In your template use the <tt>tabs_tag</tt> helper to create your tab.
32
34
 
33
- <% tabs_tab do |tab| %>
34
- <%= tab.home, 'Homepage', root_path %>
35
- <%= tab.dashboard, 'Dashboard', dashboard_path %>
36
- <%= tab.account, 'Account', account_path %>
35
+ <% tabs_tag do |tab| %>
36
+ <%= tab.home 'Homepage', root_path %>
37
+ <%= tab.dashboard 'Dashboard', dashboard_path %>
38
+ <%= tab.account 'Account', account_path %>
37
39
  <% end %>
38
40
 
39
41
  The example above produces the following HTML output.
@@ -48,10 +50,10 @@ The usage is similar to the Rails route file.
48
50
  You create named tabs with the syntax <tt>tab.name_of_tab</tt>.
49
51
 
50
52
  The name you use creating a tab is the same you're going to refer to in your controller
51
- when you want to mark a tab as the current_tab.
53
+ when you want to mark a tab as the current tab.
52
54
 
53
55
  class DashboardController < ApplicationController
54
- current_tab :dashboard
56
+ set_tab :dashboard
55
57
  end
56
58
 
57
59
  Now, if the action belongs to DashboardController, the template will automatically render the following HTML code.
@@ -62,21 +64,30 @@ Now, if the action belongs to DashboardController, the template will automatical
62
64
  <li><a href="/account">Account</a></li>
63
65
  </ul>
64
66
 
67
+ Use the <tt>current_tab</tt> helper method if you need to access the value of current tab in your controller or template.
68
+
69
+ class DashboardController < ApplicationController
70
+ set_tab :dashboard
71
+ end
72
+
73
+ # In your view
74
+ <p>The name of current tab is <%= current_tab %>.</p>
75
+
65
76
 
66
- == Restricting current_tab scope
77
+ == Restricting set_tab scope
67
78
 
68
- The <tt>current_tab</tt> method understands all options you are used to pass to a Rails controller filter.
69
- In fact, behind the scenes this method uses a <tt>before_filter</tt> to set a special <tt>@current_tab</tt> variable.
79
+ The <tt>set_tab</tt> method understands all options you are used to pass to a Rails controller filter.
80
+ In fact, behind the scenes this method uses a <tt>before_filter</tt> to store the tab in the <tt>@tab_stack</tt> variable.
70
81
 
71
82
  Taking advantage of Rails filter options, you can restrict a tab to a selected group of actions in the same controller.
72
83
 
73
84
  class PostsController < ApplicationController
74
- current_tab :admin
75
- current_tab :posts, :only => [ :index, :show ]
85
+ set_tab :admin
86
+ set_tab :posts, :only => [ :index, :show ]
76
87
  end
77
88
 
78
89
  class ApplicationController < ActionController::Base
79
- current_tab :admin, :if => :admin_controller?
90
+ set_tab :admin, :if => :admin_controller?
80
91
 
81
92
  def admin_controller?
82
93
  self.class.name =~ /^Admin(::|Controller)/
@@ -84,6 +95,100 @@ Taking advantage of Rails filter options, you can restrict a tab to a selected g
84
95
  end
85
96
 
86
97
 
98
+ == Using Namescopes to create Multiple Tabs
99
+
100
+ Namescopes enable you to create and manage tabs in parallels. The best way to demonstrate namescope usage is using an example.
101
+
102
+ Let's assume your application provide 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.
103
+
104
+ class HomeController
105
+ set_tab :home
106
+ end
107
+
108
+ class DashboardController
109
+ set_tab :dashboard
110
+ end
111
+
112
+ class ProjectsController
113
+ set_tab :projects
114
+
115
+ def first; end
116
+ def second; end
117
+ def third; end
118
+ end
119
+
120
+ 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.
121
+
122
+ Without namespaces, you wouldn't be able to accomplish this task because you already set the current tab value to :project. You need to create a parallel navigation menu and uniquely identify it with a custom namescope.
123
+ Let's call it :navigation.
124
+
125
+ class ProjectsController
126
+ set_tab :projects
127
+
128
+ # Create an other tab navigation level
129
+ set_tab :first, :navigation, :only => %(first)
130
+ set_tab :second, :navigation, :only => %(second)
131
+ set_tab :third, :navigation, :only => %(third)
132
+
133
+ def first; end
134
+ def second; end
135
+ def third; end
136
+ end
137
+
138
+ Voilà! That's all you need to do. And you can create an unlimited number of namescope as long as you use an unique name to identify them.
139
+
140
+ The default namescope is called :default. Passing :default as namescope name is the same as don't using any namescope at all. The following lines are equivalent.
141
+
142
+ # The following
143
+ set_tab :projects
144
+ set_tab :projects, :default
145
+
146
+
147
+ === Rendering Tabs with Namescopes
148
+
149
+ To swith namescope in your template, just pass the :namescope option to the <tt>tabs_tag</tt> helper method.
150
+
151
+ <% tabs_tag do |tab| %>
152
+ <%= tab.home 'Homepage', root_path %>
153
+ <%= tab.dashboard 'Dashboard', dashboard_path %>
154
+ <%= tab.projects 'Projects', projects_path %>
155
+ <% end %>
156
+
157
+ <% tabs_tag :namescope => :navigation do |tab| %>
158
+ <%= tab.first 'First', first_project_path %>
159
+ <%= tab.second 'Second', second_project_path %>
160
+ <%= tab.third 'Account', third_project_path %>
161
+ <% end %>
162
+
163
+ === Namescope scope
164
+
165
+ As a bonus feature, the namescope name does need to be unique within current request scope, not necessarily across the entire application.
166
+
167
+ Back to the previous example, you can reuse the same namescope in the other controllers. In this way, you can reuse your templates as well.
168
+
169
+ class HomeController
170
+ set_tab :home
171
+ end
172
+
173
+ class DashboardController
174
+ set_tab :dashboard
175
+
176
+ set_tab :index, :navigation, :only => %(index)
177
+ set_tab :common, :navigation, :only => %(foo bar)
178
+
179
+ # ...
180
+ end
181
+
182
+ class ProjectsController
183
+ set_tab :projects
184
+
185
+ set_tab :first, :navigation, :only => %(first)
186
+ set_tab :second, :navigation, :only => %(second)
187
+ set_tab :third, :navigation, :only => %(third)
188
+
189
+ # ...
190
+ end
191
+
87
192
  == Tab Builders
88
193
 
89
194
  The <tt>Builder</tt> is responsible for creating the tabs HTML code. This library is bundled with two <tt>Builders</tt>:
@@ -122,10 +227,10 @@ The following example creates a custom tab builder called MainTabBuilder.
122
227
 
123
228
  In your view, simply pass the builder class to the <tt>tabs_tag</tt> method.
124
229
 
125
- <% tabs_tag(MenuTabBuilder) do |tab| %>
126
- <%= tab.home, 'Homepage', root_path %>
127
- <%= tab.dashboard, 'Dashboard', dashboard_path %>
128
- <%= tab.account, 'Account', account_path, :style => 'float: right;' %>
230
+ <% tabs_tag(:builder => MenuTabBuilder) do |tab| %>
231
+ <%= tab.home 'Homepage', root_path %>
232
+ <%= tab.dashboard, 'Dashboard', dashboard_path %>
233
+ <%= tab.account 'Account', account_path, :style => 'float: right;' %>
129
234
  <% end %>
130
235
 
131
236
  This is the final result.
@@ -20,36 +20,120 @@ module TabsOnRails
20
20
 
21
21
  def self.included(base)
22
22
  base.extend ClassMethods
23
- base.send :helper, HelperMethods
24
23
  base.class_eval do
25
- attr_accessor :current_tab
24
+ include InstanceMethods
25
+ helper HelperMethods
26
26
  helper_method :current_tab
27
27
  end
28
28
  end
29
29
 
30
30
  module ClassMethods
31
31
 
32
- # Sets +name+ as +current_tab+.
33
- #
32
+ # Sets the value for current tab to given name.
33
+ #
34
+ # set_tab :foo
35
+ #
36
+ # If you need to manage multiple tabs, then you can pass an optional namespace.
37
+ #
38
+ # set_tab :foo, :namespace
39
+ #
40
+ # The <tt>set_tab</tt> method understands all options you are used to pass to a Rails controller filter.
41
+ # In fact, behind the scenes this method uses a <tt>before_filter</tt>
42
+ # to store the tab in the <tt>@tab_stack</tt> variable.
43
+ # For example, you can set the tab only for a restricted group of actions in the same controller
44
+ # using the <tt>:only</tt> and <tt>:except</tt> options.
45
+ #
34
46
  # ==== Examples
35
47
  #
36
- # tab :foo
37
- # tab :foo, :except => :new
38
- # tab :foo, :only => [ :index, :show ]
39
- #
40
- def current_tab(name, options = {})
48
+ # set_tab :foo
49
+ # set_tab :foo, :except => :new
50
+ # set_tab :foo, :only => [ :index, :show ]
51
+ #
52
+ # set_tab :foo, :namespace
53
+ # set_tab :foo, :namespace, :only => [ :index, :show ]
54
+ #
55
+ def set_tab(*args)
56
+ options = args.extract_options!
57
+ name, namespace = args
58
+
41
59
  before_filter(options) do |controller|
42
- controller.current_tab = name
60
+ controller.set_tab(name, namespace)
43
61
  end
44
62
  end
45
63
 
64
+ # This method is deprecated and exists only for compatibility with version 0.2.
65
+ # Please use <tt>set_tab</tt> method instead.
66
+ def current_tab(*args)
67
+ ActiveSupport::Deprecation.warn("Method current_tab is deprecated and will be removed in a future version. Please use set_tab instead.", caller)
68
+ set_tab(*args)
69
+ end
70
+
71
+ end
72
+
73
+ module InstanceMethods
74
+
75
+ # This method is deprecated and exists only for compatibility with version 0.2.
76
+ # Please use <tt>set_tab</tt> method instead of <tt>current_tab=</tt> setter method.
77
+ def current_tab=(name)
78
+ ActiveSupport::Deprecation.warn("Method current_tab= is deprecated and will be removed in a future version. Please use set_tab instead.", caller)
79
+ set_tab(name)
80
+ end
81
+
82
+ # Sets the value for current tab to given name.
83
+ # If you need to manage multiple tabs, then you can pass an optional namespace.
84
+ #
85
+ # ==== Examples
86
+ #
87
+ # set_tab :homepage
88
+ # set_tab :dashboard, :menu
89
+ #
90
+ def set_tab(name, namespace = nil)
91
+ tab_stack[namespace || :default] = name
92
+ end
93
+
94
+ # Returns the value for current tab in the default namespace,
95
+ # or nil if no tab has been set before.
96
+ # You can pass <tt>namespace</tt> get the value of current tab for a different namescope.
97
+ #
98
+ # ==== Examples
99
+ #
100
+ # current_tab # => nil
101
+ # current_tab :menu # => nil
102
+ #
103
+ # set_tab :homepage
104
+ # set_tab :dashboard, :menu
105
+ #
106
+ # current_tab # => :homepage
107
+ # current_tab :menu # => :dashboard
108
+ #
109
+ def current_tab(namespace = nil)
110
+ tab_stack[namespace || :default]
111
+ end
112
+
113
+ # Returns whether the current tab in <tt>namespace</tt> matches <tt>name</tt>.
114
+ def current_tab?(name, namespace = nil)
115
+ current_tab(namespace).to_s == name.to_s
116
+ end
117
+
118
+ # Initializes and/or returns the tab stack.
119
+ # You won't probably need to use this method directly
120
+ # unless you are trying to hack the plugin architecture.
121
+ def tab_stack
122
+ @tab_stack ||= {}
123
+ end
124
+
46
125
  end
47
126
 
48
127
  module HelperMethods
49
128
 
50
- def tabs_tag(builder = nil, &block)
129
+ def tabs_tag(options = {}, &block)
51
130
  raise LocalJumpError, "no block given" unless block_given?
52
- tabs = Tabs.new(self, builder)
131
+
132
+ unless options.is_a?(Hash)
133
+ ActiveSupport::Deprecation.warn('tabs_tag takes a Hash of options, no longer a builder class. Use :builder => BuilderClass.', caller)
134
+ options = { :builder => options }
135
+ end
136
+ tabs = Tabs.new(self, { :namespace => :default }.merge(options))
53
137
 
54
138
  concat(tabs.open_tabs.to_s)
55
139
  yield tabs
@@ -31,8 +31,11 @@ module TabsOnRails
31
31
  class Builder
32
32
 
33
33
  # Initializes a new builder with +context+.
34
- def initialize(context)
35
- @context = context
34
+ #
35
+ # Note. You should not overwrite this method to prevent incompatibility with future versions.
36
+ def initialize(context, options = {})
37
+ @context = context
38
+ @namespace = options.delete(:namespace) || :default
36
39
  end
37
40
 
38
41
  # Returns true if +tab+ is the +current_tab+.
@@ -49,8 +52,9 @@ module TabsOnRails
49
52
  # current_tab? 'bar' # => false
50
53
  #
51
54
  def current_tab?(tab)
52
- tab.to_s == @context.current_tab.to_s
55
+ tab.to_s == @context.current_tab(@namespace).to_s
53
56
  end
57
+
54
58
 
55
59
  # Creates and returns a tab with given +args+.
56
60
  #
@@ -111,9 +115,10 @@ module TabsOnRails
111
115
 
112
116
  end
113
117
 
114
- def initialize(context, builder = nil, &block)
118
+
119
+ def initialize(context, options = {}, &block)
115
120
  @context = context
116
- @builder = (builder || TabsBuilder).new(@context)
121
+ @builder = (options.delete(:builder) || TabsBuilder).new(@context, options)
117
122
  end
118
123
 
119
124
  %w(open_tabs close_tabs).each do |method|
@@ -18,7 +18,7 @@ module TabsOnRails
18
18
 
19
19
  module Version
20
20
  MAJOR = 0
21
- MINOR = 2
21
+ MINOR = 3
22
22
  TINY = 0
23
23
 
24
24
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -2,26 +2,27 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{tabs_on_rails}
5
- s.version = "0.2.0"
5
+ s.version = "0.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Simone Carletti"]
9
- s.date = %q{2009-02-17}
10
- s.description = %q{TabsOnRails is a simple Ruby on Rails plugin for creating and managing Tabs. It provides helpers for creating tabs with a flexible interface.}
9
+ s.date = %q{2009-05-01}
10
+ s.description = %q{ TabsOnRails is a simple Ruby on Rails plugin for creating and managing Tabs. It provides helpers for creating tabs with a flexible interface.
11
+ }
11
12
  s.email = %q{weppos@weppos.net}
12
13
  s.extra_rdoc_files = ["CHANGELOG.rdoc", "lib/tabs_on_rails/controller_mixin.rb", "lib/tabs_on_rails/tabs.rb", "lib/tabs_on_rails/version.rb", "lib/tabs_on_rails.rb", "LICENSE.rdoc", "README.rdoc"]
13
- s.files = ["CHANGELOG.rdoc", "init.rb", "install.rb", "lib/tabs_on_rails/controller_mixin.rb", "lib/tabs_on_rails/tabs.rb", "lib/tabs_on_rails/version.rb", "lib/tabs_on_rails.rb", "LICENSE.rdoc", "rails/init.rb", "Rakefile", "README.rdoc", "tabs_on_rails.gemspec", "tasks/tabs_on_rails_tasks.rake", "test/builder_test.rb", "test/controller_mixin_helpers_test.rb", "test/controller_mixin_test.rb", "test/fixtures/mixin/standard.html.erb", "test/tabs_builder_test.rb", "test/tabs_on_rails_test.rb", "test/test_helper.rb", "uninstall.rb", "Manifest"]
14
+ s.files = ["CHANGELOG.rdoc", "init.rb", "install.rb", "lib/tabs_on_rails/controller_mixin.rb", "lib/tabs_on_rails/tabs.rb", "lib/tabs_on_rails/version.rb", "lib/tabs_on_rails.rb", "LICENSE.rdoc", "rails/init.rb", "Rakefile", "README.rdoc", "tabs_on_rails.gemspec", "tasks/tabs_on_rails_tasks.rake", "test/builder_test.rb", "test/controller_mixin_helpers_test.rb", "test/controller_mixin_test.rb", "test/fixtures/mixin/standard.html.erb", "test/tabs_builder_test.rb", "test/tabs_on_rails_test.rb", "test/test_helper.rb", "uninstall.rb", "Manifest", "test/controller_test.rb"]
14
15
  s.has_rdoc = true
15
16
  s.homepage = %q{http://code.simonecarletti.com/tabsonrails}
16
17
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Tabs_on_rails", "--main", "README.rdoc"]
17
18
  s.require_paths = ["lib"]
18
- s.rubygems_version = %q{1.3.1}
19
+ s.rubygems_version = %q{1.3.2}
19
20
  s.summary = %q{A simple Ruby on Rails plugin for creating and managing Tabs.}
20
- s.test_files = ["test/builder_test.rb", "test/controller_mixin_helpers_test.rb", "test/controller_mixin_test.rb", "test/tabs_builder_test.rb", "test/tabs_on_rails_test.rb", "test/test_helper.rb"]
21
+ s.test_files = ["test/builder_test.rb", "test/controller_mixin_helpers_test.rb", "test/controller_mixin_test.rb", "test/controller_test.rb", "test/tabs_builder_test.rb", "test/tabs_on_rails_test.rb", "test/test_helper.rb"]
21
22
 
22
23
  if s.respond_to? :specification_version then
23
24
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
- s.specification_version = 2
25
+ s.specification_version = 3
25
26
 
26
27
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
28
  s.add_development_dependency(%q<rake>, [">= 0.8"])
data/test/builder_test.rb CHANGED
@@ -1,29 +1,82 @@
1
1
  require 'test_helper'
2
2
 
3
+ class BuilderTemplate
4
+ include ActionView::Helpers::TagHelper
5
+ include ActionView::Helpers::UrlHelper
6
+
7
+ def current_tab(namespace)
8
+ case namespace
9
+ when nil, :default
10
+ :dashboard
11
+ when :foospace
12
+ :footab
13
+ else
14
+ :elsetab
15
+ end
16
+ end
17
+ end
18
+
19
+
3
20
  class BuilderTest < ActiveSupport::TestCase
4
21
 
5
22
  def setup
6
- @template = Template.new
7
- @builder = TabsOnRails::Tabs::Builder.new(@template)
23
+ @template = BuilderTemplate.new
8
24
  end
9
25
 
26
+
10
27
  def test_initialize_should_require_context
11
28
  assert_raise(ArgumentError) { TabsOnRails::Tabs::Builder.new }
12
29
  assert_nothing_raised { TabsOnRails::Tabs::Builder.new(@template) }
13
30
  end
14
31
 
32
+ def test_initialize_should_allow_options
33
+ assert_nothing_raised { TabsOnRails::Tabs::Builder.new(@template, { :namespace => 'foonamespace' }) }
34
+ end
35
+
15
36
  def test_initialize_should_set_context
37
+ @builder = TabsOnRails::Tabs::Builder.new(@template)
16
38
  assert_equal(@template, @builder.instance_variable_get(:'@context'))
17
39
  end
18
-
19
- def test_current_tab_question_should_return_true_if_tab_matches_current_tab
40
+
41
+ def test_initialize_should_set_namespace
42
+ @builder = TabsOnRails::Tabs::Builder.new(@template)
43
+ assert_equal(:default, @builder.instance_variable_get(:'@namespace'))
44
+
45
+ @builder = TabsOnRails::Tabs::Builder.new(@template, :namespace => :foobar)
46
+ assert_equal(:foobar, @builder.instance_variable_get(:'@namespace'))
47
+ end
48
+
49
+
50
+ def test_current_tab
51
+ @builder = TabsOnRails::Tabs::Builder.new(@template)
20
52
  assert( @builder.current_tab?(:dashboard))
21
53
  assert( @builder.current_tab?('dashboard'))
22
54
  assert(!@builder.current_tab?(:welcome))
23
55
  assert(!@builder.current_tab?('welcome'))
24
56
  end
25
-
57
+
58
+ def test_current_tab_with_namespace
59
+ @builder = TabsOnRails::Tabs::Builder.new(@template, :namespace => :foospace)
60
+ assert( @builder.current_tab?(:footab))
61
+ assert( @builder.current_tab?('footab'))
62
+ assert(!@builder.current_tab?(:dashboard))
63
+ assert(!@builder.current_tab?('dashboard'))
64
+ end
65
+
66
+
67
+ def test_open_tabs
68
+ @builder = TabsOnRails::Tabs::Builder.new(@template)
69
+ assert_equal(nil, @builder.open_tabs)
70
+ end
71
+
72
+ def test_close_tabs
73
+ @builder = TabsOnRails::Tabs::Builder.new(@template)
74
+ assert_equal(nil, @builder.close_tabs)
75
+ end
76
+
77
+
26
78
  def test_tab_for_should_raise_not_implemented_error
79
+ @builder = TabsOnRails::Tabs::Builder.new(@template)
27
80
  assert_raise(NotImplementedError) { @builder.tab_for }
28
81
  assert_raise(NotImplementedError) { @builder.tab_for('foo') }
29
82
  assert_raise(NotImplementedError) { @builder.tab_for('foo', 'bar') }
@@ -1,5 +1,20 @@
1
1
  require 'test_helper'
2
2
 
3
+ class MockBuilder < TabsOnRails::Tabs::Builder
4
+
5
+ def initialize_with_mocha(*args)
6
+ checkpoint
7
+ initialize_without_mocha(*args)
8
+ end
9
+ alias_method_chain :initialize, :mocha
10
+
11
+ def checkpoint
12
+ end
13
+
14
+ def tab_for(tab, name, *args)
15
+ end
16
+ end
17
+
3
18
  class NilBoundariesBuilder < TabsOnRails::Tabs::Builder
4
19
  def tab_for(tab, name, *args)
5
20
  @context.content_tag(:span, name)
@@ -24,12 +39,26 @@ class ControllerMixinHelpersTest < ActionView::TestCase
24
39
  include ActionView::Helpers::UrlHelper
25
40
  include TabsOnRails::ControllerMixin::HelperMethods
26
41
 
42
+
27
43
  def test_tabs_tag_should_raise_local_jump_error_without_block
28
44
  assert_raise(LocalJumpError) { tabs_tag }
29
45
  end
30
46
 
47
+ def test_tabs_tag_with_builder
48
+ MockBuilder.any_instance.expects(:checkpoint).once
49
+ tabs_tag(:builder => MockBuilder) {}
50
+ end
51
+
52
+ def test_tabs_tag_with_namespace
53
+ tabs_tag(:builder => MockBuilder, :namespace => :custom) do |tabs|
54
+ builder = tabs.instance_variable_get(:'@builder')
55
+ assert_equal(:custom, builder.instance_variable_get(:'@namespace'))
56
+ end
57
+ end
58
+
59
+
31
60
  def test_tabs_tag_should_not_concat_open_close_tabs_when_nil
32
- content = tabs_tag(NilBoundariesBuilder) do |t|
61
+ content = tabs_tag(:builder => NilBoundariesBuilder) do |t|
33
62
  concat t.single('Single', '#')
34
63
  end
35
64
 
@@ -37,7 +66,7 @@ class ControllerMixinHelpersTest < ActionView::TestCase
37
66
  end
38
67
 
39
68
  def test_tabs_tag_should_not_concat_open_tabs_when_nil
40
- content = tabs_tag(NilOpenBoundaryBuilder) do |t|
69
+ content = tabs_tag(:builder => NilOpenBoundaryBuilder) do |t|
41
70
  concat t.single('Single', '#')
42
71
  end
43
72
 
@@ -45,11 +74,19 @@ class ControllerMixinHelpersTest < ActionView::TestCase
45
74
  end
46
75
 
47
76
  def test_tabs_tag_should_not_concat_close_tabs_when_nil
48
- content = tabs_tag(NilCloseBoundaryBuilder) do |t|
77
+ content = tabs_tag(:builder => NilCloseBoundaryBuilder) do |t|
49
78
  concat t.single('Single', '#')
50
79
  end
51
80
 
52
81
  assert_dom_equal('<br /><span>Single</span>', content)
53
82
  end
54
83
 
84
+
85
+ # Deprecated.
86
+ def test_deprecated_tabs_tag_with_builder
87
+ assert_deprecated do
88
+ tabs_tag(NilBoundariesBuilder) {}
89
+ end
90
+ end
91
+
55
92
  end
@@ -1,25 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class MixinController < ActionController::Base
4
- def self.controller_name; "mixin"; end
5
- def self.controller_path; "mixin"; end
6
-
7
- layout false
8
-
9
- current_tab :dashboard
10
- current_tab :welcome, :only => [ :action_welcome ]
11
-
12
- def method_missing(method, *args)
13
- if method =~ /^action_(.*)/
14
- render :action => (params[:template] || $1)
15
- end
16
- end
17
-
18
- def rescue_action(e) raise end
19
4
  end
20
5
 
21
- MixinController.view_paths = [ File.dirname(__FILE__) + "/fixtures/" ]
22
-
23
6
 
24
7
  class ControllerMixinTest < ActiveSupport::TestCase
25
8
 
@@ -28,29 +11,88 @@ class ControllerMixinTest < ActiveSupport::TestCase
28
11
  @request = ActionController::TestRequest.new
29
12
  @response = ActionController::TestResponse.new
30
13
  end
14
+
31
15
 
32
- def test_controller_should_include_current_tab_accessors
33
- assert_equal(nil, @controller.current_tab)
34
- @controller.current_tab = :footab
35
- assert_equal(:footab, @controller.current_tab)
16
+ def test_set_tab
17
+ @controller.set_tab :footab
18
+ assert_equal(:footab, @controller.tab_stack[:default])
36
19
  end
37
-
38
- def test_current_tab_should_be_dashboard
39
- get :action_dashboard, :template => 'standard'
40
- assert_equal(:dashboard, @controller.current_tab)
41
- assert_equal(%Q{<ul>
42
- <li><span>Dashboard</span></li>
43
- <li><a href="/w">Welcome</a></li>
44
- </ul>}, @response.body)
20
+
21
+ def test_set_tab_with_namespace
22
+ @controller.set_tab :footab, :namespace
23
+ assert_equal(:footab, @controller.tab_stack[:namespace])
45
24
  end
46
-
47
- def test_current_tab_should_be_welcome_only_for_action_welcone
48
- get :action_welcome, :template => 'standard'
49
- assert_equal(:welcome, @controller.current_tab)
50
- assert_equal(%Q{<ul>
51
- <li><a href="/d">Dashboard</a></li>
52
- <li><span>Welcome</span></li>
53
- </ul>}, @response.body)
25
+
26
+ def test_set_tab_with_default_namespace
27
+ @controller.set_tab :footab, :default
28
+ assert_equal(:footab, @controller.tab_stack[:default])
29
+ end
30
+
31
+ def test_set_tab_with_and_without_namespace
32
+ @controller.set_tab :firsttab
33
+ @controller.set_tab :secondtab, :custom
34
+ assert_equal(:firsttab, @controller.tab_stack[:default])
35
+ assert_equal(:secondtab, @controller.tab_stack[:custom])
36
+ end
37
+
38
+
39
+ def test_current_tab
40
+ @controller.tab_stack[:default] = :mytab
41
+ assert_equal(:mytab, @controller.current_tab)
42
+ end
43
+
44
+ def test_current_tab_with_namespace
45
+ @controller.tab_stack[:namespace] = :mytab
46
+ assert_equal(:mytab, @controller.current_tab(:namespace))
47
+ end
48
+
49
+ def test_current_tab_with_default_namespace
50
+ @controller.tab_stack[:default] = :mytab
51
+ assert_equal(:mytab, @controller.current_tab(:default))
52
+ end
53
+
54
+ def test_set_tab_with_and_without_namespace
55
+ @controller.tab_stack[:default] = :firsttab
56
+ @controller.tab_stack[:custom] = :secondtab
57
+ assert_equal(:firsttab, @controller.current_tab(:default))
58
+ assert_equal(:secondtab, @controller.current_tab(:custom))
59
+ end
60
+
61
+
62
+ def test_current_tab_question
63
+ @controller.tab_stack[:default] = :mytab
64
+ assert( @controller.current_tab?(:mytab))
65
+ assert(!@controller.current_tab?(:yourtab))
66
+ end
67
+
68
+ def test_current_tab_question_with_namespace
69
+ @controller.tab_stack[:custom] = :mytab
70
+ assert( @controller.current_tab?(:mytab, :custom))
71
+ assert(!@controller.current_tab?(:yourtab, :custom))
72
+ end
73
+
74
+ def test_current_tab_question_with_default_namespace
75
+ @controller.tab_stack[:default] = :mytab
76
+ assert( @controller.current_tab?(:mytab, :default))
77
+ assert(!@controller.current_tab?(:yourtab, :default))
78
+ end
79
+
80
+ def test_current_tab_question_with_and_without_namespace
81
+ @controller.tab_stack[:default] = :firsttab
82
+ @controller.tab_stack[:custom] = :secondtab
83
+ assert( @controller.current_tab?(:firsttab, :default))
84
+ assert(!@controller.current_tab?(:secondtab, :default))
85
+ assert( @controller.current_tab?(:secondtab, :custom))
86
+ assert(!@controller.current_tab?(:firsttab, :custom))
87
+ end
88
+
89
+
90
+ # Deprecated.
91
+ def test_deprecated_current_tab_setter
92
+ assert_deprecated do
93
+ @controller.current_tab = :footab
94
+ assert_equal(:footab, @controller.tab_stack[:default])
95
+ end
54
96
  end
55
97
 
56
98
  end
@@ -0,0 +1,76 @@
1
+ require 'test_helper'
2
+
3
+ class MixinController < ActionController::Base
4
+ def self.controller_name; "mixin"; end
5
+ def self.controller_path; "mixin"; end
6
+
7
+ layout false
8
+
9
+ set_tab :dashboard
10
+ set_tab :welcome, :only => %w(action_welcome)
11
+ set_tab :dashboard, :only => %w(action_namespace)
12
+ set_tab :homepage, :namespace, :only => %w(action_namespace)
13
+
14
+ # Deprecated.
15
+ current_tab :deprecated, :only => %w(action_current_tab)
16
+
17
+
18
+ def method_missing(method, *args)
19
+ if method =~ /^action_(.*)/
20
+ render :action => (params[:template] || 'standard')
21
+ end
22
+ end
23
+
24
+ def rescue_action(e) raise end
25
+ end
26
+
27
+ MixinController.view_paths = [ File.dirname(__FILE__) + "/fixtures/" ]
28
+
29
+
30
+ class ControllerMixinTest < ActiveSupport::TestCase
31
+
32
+ def setup
33
+ @controller = MixinController.new
34
+ @request = ActionController::TestRequest.new
35
+ @response = ActionController::TestResponse.new
36
+ end
37
+
38
+ def test_set_tab
39
+ get :action_dashboard
40
+ assert_equal(:dashboard, @controller.current_tab)
41
+ assert_equal(:dashboard, @controller.current_tab(:default))
42
+ assert_equal(%Q{<ul>
43
+ <li><span>Dashboard</span></li>
44
+ <li><a href="/w">Welcome</a></li>
45
+ </ul>}, @response.body)
46
+ end
47
+
48
+ def test_set_tab_with_only_option
49
+ get :action_welcome
50
+ assert_equal(:welcome, @controller.current_tab)
51
+ assert_equal(:welcome, @controller.current_tab(:default))
52
+ assert_equal(%Q{<ul>
53
+ <li><a href="/d">Dashboard</a></li>
54
+ <li><span>Welcome</span></li>
55
+ </ul>}, @response.body)
56
+ end
57
+
58
+ def test_set_tab_with_namespace
59
+ get :action_namespace
60
+ assert_equal(:dashboard, @controller.current_tab)
61
+ assert_equal(:dashboard, @controller.current_tab(:default))
62
+ assert_equal(:homepage, @controller.current_tab(:namespace))
63
+ assert_equal(%Q{<ul>
64
+ <li><span>Dashboard</span></li>
65
+ <li><a href="/w">Welcome</a></li>
66
+ </ul>}, @response.body)
67
+ end
68
+
69
+ # Deprecated
70
+ def test_deprecate_current_tab
71
+ get :action_current_tab
72
+ assert_equal(:deprecated, @controller.current_tab)
73
+ assert_equal(:deprecated, @controller.current_tab(:default))
74
+ end
75
+
76
+ end
@@ -1,17 +1,26 @@
1
1
  require 'test_helper'
2
2
 
3
- class Template
3
+ class TabBuilderTemplate
4
4
  include ActionView::Helpers::TagHelper
5
5
  include ActionView::Helpers::UrlHelper
6
6
 
7
- def current_tab; :dashboard; end
7
+ def current_tab(namespace)
8
+ case namespace
9
+ when nil, :default
10
+ :dashboard
11
+ when :foospace
12
+ :footab
13
+ else
14
+ :elsetab
15
+ end
16
+ end
8
17
  end
9
18
 
10
19
 
11
20
  class TabBuilderTest < ActiveSupport::TestCase
12
21
 
13
22
  def setup
14
- @template = Template.new
23
+ @template = TabBuilderTemplate.new
15
24
  @builder = TabsOnRails::Tabs::TabsBuilder.new(@template)
16
25
  end
17
26
 
data/test/test_helper.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
- require 'mocha'
3
+
4
+ # FIXME: tests raises an error with Rails 2.3.2,
5
+ # althought this library is 100% compatible with Rails 2.3.x
6
+ gem 'rails', '2.2.2'
4
7
 
5
8
  require 'active_support'
6
9
  require 'active_support/test_case'
@@ -8,16 +11,18 @@ require 'action_controller'
8
11
  require 'action_controller/cgi_ext'
9
12
  require 'action_controller/test_process'
10
13
  require 'action_view/test_case'
14
+ require 'mocha'
11
15
 
12
16
  $:.unshift File.dirname(__FILE__) + '/../lib'
13
17
 
18
+ RAILS_ROOT = '.' unless defined? RAILS_ROOT
19
+ RAILS_ENV = 'test' unless defined? RAILS_ENV
20
+ RAILS_DEFAULT_LOGGER = Logger.new(STDOUT) unless defined? RAILS_DEFAULT_LOGGER
21
+
14
22
  # simulate the inclusion as Rails does loading the init.rb file.
15
23
  require 'tabs_on_rails'
16
24
  require File.dirname(__FILE__) + '/../init.rb'
17
25
 
18
- RAILS_ROOT = '.' unless defined? RAILS_ROOT
19
- RAILS_ENV = 'test' unless defined? RAILS_ENV
20
-
21
26
  ActionController::Base.logger = nil
22
27
  ActionController::Routing::Routes.reload rescue nil
23
28
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weppos-tabs_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simone Carletti
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-17 00:00:00 -08:00
12
+ date: 2009-05-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -69,6 +69,7 @@ files:
69
69
  - test/test_helper.rb
70
70
  - uninstall.rb
71
71
  - Manifest
72
+ - test/controller_test.rb
72
73
  has_rdoc: true
73
74
  homepage: http://code.simonecarletti.com/tabsonrails
74
75
  post_install_message:
@@ -98,12 +99,13 @@ requirements: []
98
99
  rubyforge_project:
99
100
  rubygems_version: 1.2.0
100
101
  signing_key:
101
- specification_version: 2
102
+ specification_version: 3
102
103
  summary: A simple Ruby on Rails plugin for creating and managing Tabs.
103
104
  test_files:
104
105
  - test/builder_test.rb
105
106
  - test/controller_mixin_helpers_test.rb
106
107
  - test/controller_mixin_test.rb
108
+ - test/controller_test.rb
107
109
  - test/tabs_builder_test.rb
108
110
  - test/tabs_on_rails_test.rb
109
111
  - test/test_helper.rb