Flowdock

Recent good notes

RSS feed
July 13, 2008
5 thanks

Convert strings to Dates

Uses the undocumented Date._parse method. Some usage examples:

'06/15/2008'.to_date         # => Sun, 15 Jun 2008
'20080615'.to_date           # => Sun, 15 Jun 2008
'2008-06-15'.to_date         # => Sun, 15 Jun 2008
'Sun, 15 Jun 2008'.to_date   # => Sun, 15 Jun 2008
July 8, 2008
4 thanks
July 8, 2008
3 thanks

For more information

See ActiveMailer::Base.

July 7, 2008
4 thanks

List of statuses

You can view a full list of statuses at http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_controller/status_codes.rb.

head can be called with a symbol or a status code:

Using head with a symbol

head :length_required # 411 Length Required
head :ok              # 200 OK

Using head with a status code

head 404              # 404 Not Found
July 5, 2008
23 thanks

Using counters with collections

When you’re rendering a collection partial, the partial_name_counter variable contains the position of the current element in the collection. For example:

<%= render(:partial => 'example', :collection => %w(rails-doc is cool)) %>

Now in _example.html.erb:

<p>Element: <%= example %> (index: <%= example_counter %>)</p>

It would produce:

<p>Element: rails-doc (index: 1)</p>
<p>Element: is (index: 2)</p>
<p>Element: cool (index: 3)</p>

As you can see, indexing starts from 1.

July 4, 2008
3 thanks

Using a specific layout

To choose the layout (or no layout) for a method call the render method with a :layout option.

July 4, 2008
6 thanks

Window open a dialog of no menu, no status, have scroll

Example

link_to name, url, :popup => ['dialog name','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes']
July 4, 2008
12 thanks

Add empty option and text is -select-

select :object, :method, options, :prompt => ‘-select-’

July 4, 2008
5 thanks

Used to get current Rails environment

Rails#env returns a string representing the current Rails environment.

Code example

>> Rails.env # in development mode
Rails.env
=> "development"
July 3, 2008
5 thanks

Connections to multiple databases

establish_connection can be used to connect to multiple databases. The immediate downside is that your rake migrations may not work properly without hacking.

In each model that resides in a different database we call:

establish_connection :different_database

You can check they are working by hitting script/console with:

>> App.connection.instance_eval {@config[:database]}
=> "app_development"
>> AnotherDatabase.connection.instance_eval {@config[:database]}
=> "another_database"

Now doing a call to AnotherDatabase.find() will connect to the AnotherDatabase database and start returning results.

July 2, 2008
7 thanks

error in block code example

I guess there’s an error in this part of the code:

link_to_function("Show me more", nil, :id => "more_link") do |page|
  page[:details].visual_effect  :toggle_blind
  page[:more_link].replace_html "Show me less"
end

It doesn’t work. It should be:

link_to_function("Show me more", nil, :id => "more_link") do |page|
  page[:details].toggle "Blind"
  page[:more_link].replace_html "Show me less"
end

Using Rails 2.1.0

July 2, 2008
6 thanks

Rails 2.1 caching internals

Rails 2.1 caching features are pretty much undocumented. Rob Anderton has documented some internal stuff here:

http://www.thewebfellas.com/blog/2008/6/9/rails-2-1-now-with-better-integrated-caching

July 1, 2008
5 thanks

benchmark and silence methods

benchmark (title, log_level = Logger::DEBUG, use_silence = true) {|| …}

Log and benchmark the workings of a single block and silence whatever logging that may have happened inside it (unless use_silence is set to false).

The benchmark is only recorded if the current level of the logger matches the log_level, which makes it easy to include benchmarking statements in production software that will remain inexpensive because the benchmark will only be conducted if the log level is low enough.

silence () {|| …}

Silences the logger for the duration of the block. To silence the logger in your controller, simply use:

NameOfController.logger.silence do

# your code here...

end

July 1, 2008
3 thanks

:case_sensitive is on by default?

In contrast to what the documentation said, :case_sensitive seems to be on by default. This is the case with MySQL at least, I’m not sure about other databases.

June 30, 2008
11 thanks

Turn layout off

If you don’t want a layout rendered, just put:

layout nil

in your controller.

June 29, 2008
5 thanks
June 29, 2008
9 thanks

Use the :as option for SEO friendly URLs

