Class: TabBuilder::Tab

Inherits:
Object
  • Object
show all
Defined in:
lib/tab_builder/tab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, context, &block) ⇒ Tab

Returns a new instance of Tab.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tab_builder/tab.rb', line 5

def initialize(name, options, context, &block)
  @name = name
  @options = options
  @context = context
  @current = nil
  
  @paths = []
  @controller = nil
  
  # Copy over context instance variables
  @context.instance_variables.each do |iv|
    instance_variable_set(iv, @context.instance_variable_get(iv)) unless instance_variable_defined? iv
  end
  
  instance_eval &block if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/tab_builder/tab.rb', line 53

def method_missing(name, *args, &block)
  # Pass methods along to the context, if it responds to them (allows for helpers, etc. to be used):
  if @context.respond_to? name
    @context.send(name, *args, &block)
  else
    super(name, *args, &block)
  end
end

Instance Attribute Details

#controller(name = nil) ⇒ Object

Getter/setter for controller



38
39
40
# File 'lib/tab_builder/tab.rb', line 38

def controller
  @controller
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/tab_builder/tab.rb', line 3

def options
  @options
end

#pathsObject

Returns the value of attribute paths.



3
4
5
# File 'lib/tab_builder/tab.rb', line 3

def paths
  @paths
end

Instance Method Details

#current(&block) ⇒ Object

Override how to specify a current tab



44
45
46
47
# File 'lib/tab_builder/tab.rb', line 44

def current(&block)
  @current = block if block_given?
  @current
end

#nameObject



22
23
24
# File 'lib/tab_builder/tab.rb', line 22

def name
  I18n.t("#{options.string_prefix}#{@name}")  
end

#path(url, options = {}) ⇒ Object

Define the path(s) for this tab (ie. what paths correspond to this tab)



27
28
29
30
# File 'lib/tab_builder/tab.rb', line 27

def path(url, options = {})
  url = url_for(url.merge(:only_path => false)) if url.is_a? Hash
  @paths << OpenHash.new({ :url => url, :method => options.delete(:method) || :get }) 
end

#rootObject

Get the root path (ie. what path the tab links to



33
34
35
# File 'lib/tab_builder/tab.rb', line 33

def root
  @paths.first
end

#tooltipObject



49
50
51
# File 'lib/tab_builder/tab.rb', line 49

def tooltip
  @options[:tooltip]
end