BrunoMiranda.com

Personal Blog about Technology, Software Engineering, Design & More

My good friend Chris Saylor gave a presentation on jump-starting Ruby on Rails.

Here is the video

Here are the PDF slides.

Our next Miami Ruby Meetup will be on July 16, location is still not confirmed but it will be near the Datran building.

I will be giving a presentation of Capistrano 2.

Be the best at something, not OK at a bunch of stuff. Pick one market and dedicate 80% of your working time on it.

Very basic tip, hard to remember and put in practice sometimes.

Create a Partial from selection:

ctrl + shift + h

When mouse over an action in the controller, go to target view:

command + option + down arrow

For those coming from C/C++ use to doing multi-line comments with /* */, you may find yourself looking for a way to do the same in ruby. Single-line comments are easily done with # comment. Here how to do it muli-line:

=begin
  This is a multi-line
  comment in ruby
=end def skipped_method end

def interpreted_method

end

Note =end is not a token delimiter, everything on the same line as =end will be skipped by the interpreter as well.

Very useful plugin that formats timestamps to human-friendly relative dates.

<%= relative_time(Time.now) %>
# today
<%= relative_time(1.day.ago) %>
# yesterday
<%= relative_time(1.day.from_now) %>
# tomorrow
<%= relative_time_span([Time.now, 5.days.from_now]) %>
# May 17th - 22nd

To install:

script/plugin install http://ar-code.svn.engineyard.com/plugins/relativetimehelpers

While searching for a couple of rake script shortcuts today I came accross a great Rail cheatsheet at the RubyOnRailsBlog.com

Rails Cheatsheet

The cheatsheet is very complete and insanely useful. Go check it out for yourself.

As most of us already know, if you use the built in time_select helper to interface with a time column in your DB, your HTML select box will display military format. Most of us in the USA are not accustomed to telling time in this format.

After trying a couple of the rails plug-ins out there only to find out none of them worked, I consulted my buddy Rich who wrote a brilliantly simple date helper module to get the job done.

The code follows below:

module ActionView
  module Helpers
    module DateHelper
      def select_hour_with_twelve_hour_time(datetime, options = {})        
        return select_hour_without_twelve_hour_time(datetime, options) 
        unless options[:twelve_hour].eql? true

        val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : ''
        if options[:use_hidden]
          hidden_html(options[:field_name] || 'hour', val, options)
        else
          hour_options = []
          0.upto(23) do |hour|
            ampm = hour <= 11 ? ' AM' : ' PM'
            ampm_hour = hour == 12 ? 12 : (hour / 12 == 1 ? hour % 12 : hour)

            hour_options << ((val == hour) ?
              %(\n) :
              %(\n)
            )
          end
          select_html(options[:field_name] || 'hour', hour_options, options)
        end
      end
      alias_method_chain :select_hour, :twelve_hour_time
    end
  end
end

Subversion Source:

http://svn.devjavu.com/bopia/twelve_hour/

Usage:

<%= time_select 'event', 'time', {:twelve_hour => true} %>

Trac:

http://bopia.devjavu.com/projects/bopia/report/1

The above will output something that looks like this:

Time: :

If you a trying to validate if the value of an attribute is unique on the database, you can easily accomplish this by using the built in helper method…

validates_uniqueness_of
But how would you go about making sure the attribute value is unique based on a scope parameter? Let’s say you are building a holidays table and need to make sure you only allow one ‘Christmas’ holiday to be created per year:
  validates_uniqueness_of :name, 
:scope => :year, :message => "must be unique"

Hope that helps!

Visit the Archives →