How to localize Rails routes

CSS posted about 1 year ago by christian

I forget this all the time:

   1  map.resources :products, :as => 'productos', :path_names => { :new => 'nuevo', :edit => 'editar' }

Documented here….

Tagged resources, routes, localize, rails

Time ago in words (minutes, hours, days, weeks, months ago in words)

Ruby posted over 2 years ago by christian

   1  def minutes_in_words(timestamp)
   2      minutes = (((Time.now - timestamp).abs)/60).round
   3      
   4      return nil if minutes < 0
   5      
   6      case minutes
   7        when 0..4            then '&lt; 5 minutes'
   8        when 5..14           then '&lt; 15 minutes'
   9        when 15..29          then '&lt; 30 minutes'
  10        when 30..59          then '&gt; 30 minutes'
  11        when 60..119         then '&gt; 1 hour'
  12        when 120..239        then '&gt; 2 hours'
  13        when 240..479        then '&gt; 4 hours'
  14        when 480..719        then '&gt; 8 hours'
  15        when 720..1439       then '&gt; 12 hours'
  16        when 1440..11519     then '&gt; ' << pluralize((minutes/1440).floor, 'day')
  17        when 11520..43199    then '&gt; ' << pluralize((minutes/11520).floor, 'week')
  18        when 43200..525599   then '&gt; ' << pluralize((minutes/43200).floor, 'month')  
  19        else                      '&gt; ' << pluralize((minutes/525600).floor, 'år')
  20      end
  21    end

There are also similar implementations:
  1. http://www.actsasflinn.com/articles/2007/04/10/time-ago-method-for-ruby-on-rails
  2. http://timeago.yarp.com/
Tagged month, week, hour, minutes, words, rails, ruby

Rails+Mongrel+Apache 2 on Mac OSX Leopard

Apache posted over 2 years ago by christian

I use this configuration on my development machine when I need mod_rewrite; it’s not meant for production:

   1  <VirtualHost *:80>
   2  ServerName dev.xxx.com
   3  
   4  # Enable URL rewriting
   5  RewriteEngine On
   6  
   7  # Rewrite index to check for static pages
   8  RewriteRule ^/$ /index.html [QSA]
   9  
  10  # Rewrite to check for Rails cached page
  11  RewriteRule ^([^.]+)$ $1.html [QSA]
  12  
  13  # Redirect all non-static requests to cluster
  14  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  15  RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
  16  
  17  DocumentRoot  "/Users/christian/Documents/Projects/xxx/public"
  18  <Directory "/Users/christian/Documents/Projects/xxx/public">
  19       Options Indexes FollowSymLinks
  20  
  21       AllowOverride None
  22       Order allow,deny
  23       Allow from all
  24   </Directory>
  25  
  26  </VirtualHost>
  27  
  28  <Proxy balancer://mongrel_cluster>
  29    BalancerMember http://127.0.0.1:3000
  30  </Proxy>

Tagged apache, mongrel, osx, rails

How to use ActiveRecord without Rails

Ruby posted over 2 years ago by christian

This is an example of how to use ActiveRecord without Rails:

   1  ['/model', '/db'].each do |folder|
   2    $:.unshift File.dirname(__FILE__) + folder
   3  end
   4  
   5  require 'test/unit'
   6  require 'rubygems'
   7  require 'activerecord'
   8  
   9  ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
  10  ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/config/database.yml'))
  11  ActiveRecord::Base.establish_connection('sqlite3')
  12  
  13  require 'schema'
  14  

Schema contains, for example:

   1  ActiveRecord::Schema.define :version => 0 do
   2    create_table :languages, :force => true do |t|
   3      t.string :name
   4    end
   5  end

Tagged activerecord, standalone, rails, ruby

How to use named_scope in Rails

Ruby posted over 2 years ago by christian

Simple example of how to use the named_scope feature:

   1  class Feed < ActiveRecord::Base
   2   
   3    named_scope :active, :conditions => "(active = 1)"
   4    named_scope :stale,  :conditions => ["last_updated > ?", 30.minutes.ago.to_s(:db)]

Usage:

   1  Feed.active # return the active feeds

Chaining is also possible:

   1  Feed.active.stale # return the feeds that need to be updated

Tagged named_scope, rails, activerecord, ruby