8/24/2014 - 4:09 PM

Proposal for a collection of menus for a rails project

Proposal for a collection of menus for a rails project

# A gem concept proposal for creating menu groups and their asscociated links

# The gem will rely heavily on URLHelper
# Whatever helper is associated with routing for reading paths... comments_path

# Notice how the add_link method behaves just link the link_to Rails method -- this is by design
# because we will use link_to to create links and buttons

# This could go directly in the view and be called as a partial

# Assumptions
# Method is always assumed to be GET unless otherwise specified -- method: post
# link_to unless button_to is specified -- type: :button

# http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
# Why Menu object does is not passed to a variable
# See === Object storage ==== 
SimpleMenuBuilder.Menu.new('menu_group_name') do |menu|
	# Standard link description
	# Controller is inferred if this helper is a view folder tied to a controller or
	# this is simply a standalone link
	menu.add_link name: 'The Internets', path: 'http://theinternets.com/path/on-the-internet.html'

	# you can use relative links
	menu.add_link name: 'The Internets', path: '/path/on-the-internet.html'

	# you can use route paths
	menu.add_link name: 'The Internets', path: 'profiles_path'

# Add module/helper to view
include SimpleMenuBuilder

# invoke menu group
<ul id="navigation">
	SimpleMenuBuilder.Menu('menu_group_name', opts = {tag: 'li', class: 'top-nav'})

# This call results in the following
<ul id="navigation">
	<li><a href="http://theinternets.com/path/on-the-internet.html">The Internets</a></li>
	<li><a href="/path/on-the-internet.html">The Internets</a></li>
	<li><a href="/profiles_path">The Internets</a></li>

# === Main Classes ===
class Link
	def add_link end

# I can now call add_link from Menu when creating a new menu
class Menu < Link
	def add_menu end

# === Object storage ====
# How is possible to recall menu group objects when in fact they are not stored as variables
# Example below illustrates this. This avoids variable naming collisions by wrapping them within the Module
class Menu
	attr_accessor :name
	@@menus = []
	def initialize(name)
		@@menus << self
		@name = name

	def self.all
		@@menus.each do |menu|


puts Menu.all.inspect
# => [#<Menu:0x007f92b308bc98 @name="comment">, #<Menu:0x007f92b308bc48 @name="profile">, #<Menu:0x007f92b308bbf8 @name="application">]