Avoid Duplication of Hash Options
There are lots methods in Rails where the last argument is a Hash. Sometimes the same Hash is passed into more than one method, causing you to repeat yourself.
For example, when using validation helpers in your models, you may be doing something like this:
Using with_options within a Model:
validates_presence_of :version, :on => :create validates_format_of :version, , :with => /[\d]+\.[\d]+\.[\d]+/, :on => :create
Notice, :on => create is duplicated above.
Use with_options block to clean this up by doing
with_options :on => create do |release| release.validates_presence_of :version release.validates_format_of :version, :with => /[\d]+\.[\d]+\.[\d]+/ end
Using with_options within your Routes file:
You may have a list of named routes which are using the same controller, for example. Instead of specifying the same controller when defining multiple named routes, you can use map.with_options.
Assume you’d like to clean the following up within your Routes file:
map.show 'release/show/:id', :controller => 'release', :action => "show" map.edit 'release/edit/:id', :controller => 'release', :action => "edit"
Do the following instead:
map.with_options :controller => 'release' do |release| release.show 'release/show/:id', :action => "show" release.edit 'release/edit/:id', :action => "edit" end