Advantages of choosing Ruby on Rails for your Startup !

Posted on by tkwong

This was a post that was long pending that we wanted to write but somehow assumed that all would know fair and square. Anyway we are putting it in words here for the benefit of the entrepreneurs and the startup community . Ruby on Rails has come a long way from its inception and with Rails 3 we can clearly define the value differentiator of Ruby on Rails in comparison with conventional technologies.

1. With Ruby on Rails a Quick transformation of concepts to code is possible.
2. Rapid framework building capable. Building the basic product can be very fast on ROR
3. Incremental integration is possible as there is a structured framework
4. Testing of code of errors is integrated as part of the process.
5. Possible to reduce your lead time from configuration setting to application deployment by more than 40%.
6. Build on ROR is lean and mean very compatible for mobile devices of the future.
7. Your product is future ready for emerging trends to build mobile solutions and cloud models.
8. Capitalise on the benefits of Opensource with strong online community presence that constantly refines code.
9. ROR is best suited for BDD and Agile environment where value creation is the top priority for a project

Ray Lee liked this post

Paperclip: migrating from Amazon S3 to file system storage

Posted on by tkwong

Long has a problem to migration filesystem attachment to S3 and several articles already explain.

For today I just head on the problem that in reverse..  Here is my code snippet :

    require 'aws/s3'
    require 'fileutils'

    # Assume the original s3 paperclip option is {:storage => :s3, :s3_protocol => 'https',:s3_headers => {'Expires' => 1.year.from_now.httpdate},:s3_credentials => "#{Rails.root.to_s}/config/amazon_s3.yml", :path => '/:class/:attachment/:id/:style/:filename'}
    # New paperclip options is {:storage => :filesystem, :url => '/system/:class/:attachment/:id/:style/:filename'}

    # Load credentials
    s3_options = YAML.load_file(File.join(Rails.root, 'config/amazon_s3.yml'))["production"].symbolize_keys
    bucket = s3_options.delete(:bucket)

    # Establish S3 connection
    AWS::S3::Base.establish_connection!(s3_options)

    { 'Product' => :image, 'Product' => :icon }.each do |klass_name,param|
      klass = klass_name.constantize
      # Process each attachment
      styles = klass.first.send(param).styles.keys.push(:original)

      klass.all.each_with_index do |obj, n|
        styles.each do |style|
          filesystem_path = obj.send(param).path(style)

          next if filesystem_path.blank? 

          s3_path = filesystem_path.gsub(/^.*\/system/,'')

          # File System Path => '/system/:class/:attachment/:id/:style/:filename'
          # S3 path => '/:class/:attachment/:id/:style/:filename'

          begin

            # Create Directory
            FileUtils.mkdir_p  File.dirname(filesystem_path)

            # Write File (from S3 path to filesystem path)
            File.open(filesystem_path, "wb") do |f|
              f.write(AWS::S3::S3Object.value(s3_path, bucket))
            end
          rescue Exception => e
            puts e
          end
        end
        puts "#{klass_name} Saved to #{filesystem_path} (#{n}/#{klass.count})"
      end
    end

also available in https://gist.github.com/1069064 .

Ray Lee liked this post

Rails 3.0.9 has released

Posted on by tkwong

Hi everybody!

Rails 3.0.9 has been released! I think i will have a try and post the latest updates after my test run. The main fixed in this release are problems dealing with modifications of SafeBuffers.gem install rails or update your Gemfile and bundle update while it’s hot!

CHANGES

The major changes in this release of Rails are bug fixes surrounding modifications to SafeBuffer strings. We had places that were modifying SafeBuffers and those places raised exceptions after the security fixes in the 3.0.8 release.

We’ve since updated those code paths, and now we have this nice release for you today!

Please check the CHANGELOG files in each section on github for more details.

For an exhaustive list of the commits in this release, please see github. Please enjoy this release of Rails!

Ray Lee liked this post

