skater coder

angela chng – Ruby on Rails / PHP web developer & skater girl in Singapore

  • blogabout stuff
  • aboutthis website
  • contactdrop a message

These are the posts from July 2010

July 25, 2010
Posted by roadburn

Comma Separated List in Rails

Here’s a useful tip I found from http://ariejan.net/2007/03/27/rails-tip-snippet-create-a-comma-seperate-list/

To get ‘Tom, Dick, Harry’

1
@users.collect{|u| u.name}.join(', ')

To get ‘Tom, Dick and Harry’

1
@users.collect(&:name).to_sentence

No Comments

Posted Under Rails

July 23, 2010
Posted by roadburn

How to: Create a Rails model that has categories and subcategories

Another reason to love rails, it’s so easy!

Just install the acts_as_tree plugin

1
$ script/plugin install git://github.com/rails/acts_as_tree.git

and in your model, eg. app/models/category.rb, just add

1
acts_as_tree :order => 'name'

And here’s the plugin’s readme on how to use it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
acts_as_tree
============
 
Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children
association. This requires that you have a foreign key column, which by default is called +parent_id+.
 
  class Category < ActiveRecord::Base
    acts_as_tree :order => "name"
  end
 
  Example:
  root
   \_ child1
        \_ subchild1
        \_ subchild2
 
  root      = Category.create("name" => "root")
  child1    = root.children.create("name" => "child1")
  subchild1 = child1.children.create("name" => "subchild1")
 
  root.parent   # => nil
  child1.parent # => root
  root.children # => [child1]
  root.children.first.children.first # => subchild1

Resources

  • Railscasts – Tree Based Navigation

No Comments

Posted Under Rails


Posted by roadburn

How to: Receive email in rails with mail fetcher

Dan Weinand and Luke Francl have written a nice plugin which simplifies the task of retrieving emails. Kudos to them!

So basically what you need to do is install the plugin, create the Action Mailer receiver, and then use cron to schedule the script to run

Here are the steps:

First install the fetcher plugin from http://github.com/look/fetcher/ and SystemTimer gem it requires

$ script/plugin install git://github.com/look/fetcher.git
$ sudo gem install SystemTimer

Create your ruby script eg. script/mail_fetcher

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/environment.rb'
 
require 'system_timer'
require 'yaml'
require 'fetcher'
 
 
begin
  puts "Start checking email"
 
  Lockfile.new('cron_mail_fetcher.lock', :retries => 0) do
    config = YAML.load_file("#{RAILS_ROOT}/config/mail.yml")
    config = config[RAILS_ENV].to_options
 
    fetcher = Fetcher.create({:receiver => EnquiryMailer}.merge(config))
    fetcher.fetch
  end
rescue Lockfile::MaxTriesLockError => e
  puts "Another fetcher is already running. Exiting."
end

Create the config/mail.yml file

defaults: &defaults
  # Settings for gmail
  type: imap                # pop OR imap
  server: imap.gmail.com  # server hostname
  port: 993                 # 993 for IMAPs
  ssl: true                 # Remove if not using SSL
  username: user@gmail.com    # Username
  password: xxx                # Password
  use_login: true           # Required for GMAIL
  processed_folder: processed
  error_folder: error
 
 # settings for bluehost
  type: imap                # pop OR imap
  server: boxXXX.bluehost.com  # server hostname
  port: 993                 # 993 for IMAPs
  ssl: true                 # Remove if not using SSL
  username: user@domain.com    # Username
  password: xxx                # Password
  use_login: true
  processed_folder: INBOX.processed
  error_folder: INBOX.error
 
development:
  <<: *defaults
  # Override other values here
 
test:
  <<: *defaults
  # Override other values here
 
production:
  <<: *defaults
  # Override other values here

Create your Action Mailer

$ script/generate mailer EnquiryMailer

Add the receiver in app/models/enquiry_mailer.rb

class UserMailer < ActionMailer::Base
  def receive(email)
    # do whatever you want with the email here
    # you can use:
    # email.subject, email.from, email.body
  end
end

Run the script

$ script/runner script/mail_fetcher

Then follow Ryan Bate’s screencast on how to use Cron in Ruby

Resources

  • http://guides.rubyonrails.org/action_mailer_basics.html
  • http://railspikes.com/2007/6/1/rails-email-processing
  • Railscasts: Cron in Ruby

No Comments

Posted Under Rails

July 22, 2010
Posted by roadburn

Tricolor rice bowl success!

Was inspired to cook today :)

Found out how to make the sweet japanese style omelette, it was surprisingly easy!

Ingredients:

  • 2 eggs
  • 2 tsp sugar
  • Dash of salt
  • Mirin
  • Fresh ginger juice
  • Olive oil

Steps:

  1. Beat eggs and mix in the ginger juice,  sugar, salt and mirin
  2. Heat oil in small frying pan, I used those really small frying pans they sell to make the perfect sunny side up egg.
  3. Pour the egg mixture into the frying pan and stir it so that it’s slightly scrambled, but don’t stir it too much or you will end up with scrambled eggs instead.
  4. Let the eggs cook till it’s slightly brown on the bottom, then carefully flip it over to cook the other side.
  5. That’s all!

3 Comments

Posted Under Food

July 21, 2010
Posted by roadburn

www.wendyphua.com

Just recently launched a WordPress website for a very talented Singaporean bassist and composer, Wendy Phua.

Check out her site at www.wendyphua.com!

No Comments

Posted Under Portfolio


Posted by roadburn

Rails fetcher mail.yml IMAP setting for bluehost

I was getting this error when trying to create a mailbox using create(mailbox) from Net::IMAP

1
/opt/local/lib/ruby/1.8/net/imap.rb:981:in `pick_up_tagged_response': Unknown namespace. (Net::IMAP::NoResponseError)

Realised that the processed and error mailboxes names had to be appended to “INBOX.”

1
2
3
4
5
6
7
8
9
type: imap            
server: boxXXX.bluehost.com  
port: 993                
ssl: true               
username: username@domain.com 
password: xxx
use_login: true        
processed_folder: INBOX.processed
error_folder: INBOX.error

No Comments

Posted Under Rails

  • CATEGORIES
    • Coding Tips
    • Food
    • Portfolio
    • Rails
    • Sharepoint
    • Wordpress

  • TAGS
    activities AD ADAM ajax architecture authentication bluehost books custom application custom form date design disk partition ECTS event id eventid moss forum guid hotfix iis imap keberos list id lists masterpage moss mysite profiles Rails rails 2.0 reporting services ruby Sharepoint sharepoint 2007 site templates site usage sql server 2005 time updates vhd virtual pc visual studio webparts workflow wss

Subscribe via RSS