The :as option to map.resources can be used to generate SEO friendly URLs like so:

Code Example

map.resources :operating_systems, :as => 'operating-systems'

# operating_systems_path => /operating-systems
June 29, 2008
6 thanks

Turns hash keys into symbols

This method is part of ActiveSupport, which is a collection of classes and standard library extensions that are useful for Rails.

symbolize_keys! returns a new hash with all keys converted to symbols.

This can be helpful when you have a non-ActiveRecord model and you want your initialize method to take a hash of parameters. If you symbolize the hash, you can extract your parameters regardless of whether initialize received a hash of strings or hash of symbols.

June 27, 2008
15 thanks

Passing find() arguments

I you need to pass additional arguments to a scope (e.g. limit), do this:

Shirt.colored('red').all(:limit => 10)
June 26, 2008
8 thanks

Rails defined Mime Types

Here are all the default Rails Mime Types:

"*/*"                      => :all
"text/plain"               => :text
"text/html"                => :html 
"application/xhtml+xml"    => :html
"text/javascript"          => :js 
"application/javascript"   => :js 
"application/x-javascript" => :js 
"text/calendar"            => :ics   
"text/csv"                 => :csv   
"application/xml"          => :xml 
"text/xml"                 => :xml 
"application/x-xml"        => :xml 
"text/yaml"                => :yaml 
"application/x-yaml"       => :yaml 
"application/rss+xml"      => :rss   
"application/atom+xml"     => :atom  
"application/json"         => :json 
"text/x-json"              => :json
June 26, 2008
9 thanks

Default Mime Types

This module sets up all the default mime-types. Here they are:

"*/*"                      => :all
"text/plain"               => :text
"text/html"                => :html 
"application/xhtml+xml"    => :html
"text/javascript"          => :js 
"application/javascript"   => :js 
"application/x-javascript" => :js 
"text/calendar"            => :ics   
"text/csv"                 => :csv   
"application/xml"          => :xml 
"text/xml"                 => :xml 
"application/x-xml"        => :xml 
"text/yaml"                => :yaml 
"application/x-yaml"       => :yaml 
"application/rss+xml"      => :rss   
"application/atom+xml"     => :atom  
"application/json"         => :json 
"text/x-json"              => :json
June 26, 2008
9 thanks

x-sendfile

Rails 2.1 supports the x_sendfile apache module:

send_file '/path/to.png', :x_sendfile => true, :type => 'image/png'
June 26, 2008
8 thanks

Handy for adding theme support

I’m using this to add basic theme support to my app:

append_view_path(File.join(RAILS_ROOT, "app/themes/#{@current_theme}"))
append_view_path(File.join(RAILS_ROOT, 'app/themes/_base'))

Common templates go in app/themes/_base and then you can override these with specific theme versions in each theme directory.

June 25, 2008
13 thanks

How to test different responses in controller tests/specs

When you want to write a controller test or spec (rspec) to test out a different response type other than html, just set the HTTP_ACCEPTS header like so before the request:

@request.env['HTTP_ACCEPT'] = "application/rss"
post :create, :blog => {}
June 25, 2008
4 thanks

Custom MIME Type

After you register a custom Mime::Type like stated above, you can do:

respond_to do |format|
  # .jpg corresponds to the second argument passed to #register
  # Mime::Type.register "image/jpg", :jpg
  format.jpg { ...do something here... }
end
June 25, 2008
5 thanks

:expires_in not in Rails 2.1

AFAIK this is from a patch to Rails 2.1, which hasn’t been accepted yet.

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/416-caches_actions-accepts-cache_store-options-such-as-expires_in

Also, I think it needs to be supported by the cache_store you’re using.

June 25, 2008
4 thanks

collection.exists?(conditions)

The created association method also supports the ‘exists?’ method, similar to ActiveRecord::Base#exists?

has_and_belongs_to_many :categories
...
categories.exist?(1)   # Check whether there's a relation with a Category
                       # object whose id is 1.
categories.exist?(:id => 1)    # ditto
categories.exist?(['id', 1])   # ditto
categories.exist?(:name => 'Anime')
June 22, 2008
4 thanks

update_on and update_at only set on attribute change

If you call save, and no attributes are “dirty” (changed), then an update query will not happen against the database, and thus updated_at and updated_on will not be set.

You’ll need to modify at least one field to get updated_at and updated_on to set themselves.