forked from weppos/tabs_on_rails
/
tabs.rb
61 lines (49 loc) · 1.71 KB
/
tabs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#
# = Tabs on Rails
#
# A simple Ruby on Rails plugin for creating and managing Tabs.
#
#
# Category:: Rails
# Package:: TabsOnRails
# Author:: Simone Carletti <weppos@weppos.net>
# License:: MIT License
#
#--
#
#++
require 'tabs_on_rails/tabs/builder'
require 'tabs_on_rails/tabs/tabs_builder'
module TabsOnRails
class Tabs
def initialize(context, options = {})
@context = context
@builder = (options.delete(:builder) || TabsBuilder).new(@context, options)
@options = options
end
%w(open_tabs close_tabs).each do |name|
define_method(name) do |*args| # def open_tabs(*args)
method = @builder.method(name) # method = @builder.method(:open_tabs)
if method.arity.zero? # if method.arity.zero?
method.call # method.call
else # else
method.call(*args) # method.call(*args)
end # end
end # end
end
def method_missing(*args, &block)
@builder.tab_for(*args, &block)
end
def render(&block)
raise LocalJumpError, "no block given" unless block_given?
options = @options.dup
open_tabs_options = options.delete(:open_tabs) || {}
close_tabs_options = options.delete(:close_tabs) || {}
"".tap do |html|
html << open_tabs(open_tabs_options).to_s
html << @context.capture(self, &block)
html << close_tabs(close_tabs_options).to_s
end.html_safe
end
end
end