Blog

Mar
17

Rails 2.3 is out

Posted by Michael Cindric in development, mac osx, ruby on rails | 1 Comment »

Finally Rails 2.3 is out. Officially its tagged as 2.3.2 from memory. So what are you waiting for update already. Just run the following command in your terminal and your on your way. Sorry windows users find your own way

 sudo gem install rails

Check out the release notes here

Mar
11

Please accept the terms of service and privacy policy

Posted by Michael Cindric in ruby on rails | 3 Comments »

If you need users the accept the terms of services and privcay policy or your rails application there is a simple way to handle this without a database field or javascript. It’s using the validation methods built into rails. Simply add the following code to your User model for example

validates_acceptance_of :terms_of_service

Then in your view add the following to your view and you will get a checkbox on the page that users will be required to check.

<%= check_box :user, :terms_of_service %>

This will then add this validation rule to your models validation rules and when you try to submit the form without checking the checkbox you get a nice little validation message letting you know hey you need to accept this.

Of course you can have your own validation message displayed like so

validates_acceptance_of   :terms_of_service, :message => ' and the privacy policy must be accepted'
Mar
05

Rendering google style pie chart in your rails app

Posted by Michael Cindric in google api, ruby on rails | No Comments »

Google has some of the best charts on the web. If you ever wanted to have cool pie charts like google here is a simple way to do it. Simply add the following to your application_helper.rb file.

  def google_pie_chart(data, options = {})
    options[:width] ||= 250
    options[:height] ||= 100
    options[:colors] = %w(0DB2AC F5DD7E FC8D4D FC694D FABA32 704948 968144 C08FBC ADD97E)
    dt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-."
    options[:divisor] ||= 1

    while (data.map { |k,v| v }.max / options[:divisor] >= 4096) do
      options[:divisor] *= 10
    end

    opts = {
      :cht => "p",
      :chd => "e:#{data.map{|k,v|v=v/options[:divisor];dt[v/64..v/64]+dt[v%64..v%64]}}",
      :chl => "#{data.map { |k,v| CGI::escape(k + " (#{v})")}.join('|')}",
      :chs => "#{options[:width]}x#{options[:height]}",
      :chco => options[:colors].slice(0, data.length).join(',')
    }

    image_tag("http://chart.apis.google.com/chart?#{opts.map{|k,v|"#{k}=#{v}"}.join('&amp;')}")
  rescue
  end

Once you have that in then in your view its as simple as below to render a nice pie chart. Go ahead have a piece.


 <%=  google_pie_chart([["Rails",80],[".Net",10],["Java",10]) %>
Mar
04

Setting up gravatar’s in your rails app

Posted by Michael Cindric in plugins, ruby on rails | No Comments »

Gravatars allow users to configure an avatar to go with a users email address at a central location: http://gravatar.com. Gravatar-aware websites can then look up and display each user’s preferred avatar, without having to handle avatar management. This makes life easy on the developer and the user only has to manage one central location.

First step is to get the plugin

ruby script/plugin install git://github.com/woods/gravatar-plugin.git

Now most user models have an email field so it’s usually something like below to access their email.

  user.email

If thats the case then to have the gravatar appear in your views it’s simply

<%= gravatar_for @user %>

Thanks Scott A. Woods for a great and simple plugin.

Mar
04

Timezone support in Rails 2.1+

Posted by Michael Cindric in ruby on rails | No Comments »

Setting up Timezone support in the past was a pain, but now with rails 2.1 and above it’s easy and done in minutes. Here is what you need to do

# config/environment.rb
config.time_zone = "Pacific Time (US &amp;amp; Canada)"

# controllers/application.rb
before_filter :set_user_time_zone

private
def set_user_time_zone
  Time.zone = current_user.time_zone if logged_in?
end

# Add to field to user model
add_column :users, :time_zone, :string

Once you have this in place then to user the users timezone in your app its just something like this

# To use users timezone rather then server time
  current_user.time_zone.today