Rails 3.1 include CoffeeScript, jQuery, Sass as default

Posted on by tkwong

This gallery contains 3 photos.

Yes, the coming Rails 3.1 is going to be bring new library as default: CoffeeScript, jQuery, and Sass.

In true Ruby and Rails style, there’s been a little controversy over today’s CoffeeScript and Sass news. While the jQuery migration was pretty much accepted due to Prototype’s position six feet under, plenty of developers are familiar with JavaScript and either feel slighted by CoffeeScript’s inclusion as a default or are concerned that it’ll act as a barrier for newcomers to Rails in future.

Given that you can “opt out” of having CoffeeScript and Sass included in your Rails projects by making a minor change to your Gemfile, this faux-controversy is nowhere near as interesting to me as the legitimate RSpec vs Test::Unit debate that DHH kicked off.

However, if you want to grab some popcorn and enjoy the bullshit, the comments on this GitHub commit page are epic. People have even gone so far to produce graphics to express their opinions. Rock and roll. There’s also a significant level of pie-slinging going on on Twitter too. lets quickly go through the details now.

jQuery – Trivial

A lot of Rails applications lean on JavaScript to provide client-side and AJAX functionality. Till now, the Prototype library had been included in Rails by default as a way to make various things easier to achieve across different browsers, but jQuery has become significantly more popular over the last few years.

Given this, DHH’s announcement passed with little controversy since Rails developers had become used to using jQuery and installing the jquery-rails gem anyway. A good move and a progression with the times – awesome.

CoffeeScript – JavaScript, Improved

CoffeeScipt, which may be new to all. From CoffeeScript : CoffeeScript is a little language that compiles into JavaScript. If started life as a Ruby project that converted a cleaner, JavaScript-esque language into JavaScript. CoffeeScript’s syntax enables you to write JavaScript in a cleaner and, often, more logical way.

Sass – CSS (yes) – HAML – HTML (no)

Sass is to CSS as CoffeeScript is to JavaScript, though to a less extreme level. In its modern form, Sass looks just like CSS but adds support for things like variables, functions, nesting, and similar useful features. Crucially, Sass is just a superset of CSS so you can still use regular CSS with Sass and it’ll Just Work™. You can then learn new features one by one and start using them as you like (I must admit, I’m a massive fan of Sass and I mostly stick to the variables and nesting features).

Reference:

How to Play with Rails 3.1, CoffeeScript and All That Jazz Right Now

Rails SEO pack: 5 plugins to optimize your site

Posted on by tkwong

Here I am going to share some insights on getting your Ruby on Rails site to perform better in search engines, popularly called SEO or Search Engine Optimization.

We are going to use these five Rails plugins:

  1. Headliner for titles
  2. Metamagic for meta tags
  3. Gretel for breadcrumbs
  4. FriendlyId for friendly URLs
  5. Dynamic Sitemaps for sitemaps

1. Titles using Headliner

Headliner is a Ruby on Rails plugin for creating titles in a <title> tag. The plugin makes it easy to define your titles without having to define it in several places.

Titles are important in your SEO pack because they tell search engine users what content is in your individual pages.

In your application.html.erb:

<head>
<%= title :site => "My Awesome Site",
:separator => "—",
:reverse => true %>
...
</head>

In your view:

<h1><%= title "Contact info" %></h1>

Would generate the following:

<head>
<title>Contact info — My Awesome Site</title>
...
</head>

2. Meta tags using Metamagic

Metamagic is a Ruby on Rails plugin for generating meta tags.

Despite what you may have heard, meta tags are still important in your SEO pack, especially the description meta tag which Google uses in addition to the page content to both find and display your site.

In your application.html.erb:

<head>
...
<%= metamagic :title => @title %>
...
</head>

(the :title => @title part is to automatically retrieve the title from the above mentioned Headliner plugin.)

In your view:

<% meta :description => "This is my page description.",
:keywords => "one, two, three" %>

Would generate the following:

