tabs_on_rails 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ #
2
+ # = Tabs on Rails
3
+ #
4
+ # A simple Ruby on Rails plugin for creating and managing Tabs.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: TabsOnRails
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # License:: MIT License
11
+ #
12
+ #--
13
+ #
14
+ #++
15
+
16
+
17
+ module TabsOnRails
18
+
19
+ class Tabs
20
+
21
+ #
22
+ # = Builder
23
+ #
24
+ # The Builder class represents the interface for any custom Builder.
25
+ #
26
+ # To create a custom Builder extend this class
27
+ # and implement the following abstract methods:
28
+ #
29
+ # * tab_for(args)
30
+ #
31
+ class Builder
32
+
33
+ # Initializes a new builder with +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
39
+ end
40
+
41
+ # Returns true if +tab+ is the +current_tab+.
42
+ #
43
+ # ==== Examples
44
+ #
45
+ # class MyController < ApplicationController
46
+ # tab :foo
47
+ # end
48
+ #
49
+ # current_tab? :foo # => true
50
+ # current_tab? 'foo' # => true
51
+ # current_tab? :bar # => false
52
+ # current_tab? 'bar' # => false
53
+ #
54
+ def current_tab?(tab)
55
+ tab.to_s == @context.current_tab(@namespace).to_s
56
+ end
57
+
58
+
59
+ # Creates and returns a tab with given +args+.
60
+ #
61
+ # ==== Raises
62
+ #
63
+ # NotImplemented:: you should implement this method in your custom Builder.
64
+ #
65
+ def tab_for(*args)
66
+ raise NotImplementedError
67
+ end
68
+
69
+ # Overwrite this method to use a custom open tag for your tabs.
70
+ def open_tabs
71
+ end
72
+
73
+ # Overwrite this method to use a custom close tag for your tabs.
74
+ def close_tabs
75
+ end
76
+
77
+ end
78
+
79
+ #
80
+ # = Tabs Builder
81
+ #
82
+ # The TabsBuilder is and example of custom Builder.
83
+ # It creates a new tab
84
+ #
85
+ class TabsBuilder < Builder
86
+
87
+ # Implements Builder#tab_for.
88
+ # Returns a link_to +tab+ with +name+ and +options+ if +tab+ is not the current tab,
89
+ # a simple tab name wrapped by a span tag otherwise.
90
+ #
91
+ # current_tab? :foo # => true
92
+ #
93
+ # tab_for :foo, 'Foo', foo_path
94
+ # # => <li><span>Foo</span></li>
95
+ #
96
+ # tab_for :bar, 'Bar', bar_path
97
+ # # => <li><a href="/link/to/bar">Bar</a></li>
98
+ #
99
+ def tab_for(tab, name, options)
100
+ content = @context.link_to_unless(current_tab?(tab), name, options) do
101
+ @context.content_tag(:span, name)
102
+ end
103
+ @context.content_tag(:li, content)
104
+ end
105
+
106
+ # Implements Builder#open_tabs.
107
+ def open_tabs
108
+ '<ul>'
109
+ end
110
+
111
+ # Implements Builder#close_tabs.
112
+ def close_tabs
113
+ '</ul>'
114
+ end
115
+
116
+ end
117
+
118
+
119
+ def initialize(context, options = {}, &block)
120
+ @context = context
121
+ @builder = (options.delete(:builder) || TabsBuilder).new(@context, options)
122
+ end
123
+
124
+ %w(open_tabs close_tabs).each do |method|
125
+ define_method(method) do
126
+ @builder.send(method)
127
+ end
128
+ end
129
+
130
+ def method_missing(*args)
131
+ @builder.tab_for(*args)
132
+ end
133
+
134
+ end
135
+
136
+ end
@@ -0,0 +1,31 @@
1
+ #
2
+ # = Tabs on Rails
3
+ #
4
+ # A simple Ruby on Rails plugin for creating and managing Tabs.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: TabsOnRails
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # License:: MIT License
11
+ #
12
+ #--
13
+ #
14
+ #++
15
+
16
+
17
+ module TabsOnRails
18
+
19
+ module Version
20
+ MAJOR = 0
21
+ MINOR = 8
22
+ TINY = 1
23
+
24
+ STRING = [MAJOR, MINOR, TINY].join('.')
25
+ end
26
+
27
+ VERSION = Version::STRING
28
+ STATUS = 'beta'
29
+ BUILD = ''.match(/(\d+)/).to_a.first
30
+
31
+ end
@@ -0,0 +1,28 @@
1
+ #
2
+ # = Tabs on Rails
3
+ #
4
+ # A simple Ruby on Rails plugin for creating and managing Tabs.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: TabsOnRails
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # License:: MIT License
11
+ #
12
+ #--
13
+ #
14
+ #++
15
+
16
+
17
+ require 'tabs_on_rails/controller_mixin'
18
+ require 'tabs_on_rails/tabs'
19
+ require 'tabs_on_rails/version'
20
+
21
+
22
+ module TabsOnRails
23
+
24
+ NAME = 'Tabs on Rails'
25
+ GEM = 'tabs_on_rails'
26
+ AUTHOR = 'Simone Carletti <weppos@weppos.net>'
27
+
28
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'tabs_on_rails'
2
+
3
+ ActionController::Base.send :include, TabsOnRails::ControllerMixin
4
+
5
+ RAILS_DEFAULT_LOGGER.info("** TabsOnRails: initialized properly") if defined?(RAILS_DEFAULT_LOGGER)
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{tabs_on_rails}
5
+ s.version = "0.8.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Simone Carletti"]
9
+ s.date = %q{2009-10-09}
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
+ }
12
+ s.email = %q{weppos@weppos.net}
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"]
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", "Manifest", "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/controller_mixin_with_controller_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"]
15
+ s.homepage = %q{http://code.simonecarletti.com/tabsonrails}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Tabs_on_rails", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubygems_version = %q{1.3.4}
19
+ 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/controller_mixin_with_controller_test.rb", "test/tabs_builder_test.rb", "test/tabs_on_rails_test.rb", "test/test_helper.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<rake>, [">= 0.8"])
28
+ s.add_development_dependency(%q<echoe>, [">= 3.1"])
29
+ else
30
+ s.add_dependency(%q<rake>, [">= 0.8"])
31
+ s.add_dependency(%q<echoe>, [">= 3.1"])
32
+ end
33
+ else
34
+ s.add_dependency(%q<rake>, [">= 0.8"])
35
+ s.add_dependency(%q<echoe>, [">= 3.1"])
36
+ end
37
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :tabs_on_rails do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
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
+
20
+ class BuilderTest < ActiveSupport::TestCase
21
+
22
+ def setup
23
+ @template = BuilderTemplate.new
24
+ end
25
+
26
+
27
+ def test_initialize_should_require_context
28
+ assert_raise(ArgumentError) { TabsOnRails::Tabs::Builder.new }
29
+ assert_nothing_raised { TabsOnRails::Tabs::Builder.new(@template) }
30
+ end
31
+
32
+ def test_initialize_should_allow_options
33
+ assert_nothing_raised { TabsOnRails::Tabs::Builder.new(@template, { :namespace => 'foonamespace' }) }
34
+ end
35
+
36
+ def test_initialize_should_set_context
37
+ @builder = TabsOnRails::Tabs::Builder.new(@template)
38
+ assert_equal(@template, @builder.instance_variable_get(:'@context'))
39
+ end
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)
52
+ assert( @builder.current_tab?(:dashboard))
53
+ assert( @builder.current_tab?('dashboard'))
54
+ assert(!@builder.current_tab?(:welcome))
55
+ assert(!@builder.current_tab?('welcome'))
56
+ end
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
+
78
+ def test_tab_for_should_raise_not_implemented_error
79
+ @builder = TabsOnRails::Tabs::Builder.new(@template)
80
+ assert_raise(NotImplementedError) { @builder.tab_for }
81
+ assert_raise(NotImplementedError) { @builder.tab_for('foo') }
82
+ assert_raise(NotImplementedError) { @builder.tab_for('foo', 'bar') }
83
+ end
84
+
85
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
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
+
17
+ end
18
+
19
+ class NilBoundariesBuilder < TabsOnRails::Tabs::Builder
20
+ def tab_for(tab, name, *args)
21
+ @context.content_tag(:span, name)
22
+ end
23
+ end
24
+
25
+ class NilOpenBoundaryBuilder < NilBoundariesBuilder
26
+ def close_tabs
27
+ '<br />'
28
+ end
29
+ end
30
+
31
+ class NilCloseBoundaryBuilder < NilBoundariesBuilder
32
+ def open_tabs
33
+ '<br />'
34
+ end
35
+ end
36
+
37
+
38
+ class ControllerMixinHelpersTest < ActionView::TestCase
39
+ tests TabsOnRails::ControllerMixin::HelperMethods
40
+ include ActionView::Helpers::TagHelper
41
+ include ActionView::Helpers::UrlHelper
42
+
43
+
44
+ def test_tabs_tag_should_raise_local_jump_error_without_block
45
+ assert_raise(LocalJumpError) { tabs_tag }
46
+ end
47
+
48
+ def test_tabs_tag_with_builder
49
+ MockBuilder.any_instance.expects(:checkpoint).once
50
+ tabs_tag(:builder => MockBuilder) {}
51
+ end
52
+
53
+ def test_tabs_tag_with_namespace
54
+ MockBuilder.any_instance.expects(:checkpoint).once
55
+ tabs_tag(:builder => MockBuilder, :namespace => :custom) do |tabs|
56
+ builder = tabs.instance_variable_get(:'@builder')
57
+ assert_equal(:custom, builder.instance_variable_get(:'@namespace'))
58
+ end
59
+ end
60
+
61
+
62
+ def test_tabs_tag_should_not_concat_open_close_tabs_when_nil
63
+ content = tabs_tag(:builder => NilBoundariesBuilder) do |t|
64
+ concat t.single('Single', '#')
65
+ end
66
+
67
+ assert_dom_equal('<span>Single</span>', content)
68
+ end
69
+
70
+ def test_tabs_tag_should_not_concat_open_tabs_when_nil
71
+ content = tabs_tag(:builder => NilOpenBoundaryBuilder) do |t|
72
+ concat t.single('Single', '#')
73
+ end
74
+
75
+ assert_dom_equal('<span>Single</span><br />', content)
76
+ end
77
+
78
+ def test_tabs_tag_should_not_concat_close_tabs_when_nil
79
+ content = tabs_tag(:builder => NilCloseBoundaryBuilder) do |t|
80
+ concat t.single('Single', '#')
81
+ end
82
+
83
+ assert_dom_equal('<br /><span>Single</span>', content)
84
+ end
85
+
86
+ end
@@ -0,0 +1,90 @@
1
+ require 'test_helper'
2
+
3
+ class ControllerMixinTest < ActionController::TestCase
4
+ include ControllerTestHelpers
5
+
6
+ class MixinController < ActionController::Base
7
+ end
8
+
9
+ def setup
10
+ @controller = MixinController.new
11
+ @controller_proxy = ControllerProxy.new(@controller)
12
+ @request = ActionController::TestRequest.new
13
+ @response = ActionController::TestResponse.new
14
+ end
15
+
16
+
17
+ def test_set_tab
18
+ controller.set_tab :footab
19
+ assert_equal(:footab, controller.tab_stack[:default])
20
+ end
21
+
22
+ def test_set_tab_with_namespace
23
+ controller.set_tab :footab, :namespace
24
+ assert_equal(:footab, controller.tab_stack[:namespace])
25
+ end
26
+
27
+ def test_set_tab_with_default_namespace
28
+ controller.set_tab :footab, :default
29
+ assert_equal(:footab, controller.tab_stack[:default])
30
+ end
31
+
32
+ def test_set_tab_with_and_without_namespace
33
+ controller.set_tab :firsttab
34
+ controller.set_tab :secondtab, :custom
35
+ assert_equal(:firsttab, controller.tab_stack[:default])
36
+ assert_equal(:secondtab, controller.tab_stack[:custom])
37
+ end
38
+
39
+
40
+ def test_current_tab
41
+ controller.tab_stack[:default] = :mytab
42
+ assert_equal(:mytab, controller.current_tab)
43
+ end
44
+
45
+ def test_current_tab_with_namespace
46
+ controller.tab_stack[:namespace] = :mytab
47
+ assert_equal(:mytab, controller.current_tab(:namespace))
48
+ end
49
+
50
+ def test_current_tab_with_default_namespace
51
+ controller.tab_stack[:default] = :mytab
52
+ assert_equal(:mytab, controller.current_tab(:default))
53
+ end
54
+
55
+ def test_set_tab_with_and_without_namespace
56
+ controller.tab_stack[:default] = :firsttab
57
+ controller.tab_stack[:custom] = :secondtab
58
+ assert_equal(:firsttab, controller.current_tab(:default))
59
+ assert_equal(:secondtab, controller.current_tab(:custom))
60
+ end
61
+
62
+
63
+ def test_current_tab_question
64
+ controller.tab_stack[:default] = :mytab
65
+ assert( controller.current_tab?(:mytab))
66
+ assert(!controller.current_tab?(:yourtab))
67
+ end
68
+
69
+ def test_current_tab_question_with_namespace
70
+ controller.tab_stack[:custom] = :mytab
71
+ assert( controller.current_tab?(:mytab, :custom))
72
+ assert(!controller.current_tab?(:yourtab, :custom))
73
+ end
74
+
75
+ def test_current_tab_question_with_default_namespace
76
+ controller.tab_stack[:default] = :mytab
77
+ assert( controller.current_tab?(:mytab, :default))
78
+ assert(!controller.current_tab?(:yourtab, :default))
79
+ end
80
+
81
+ def test_current_tab_question_with_and_without_namespace
82
+ controller.tab_stack[:default] = :firsttab
83
+ controller.tab_stack[:custom] = :secondtab
84
+ assert( controller.current_tab?(:firsttab, :default))
85
+ assert(!controller.current_tab?(:secondtab, :default))
86
+ assert( controller.current_tab?(:secondtab, :custom))
87
+ assert(!controller.current_tab?(:firsttab, :custom))
88
+ end
89
+
90
+ end