Class: SimpleNavigation::Configuration::Builder::Navigation::Menu

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

Overview

Menu builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, title = nil, options = {}) ⇒ Menu

Returns a new instance of Menu.



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/simple_navigation.rb', line 174

def initialize(name, title = nil, options = {})
  self.id = [options[:prefix], name].join('_')
  self.menus = []
  self.name = name
  self.title = title
  self.translation = [options[:translation], name].join('.')
  self.url = options[:url]
  self.urls = []
  options.delete(:translation)
  options.delete(:url)
  self.options = options
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



172
173
174
# File 'lib/simple_navigation.rb', line 172

def id
  @id
end

Returns the value of attribute menus.



172
173
174
# File 'lib/simple_navigation.rb', line 172

def menus
  @menus
end

#nameObject

Returns the value of attribute name.



172
173
174
# File 'lib/simple_navigation.rb', line 172

def name
  @name
end

#optionsObject

Returns the value of attribute options.



172
173
174
# File 'lib/simple_navigation.rb', line 172

def options
  @options
end

#titleObject

Returns the value of attribute title.



172
173
174
# File 'lib/simple_navigation.rb', line 172

def title
  @title
end

#translationObject

Returns the value of attribute translation.



172
173
174
# File 'lib/simple_navigation.rb', line 172

def translation
  @translation
end

#urlObject

Returns the value of attribute url.



172
173
174
# File 'lib/simple_navigation.rb', line 172

def url
  @url
end

#urlsObject

Returns the value of attribute urls.



172
173
174
# File 'lib/simple_navigation.rb', line 172

def urls
  @urls
end

Instance Method Details

#buildObject



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/simple_navigation.rb', line 204

def build
  menu = { :id => self.id.to_sym, :name => self.name.to_sym,
    :options => self.options }
  # Add keys with values only:
  menu.merge!(:menus => self.menus) unless self.menus.empty?
  menu.merge!(:title => self.title) unless self.title.nil?
  menu.merge!(:translation => [self.translation, 'title'].join('.')) if self.options[:i18n] == true
  menu.merge!(:url => self.url) unless self.url.nil?
  menu.merge!(:urls => self.urls) unless self.urls.empty?
  # Return menu hash
  menu
end

#connect(options = {}) ⇒ Object



199
200
201
202
# File 'lib/simple_navigation.rb', line 199

def connect(options = {})
  options[:controller] = self.url[:controller] unless options.has_key?(:controller)
  self.urls << options
end

Create a new child menu

Yields:



188
189
190
191
192
193
194
195
196
197
# File 'lib/simple_navigation.rb', line 188

def menu(name, *args, &block)
  title = args.first.is_a?(String) ? args.first : nil
  options = args.last.is_a?(::Hash) ? args.last : {}
  options.merge!(:i18n => self.options[:i18n])
  options.merge!(:translation => [self.translation, 'menus'].join('.'))
  options.merge!(:prefix => self.id)
  menu = Menu.new(name, title, options)
  yield menu if block
  self.menus << menu.build
end