<head>
...
<meta name="title" content="Page title set using Headliner" />
<meta name="description" content="This is my page description." />
<meta name="keywords" content="one, two, three" />
...
</head>

3. Breadcrumbs using Gretel

Gretel is a Ruby on Rails plugin for generating breadcrumbs.

Breadcrumbs are important in your SEO pack as they tell both users and search engines the location or “path” to your pages. Google will also often display the breadcrumb instead of the page URL.

In your application.html.erb:

<body>
...
<div id="breadcrumb">
<%= breadcrumb :pretext => "You are here:",
:separator => "›",
:autoroot => true,
:show_root_alone => false,
:link_last => false %>
</div>
...
</body>
In config/initializers/breadcrumbs.rb:

Gretel::Crumbs.layout do
crumb :root do
link "Home", root_path
end
crumb :articles do
link "Articles", articles_path
end
crumb :article do |article|
link article.title, article_path(article)
parent :articles
end
end

In your controller:

def show
@article = Article.find(params[:id])
end

In your view:

<% breacrumb :article, @article %>

Would generate a breadcrumb like this:

<div id="breadcrumb">
You are here: <a href="/">Home</a> › <a href="/articles">Articles</a> › My Article
</div>

4. Friendly URLs using FriendlyId

FriendlyId is a Ruby on Rails plugin for generating friendly URLs.

Friendly URLs are important in your SEO pack as they tell users and search engines what lies beneath the URL, e.g. /articles/34 becomes/articles/my-awesome-article.

In your model:

class Article < ActiveRecord::Base
has_friendly_id :title,
:use_slug => true,
:approximate_ascii => true
end

In your controller:

def create
@article = Article.create(:title => "My awesome article")
redirect_to article_path(@article) # => /articles/my-awesome-article
end

5. Sitemaps using Dynamic Sitemaps

Dynamic Sitemaps is a Ruby on Rails plugin for generating sitemaps in thesitemaps.org XML format specification.

Sitemaps are important in your SEO pack because they enable search engine crawlers to find all of your pages.

In config/initializers/sitemap.rb:

Sitemap::Map.draw do
url root_url, :last_mod => DateTime.now, :change_freq => 'daily', :priority => 1
url about_url, :change_freq => 'monthly', :priority => 0.5
url contact_url, :change_freq => 'monthly', :priority => 0.5
url terms_url, :change_freq => 'monthly', :priority => 0.5
autogenerate :articles, :last_mod => :updated_at, :change_freq => 'weekly', :priority => 0.8
end

Would generate a sitemap like this:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://yourdomain.com/</loc>
<lastmod>2011-03-09</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url>
...
</urlset>

Conclusion

I hope this short guide will be of great use when you’re optimizing your page for search engines. If you have further tips or suggestions, please write a comment below.

 

Time Zones Problem

Posted on by tkwong

Time functions in Rails at some points rather shaky. So you would expect Time.now and 0.seconds.ago equal. That is not so:

Time.now => Thu May 28 23:28:37 +0200 2009 >> 0.seconds.ago => Thu, 28 May 2009 23:28:42 CEST +02:00

Closer inspection shows that Time.now a normal Ruby Time instance, while 0.seconds.ago an ActiveSupport::TimeWithZone object.

And that is quite a difference! In this code example:

>> Time.now.to_s(:db) => "2009-05-28 23:33:08" >> 0.seconds.ago.to_s(:db) => "2009-05-28 21:33:16"

The to_s (: db) method is used to find queries and named scopes, and the above difference is that the use of Time.now , Time.parse and Time.atinadvisable when the database as a parameter can be used. Rails convertsTime instances will in TimeWithZone items from ActiveRecord attributes.Alternatively Time.zone.now etc. used.
In short: u.published_at = Time.now works fine, but when you are in your environment.rb you are a local time zone, this is wrong in Rails 2.3.2:

named_scope :begun, proc{{:conditions => ['started_at < ?', Time.now]}}

Beware!