Class: Tabulous::Tab

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTab

Returns a new instance of Tab.



8
9
10
11
12
13
14
# File 'lib/tabulous/tab.rb', line 8

def initialize
  @subtabs = []
  @active_actions = {}
  @kind = :primary_tab
  @declared_to_have_subtabs = false
  @http_verb = :get
end

Instance Attribute Details

#declared_to_have_subtabsObject

Returns the value of attribute declared_to_have_subtabs.



5
6
7
# File 'lib/tabulous/tab.rb', line 5

def declared_to_have_subtabs
  @declared_to_have_subtabs
end

#enabled_when=(value) ⇒ Object (writeonly)

Sets the attribute enabled_when

Parameters:

  • value

    the value to set the attribute enabled_when to.



6
7
8
# File 'lib/tabulous/tab.rb', line 6

def enabled_when=(value)
  @enabled_when = value
end

#http_verb(view = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tabulous/tab.rb', line 50

def http_verb(view=nil)
  value = if @http_verb.respond_to?(:call)
    view.instance_exec(&@http_verb)
  else
    @http_verb
  end
  value = value.to_s.downcase.to_sym
  unless [:get, :post, :delete, :patch, :put].include?(value)
    raise ImproperValueError, "The http_verb of tab '#{self.name}' must be :get, :post, :delete, :patch or :put."
  end
  value
end

#kindObject

Returns the value of attribute kind.



5
6
7
# File 'lib/tabulous/tab.rb', line 5

def kind
  @kind
end


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tabulous/tab.rb', line 38

def link_path(view=nil)
  value = if @link_path.respond_to?(:call)
    view.instance_exec(&@link_path)
  else
    @link_path
  end
  unless value.is_a?(String)
    raise ImproperValueError, "The link_path of tab '#{self.name}' needs to be a string."
  end
  value
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/tabulous/tab.rb', line 5

def name
  @name
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/tabulous/tab.rb', line 4

def parent
  @parent
end

#subtabsObject

Returns the value of attribute subtabs.



5
6
7
# File 'lib/tabulous/tab.rb', line 5

def subtabs
  @subtabs
end

#text(view = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tabulous/tab.rb', line 26

def text(view=nil)
  value = if @text.respond_to?(:call)
    view.instance_exec(&@text)
  else
    @text
  end
  unless value.is_a?(String)
    raise ImproperValueError, "The text of tab '#{self.name}' needs to be a string."
  end
  value
end

#visible_when=(value) ⇒ Object (writeonly)

Sets the attribute visible_when

Parameters:

  • value

    the value to set the attribute visible_when to.



6
7
8
# File 'lib/tabulous/tab.rb', line 6

def visible_when=(value)
  @visible_when = value
end

Instance Method Details

#active?(view) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
# File 'lib/tabulous/tab.rb', line 93

def active?(view)
  current_controller = view.controller_path.to_s
  current_action = view.action_name.to_s
  active_actions = @active_actions[current_controller]
  return true if active_actions && (active_actions.include?('any') || active_actions.include?(current_action))
  return true if @subtabs.any? { |subtab| subtab.active?(view) }
  false
end

#active_actions_overlap?(tab) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tabulous/tab.rb', line 102

def active_actions_overlap?(tab)
  for controller in @active_actions.keys
    other_actions = tab.active_actions[controller]
    if other_actions
      these_actions = @active_actions[controller]
      if (!(other_actions & these_actions).empty?) ||
         other_actions.include?('any') ||
         these_actions.include?('any')
        return true
      end
    end
  end
  false
end

#add_active_actions(controller, actions) ⇒ Object

controller actions that make this tab active



84
85
86
87
88
89
90
91
# File 'lib/tabulous/tab.rb', line 84

def add_active_actions(controller, actions)
  controller = controller.to_s
  actions = [actions] if !actions.is_a?(Array)
  for action in actions
    @active_actions[controller] ||= []
    @active_actions[controller] << action.to_s
  end
end

#clickable?(view) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/tabulous/tab.rb', line 79

def clickable?(view)
  enabled?(view) && (!active?(view) || Config.active_tab_clickable)
end

#enabled?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/tabulous/tab.rb', line 71

def enabled?(view=nil)
  if @enabled_when.respond_to?(:call)
    view.instance_exec(&@enabled_when)
  else
    @enabled_when
  end
end

#subtab?Boolean

Returns:

  • (Boolean)


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

def subtab?
  @kind == :subtab
end

#visible?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/tabulous/tab.rb', line 63

def visible?(view=nil)
  if @visible_when.respond_to?(:call)
    view.instance_exec(&@visible_when)
  else
    @visible_when
  end
end