Class: Lolita::Navigation::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/lolita/navigation/branch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Branch

Returns a new instance of Branch.



9
10
11
12
13
14
15
# File 'lib/lolita/navigation/branch.rb', line 9

def initialize(*args)
  
  @options=args ? args.extract_options! : {}
  set_object(args||[])
  set_default_values
  assign_attributes_from_options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/lolita/navigation/branch.rb', line 17

def method_missing method_name, *args
  if @options.keys.include?(method_name) || @options.keys.include?(method_name.to_s)
    @options[method_name] || @options[method_name.to_s]
  else
    super
  end
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/lolita/navigation/branch.rb', line 6

def level
  @level
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/lolita/navigation/branch.rb', line 5

def name
  @name
end

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/lolita/navigation/branch.rb', line 5

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/lolita/navigation/branch.rb', line 6

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/lolita/navigation/branch.rb', line 6

def parent
  @parent
end

#titleObject



25
26
27
28
29
30
31
32
33
# File 'lib/lolita/navigation/branch.rb', line 25

def title
  if @title && @title.respond_to?(:call)
    @title.call(self)
  else
    @title || 
    (self.object.to && (self.object.to.lolita_model_name.human(:count=>2)) || self.object.to.to_s) ||
    ::I18n.t("lolita.navigation.#{object.name}")
  end
end

#treeObject

Returns the value of attribute tree.



6
7
8
# File 'lib/lolita/navigation/branch.rb', line 6

def tree
  @tree
end

Class Method Details

.get_or_create(*args) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/lolita/navigation/branch.rb', line 145

def self.get_or_create(*args)
  options=args ? args.extract_options! : {}
  args||=[]
  possible_object=args[0]

  if possible_object.is_a?(String)
    self.new(possible_object,options.merge(:title=>possible_object))
  elsif possible_object.is_a?(self)
    possible_object
  else
    self.new(possible_object,options)
  end
end

Instance Method Details

#active?(view) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lolita/navigation/branch.rb', line 117

def active?(view)
  resource = view.respond_to?(:resource_class) ? view.send(:resource_class) : nil rescue nil
  request = view.send(:request)
  self_active = if self.object.is_a?(Lolita::Mapping) && self.object && self.object.to == resource
    true
  elsif self.options[:active].respond_to?(:call)
    self.options[:active].call(view,self)
  elsif self.options[:url]
    self.options[:url] == request.path
  end
  self_active || (self.children.any? && self.children.branches.detect{|c_branch| c_branch.active?(view)})
end

#after(*args) ⇒ Object



105
106
107
# File 'lib/lolita/navigation/branch.rb', line 105

def after(*args)
  move_to(:after,*args)
end

#append(*args) ⇒ Object



101
102
103
# File 'lib/lolita/navigation/branch.rb', line 101

def append(*args)
  move_to(:append,*args)
end

#before(*args) ⇒ Object



109
110
111
# File 'lib/lolita/navigation/branch.rb', line 109

def before(*args)
  move_to(:before,*args)
end

#calculate_url(view) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lolita/navigation/branch.rb', line 72

def calculate_url(view)
  if self.options[:url].respond_to?(:call)
    self.options[:url].call(view,self)
  elsif self.options[:url]
    self.options[:url]
  elsif self.object.is_a?(Lolita::Mapping)
    view.send(:lolita_resources_path, self.object) 
  else
    self.first_url_in_subtree(view)
  end
end

#childrenObject



40
41
42
43
44
45
46
47
# File 'lib/lolita/navigation/branch.rb', line 40

def children
  unless @children
    tree=Lolita::Navigation::Tree.new("#{name}_children_tree")
    tree.set_parent(self)
    @children=tree
  end
  @children
end

#first_url_in_subtree(view) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/lolita/navigation/branch.rb', line 84

def first_url_in_subtree(view)
  if self.subtree?
    subtree_branch = self.children.branches.detect{|branch|
      branch.visible?(view)
    }
    subtree_branch.calculate_url(view) if subtree_branch
  end
end

#get_or_create(*args) ⇒ Object



159
160
161
# File 'lib/lolita/navigation/branch.rb', line 159

def get_or_create(*args)
  self.class.get_or_create(*args)
end

#indexObject



60
61
62
# File 'lib/lolita/navigation/branch.rb', line 60

def index
  self.tree.get_branch_index(self)
end

#populate_url(view) ⇒ Object



68
69
70
# File 'lib/lolita/navigation/branch.rb', line 68

def populate_url(view)
  self.options[:calculated_url] = calculate_url(view)
end

#prepend(*args) ⇒ Object



113
114
115
# File 'lib/lolita/navigation/branch.rb', line 113

def prepend(*args)
  move_to(:prepend,*args)
end

#self_with_childrenObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/lolita/navigation/branch.rb', line 49

def self_with_children
  if block_given?
    yield self
    @children.each do |branch|
      yield branch
    end
  else
    [self]+@children.map{|b| b}
  end
end

#siblingsObject



93
94
95
96
97
98
99
# File 'lib/lolita/navigation/branch.rb', line 93

def siblings
  index=self.index
  {
    :before=>self.tree.branches[index-1],
    :after=>self.tree.branches[index+1]
  }
end

#subtree?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/lolita/navigation/branch.rb', line 64

def subtree?
  self.children.branches.any?
end

#visible?(view) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/lolita/navigation/branch.rb', line 130

def visible?(view)
  self_visible = if self.object && self.object.respond_to?(:to)
    view.send(:authorization_proxy).send(:can?,:read,self.object.to)
  elsif self.options[:visible]
    if self.options[:visible].respond_to?(:call)
      self.options[:visible].call(view,self,branch)
    else
      self.options[:visible]
    end
  else
    true
  end
  self_visible && (self.children.any? && self.children.visible?(view) || self.children.empty?)
end