Skip to content

Commit

Permalink
add feature: set default builder for Tabs
Browse files Browse the repository at this point in the history
  with this feature, we don't need adding argument like :builder =>
CustomBuilder on tabs_tag any more, this woule be useful if we call
 tabs_tag many times.
  • Loading branch information
ilstar committed May 23, 2012
1 parent a91a476 commit e0f924a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/tabs_on_rails/tabs.rb
Expand Up @@ -15,9 +15,12 @@ module TabsOnRails

class Tabs

mattr_accessor :default_builder
@@default_builder = TabsBuilder

def initialize(context, options = {})
@context = context
@builder = (options.delete(:builder) || TabsBuilder).new(@context, options)
@builder = (options.delete(:builder) || self.class.default_builder).new(@context, options)
@options = options
end

Expand Down
28 changes: 28 additions & 0 deletions test/unit/tabs_test.rb
Expand Up @@ -43,6 +43,17 @@ def test_initialize_with_option_builder
assert_instance_of builder, @tabs.instance_variable_get(:"@builder")
end

def test_initialize_with_default_builder
default_builder = TabsOnRails::Tabs.default_builder
TabsOnRails::Tabs.default_builder = CustomBuilder

custom_tab = TabsOnRails::Tabs.new(@template) # doesnt need argument :builder => CustomBuilder

assert_instance_of CustomBuilder, custom_tab.instance_variable_get(:"@builder")

TabsOnRails::Tabs.default_builder = default_builder
end


def test_open_tabs
assert_equal '<ul>', @tabs.open_tabs
Expand Down Expand Up @@ -91,4 +102,21 @@ def test_tab_for_with_block
assert_dom_equal expected, result
end

class CustomBuilder < TabsOnRails::Tabs::Builder
def open_tabs(options = {})
@context.tag("ul", options, open = true)
end

def close_tabs(options = {})
"</ul>".html_safe
end

def tab_for(tab, name, options, item_options = {})
item_options[:class] = (current_tab?(tab) ? 'active' : '')
@context.content_tag(:li, item_options) do
@context.link_to(name, options)
end
end
end

end

0 comments on commit e0f924a

Please sign in to comment.