Bruno Miranda's Notebook

Personal Blog about Ruby on Rails, XHTML, CSS, and Design

RubyOnda is a ruby news website in portuguese built by myself and Roberto.

Today Felipe Mesquita built a nice little widget for those interested in showcasing the latest community news posted to RubyOnda on their own site.

The code to insert the widget is:

< script src="http://rubyonda.com/widget-embedded.js" type="text/javascript">

Here is what it looks like:

Some of you may have participated or heard about last year’s Rails Day 2006. The contest allowed developers to design and implement a web application in a 24 hour period. In exchange for prizes and a bit of community recognition.

Although the contest was partially successful, and many interesting apps came out of it, this year’s Rails Rumble, September 7-8, 2007, organizers decided to do it differently.

rails rumble sticker

Rails Rumble will allow a 48 time period for app development and deployment. The teams will be made up of 1-4 developers and there is currently a max of 50 teams. Applications will be judged by their completeness, user interface, originality, usefulness and overall.

The event includes sponsorships by Pragmatic Bookshelf, Cashboard, Active State so good prizes are expected.

If you are a rails developer, and would like to participate run over to Rails Rumble and sign up.

Comments: 0 (view/add your own) Tags: Rails, ruby

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.

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.

Comments: 0 (view/add your own) Tags: ruby

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

Comments: 0 (view/add your own) Tags: date, ruby, time

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!

I’ve been looking for a way to limit the number of options under minute on timeselect and datetimeselect. You can set the :minute_step to 15 which will give you: 00, 15, 30 and 45.

<%= datetime_select 'game', 'game_date_time', {:minute_step => 15} %>

Hope that helps!

If you need to display a a float/string formatted as money (with two decimal points), simply add a helper method to your application_helper.rb.

module ApplicationHelper
  def two_dec(n)
    sprintf("%.2f", n)
  end
end

Now every time you need to display you value, just pass it into the two_dec method.

  $<%= two_dec(@repair.cost) %>

Will produce: $150.00.

I couldn’t count how many times per day I have to reference the Rails API. RailsBrain makes it easy to quickly find what you are looking for.

Comments: 0 (view/add your own) Tags: API, Rails, ruby

If you’ve ever wondered how you can access an XMLRPC resource using ruby, the code below displays how to do it.

The snippet returns a hash containing information corresponding to the UPC code we performed the lookup on.

require 'xmlrpc/client'
server = XMLRPC::Client.new2('http://www.upcdatabase.com/rpc')
@resp = server.call('lookupUPC', '099482409463')
puts @resp['description']
puts @resp['upc']

The return value from the above will be:

  • Whole Foods Market 365 Italian Still Mineral Water
  • 099482409463
Comments: 0 (view/add your own) Tags: XMLRPC, ruby