menu_txt

If you have non-technical people who want to be able to edit menus on your website, this is the gem for you.

Usage

The gem is not rails-dependent, but here's a good example of rails usage.

Throw a text file somewhere, say app/menus/my_awesome_menu.txt.

1st level thing 1 | /foo1

- 2nd level thing A | /foo1/bar1
-- 3rd level thing A | /foo1/bar1/baz1
-- 3rd level thing B | /foo1/bar1/baz2

- 2nd level thing B | /foo1/bar2
-- 3rd level thing A | /foo1/bar2/baz1

- 2nd level thing C | /foo1/bar3

1st level thing 2 | /foo2

You can place that file anywhere you like, but placing all the menus in one dir is a good idea, because we can add this line to development.rb.

config.watchable_dirs['app/menus'] = [:txt]

This line ensures that all changes to txt files in app/menus will cause your rails app to reload, so you can see changes with a browser refresh.

Next, create a class for your menu, for example it could be app/models/my_awesome_menu.rb. Put this code into it.

MyAwesomeMenu = MenuTxt.parse_path('app/menus/my_awesome_menu.txt')

And now we just need a partial which will render itself recursively for every submenu. For example, let's create app/views/my_awesome_menu/_nodes.html.erb.

<% nodes.each do |node| %>
<li <%=raw node.leaf? ? '' : 'class="dropdown-submenu"' %>>
  <%= link_to node.name, node.url %>

  <% unless node.leaf? %>
  <ul class="dropdown-menu">
    <%= render 'my_awesome_menu/nodes', nodes: node.children %>
  </ul>
  <% end %>
</li>
<% end %>

Notice how I use leaf? above to determine if there are no more submenus under this node.

Finally, wherever you want to render this menu in your html, all you gotta do is the following.

<ul class="dropdown-menu">
  <%= render 'my_awesome_menu/nodes', nodes: MyAwesomeMenu.children %>
</ul>

So given you have css/javascript hooked to classes dropdown-menu and dropdown-submenu, everything would just work. Now try editing my_awesome_menu.txt and refreshing the page. That's it, now your marketing people could edit the a plain text file on github and commit it without your involvement.

Advanced usage

Traversing nodes

The MyAwesomeMenu in the above example is fully Enumerable and if each is called without a block it returns a proper iterator. The order of iteration is exactly like reading the text file top to bottom.

Extending nodes

If you want each node in your menu to have special methods, you can use your own node class. Simply create it kinda like this.

class MySpecialMenuNode
  include MenuTxt::Node

  def highlight?
    url.include?('special_deals')
  end
end

The MenuTxt::Node is a convenient module that gives you all the node boilerplate. Then, when you create the menu model, you can pass your node object as the second argument, and it will become root of the menu, which acts as the prototype for all nested nodes.

MyAwesomeMenu =
  MenuTxt.parse_path('app/menus/menu.txt', MySpecialMenuNode.new(nil))

Installation

Add this line to your application's Gemfile:

gem 'menu_txt'

And then execute:

$ bundle

Or install it yourself as:

$ gem install menu_txt

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/maxim/menu_txt/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request