Ruby: Mixin Template

This is the basic structure I use to create a mixin in Ruby:

# = Mixin Template
# == Usage
# ActionController::Base.send :include, MixinModuleName
module MixinModuleName
  def self.included(recipient)
    recipient.class_eval do
      include InstanceMethods
  module InstanceMethods
  module ClassMethods
Ruby: Mixin Template

Git: Creating a Remote Branch (and track it locally)

Required steps to create and track locally a remote branch with git:

# Create new remote branch
git push origin origin:refs/heads/new_branch_name
# Make sure everything is updated
git fetch origin
# Check your branch has been created
git branch -r
# Track a remote branch
git branch --track new_branch_name origin/new_branch_name
# Checkout remote branch
git checkout new_branch_name
Git: Creating a Remote Branch (and track it locally)

Gem: Simple Navigation (Navigation menu builder for Ruby on Rails)

I’ve just released my new gem simple_navigation. This gem help’s you to create a navigation menu for your Ruby on Rails application.


Edit your config/environment.rb to setup simple_navigation gem:

config.gem "mexpolk-simple_navigation",
    :lib => "simple_navigation",
    :source => ""

And from the command line, install the plugin:

rake gems:install


To create your menus create a new file named config/initializers/simple_navigation.rb like this:

SimpleNavigation::Builder.config do |map|
  map.navigation :default do |navigation|

    # Root menu without child elements (menus) that points to /dashboard :home, :url => { :controller => "home", :action => "index"}

    # Root menu with child menus without anchor link :contacts do |contacts|

      # Child menu with many possible urls (or many controllers and actions) :list, :url => { :controller => "contacts", :action => "index" } do |contact_list|

        # This menu will marked as current when you're on the following
        # controllers and actions (including the controller and action
        # specified in the :url option):
        contact_list.connect :controller => "contacts" # ...current on any action from this controller
        contact_list.connect :controller => "people", :except => "new"
        contact_list.connect :controller => "companies", :except => "new"


      # Another submenu that points to /person/new :new_person, :url => { :controller => "people", :action => "new" }

      # Another submenu that points to /company/new :new_company, :url => { :controller => "companies", :action => "new" }


    # Another root menu with nested submenus :admin, :url => { :controller => "users", :action => "index" } do |admin| :users, :url => { :controller => "users", :action => "index" } do |users| :reports, :url => { :controller => "user_reports", :action => "index" } do |reports|
 :activity, :url => { :controller => "user_reports", :action => "activity" }
 :login_atempts, :url => { :controller => "user_reports", :action => "login_atempts" }
        end :new_user, :url => { :controller => "users", :action => "new" }


Finally, to render you newly created menu called :default, in your default layout (layout/application.erb):

<%= simple_navigation :default %>

Ready for Internationalization (i18n)

If you want to use internationalization in your menus, set the option :i18n => true like this:

SimpleNavigation::Builder.config do |map|
  map.navigation :default, :i18n => true do |navigation|

And add to your config/locales files (e.g. es-MX.yml) the following:

    default:                          # The name of your navigation menu
      home:                           # The name of your root menu
        title: "Inicio"               # The translated title for your root menu
            title: "Panel de Control" # The title for index action child menu
            title: "Nueva Página"     # The title for new action child menu

Sample Application

I have a sample application at GitHub so you can see it work. This application has the following configuration (config/initializers/simple_navigation.rb):

SimpleNavigation::Builder.config do |map|
  map.navigation :default, :i18n => true do |navigation| :home, "Wellcome", :url => { :controller => "home", :action => "index" } do |home| :settings, "Appliction Settings", :url => { :controller => "home", :action => "settings"}
    end :pages, :url => { :controller => "pages", :action => "index" } do |pages| :page_one, "One", :url => { :controller => "pages", :action => "one" } :page_two, "Two", :url => { :controller => "pages", :action => "two" }

When you render the navigation menu, it creates an unordered list like this:

<ul id="simple_navigation_default" class="simple_navigation" depth="0">
  <li id="simple_navigation_default_menus_home" class="menu" drop_down="true">
    <a href="/home">Wellcome</a>
    <ul id="simple_navigation_default_menus_home_menus" depth="1" style="display: none;">
      <li id="simple_navigation_default_menus_home_menus_settings" class="menu" drop_down="false">
        <a href="/home/settings">Appliction Settings</a>
  <li id="simple_navigation_default_menus_pages" class="menu current_child" drop_down="true">
    <a href="/pages">Pages</a>
    <ul id="simple_navigation_default_menus_pages_menus" depth="1" style="display: none;">
      <li id="simple_navigation_default_menus_pages_menus_page_one" class="menu" drop_down="false">
        <a href="/pages/one">One</a>
      <li id="simple_navigation_default_menus_pages_menus_page_two" class="menu current" drop_down="false">
        <a href="/pages/two">Two</a>

This result, with a little help of our friends CSS+Javascript will result in something like this:

Screenshot-Simple Navigation - Sample Application - Mozilla Firefox
Screenshot-Simple Navigation - Sample Application - Mozilla Firefox

Check the Simple Navigation Example yourself… Hope you enjoy it!

Gem: Simple Navigation (Navigation menu builder for Ruby on Rails)

Ruby: Convert Number to Words (Numerical)

Recently I’ve published my new gem NumberToWords. This plugin/gem will override Ruby’s Numeric class adding a new method called to_words. For now, it only works for Spanish.

Sample usage:

require 'rubygems'
require 'number_to_words'
=> cinco mil seiscientos setenta y ocho”

Another common usage is for describing currency quantities:

number = 4567.90
=> 4567.9
number.to_words.capitalize << ' pesos ' << (number.to_s.split('.')[1] || 0).rjust(2,'0')
=> "Cuatro mil quinientos sesenta y siete pesos 09/100 M.N."

Happy Hacking!

Ruby: Convert Number to Words (Numerical)

Git: Adding a Ruby on Rails Project to Git

Here’s how to add a recently created Ruby on Rails project to git:

1. Create your new project:

$ rails -d mysql project_name

2. Create some .gitignore empty files so you save the entire structure of your project (git doesn’t include empty folders):

$ touch db/.gitignore lib/.gitignore log/.gitignore tmp/.gitignore vendor/.gitignore

3. Create a new .gitignore file in the root directory of your project with the following content:


Note: If you are using sqlite add db/*.sqlite3 to .gitignore so you do not have the database into the repository (you can db:migrate it whenever you need it). Thanks for your reply Piku.

4. Initialize local repository:

$ git init

5. Add the project files and make que initial import:

$ git add .
$ git commit -a -m "* Initial import"

6. Finally if you have a gitosis server you can add it with the following commands:

$ git remote add origin git@YOUR_SERVER_NAME:project_name.git
$ git push origin master

That’s it, your project sources are now managed by git.

Happy hacking!

Git: Adding a Ruby on Rails Project to Git