ikhono.net

With a little help from my friends 0

Posted on Friday, November 30

In this article I’d like to present you some of my favourite Rails helper methods. Just add them to your app/helpers/application.rb file to use in your own application.

Booleans

This is maybe one of the most basic helpers you’ll ever encounter. But it also helps you DRY up your code, e.g. if your application is multilingual.

1
2
3
def yes_no(bool)
  bool ? "yes" : "no"
end

Numbers

The following helper prints numbers with correct delimiters and a fitting form of title.

1
2
3
def print_number(title, number)
  number_with_delimiter(number) + " " + title.pluralize(number)
end

Page title

The title helper helps to DRY up page titles:

1
2
3
4
5
def title(page_title)
  content_for(:title) {
    page_title
  }
end

I found this helper on Railscasts.

In app/views/layouts/application.rhtml I use the following two snippets:


<title>MySite :: <%= yield(:title) || "My uber cool site!" %></title>
1
2
3
<% if yield(:title) %>
  <h1><%= yield(:title) %></h1>
<% end %>

To set the title, I call the title helper in my views like this:


<% title "User profile of #{@user.login}" %>

Forms

The tabindex attribute of form elements specifies the position in the tabbing order. The tabbing order defines the order in which elements will receive focus when navigated by the user via the keyboard.

Instead of specifying the tabindex yourself, you can use the helper method

1
2
3
4
def autotab
  @current_tab ||= 0
  @current_tab += 1
end

in your views like this:


<%= f.text_field :first_name, :tabindex => autotab %>

Ruby on Rails 2.0 added the very handy label form helper. But unfortunately there is no label_tag helper. So I added one myself:

1
2
3
4
5
6
7
# Creates a label tag.
#   label_tag('post_title', 'Title')
#     <label for="post_title">Title</label>
def label_tag(name, text=nil, options = {})
  content = (text.blank? ? nil : text.to_s) || name.to_s.humanize
  content_tag('label', content, { 'for' => name }.merge(options.stringify_keys))
end

I often use a submit button together with a link to cancel. Overriding the submit_tag helper provided by Rails, helps me to DRY up my views:

1
2
3
4
5
def submit_tag(value = "Save changes", options = {})
  or_opt = options.delete(:or)
  return super + " <span class='button_or'>" + "or" + " " + or_opt + "</span>" if or_opt
  super
end

Now I can use the following code in my views:


<%= f.submit 'Update', :or => link_to('Cancel', user_path(@user)), :tabindex => autotab %>

Markaby

Markaby lets you generate HTML for views by writing ordinary Ruby code. I particulary like to use Markaby in my helpers. To simplify the use of Markaby, I’m using the following helper method:

1
2
3
def markaby(&block)
  Markaby::Rails::Builder.new({}, self, &block) 
end

The markaby helper can be used in other helpers like this:

1
2
3
4
5
6
7
8
def logo_text
  markaby do
    span.logo do
      span.blue "My"
      span.green "App"
    end
  end
end

The above helper generates the following HTML code:


<span class="logo"><span class="blue">My</span><span class="green">App</span></span>

Introducing Chicle 0

Posted on Thursday, November 29

Chicle is the natural gum from Manilkara chicle, which is a tropical evergreen tree native to southern North America and South America. It was traditionally used in chewing gum.

(Source: Wikipedia)

And Chicle is also a simple framework created by me for writing and publishing LaTeX documents.

Chicle needs some external programs to work - most of them are configurable:

  • GNU make
  • LaTeX, pdfTeX and BibTeX
  • latexmk: Version 3.07a is provided in the bin/ directory.
  • Viewers for PDF, DVI and/or PS

Go to the project page for more information. The project page also offers archives of the initial release.

Object-Oriented Software Construction 0

Posted on Wednesday, November 28

All you need is code
Code is all you need

  – The bOOtles

(Source: Slides of a talk about the “Eiffel Method” by Betrand Meyer)

Stairway to Heaven backwards 0

Posted on Sunday, November 25

Jeff Miller posted an interesting flash piece on his website.

Stairway to heaven

It has excerpts from different songs like “Stairway to Heaven”, “Another One Bites the Dust” and “Imagine” that you can play forward and reverse.

If you listen carefully, you can hear the hidden messages (or not :)).

New hoster 0

Posted on Saturday, November 24

I’ve had some big problems with hosting Rails applications at my previous hoster, so I decided to switch to the official Ruby on Rails host Joyent.

I signed up for a shared hosting account and I’m really happy with it. I can deploy Rails applications with Mongrel and the support is excellent.