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
# HELPER.RB
# 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'
end
# INDEX.HTML.ERB
# Add module/helper to view
include SimpleMenuBuilder
# invoke menu group
<ul id="navigation">
SimpleMenuBuilder.Menu('menu_group_name', opts = {tag: 'li', class: 'top-nav'})
</ul>
# 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>
</ul>
# === Main Classes ===
class Link
def add_link end
end
# I can now call add_link from Menu when creating a new menu
class Menu < Link
def add_menu end
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
end
def self.all
@@menus.each do |menu|
menu.name
end
end
end
Menu.new('comment')
Menu.new('profile')
Menu.new('application')
puts Menu.all.inspect
# => [#<Menu:0x007f92b308bc98 @name="comment">, #<Menu:0x007f92b308bc48 @name="profile">, #<Menu:0x007f92b308bbf8 @name="application">]