public Method

Object.with_options(options) { |ActiveSupport::OptionMerger.new(self, options)| ... }

An elegant way to refactor out common options

with_options :order => 'created_at', :class_name => 'Comment' do |post|
  post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all
  post.has_many :unapproved_comments, :conditions => ['approved = ?', false]
  post.has_many :all_comments
end

Can also be used with an explicit receiver:

map.with_options :controller => "people" do |people|
  people.connect "/people",     :action => "index"
  people.connect "/people/:id", :action => "show"
end

Source Code

# File active_support/core_ext/object/misc.rb, line 47
def with_options(options)
  yield ActiveSupport::OptionMerger.new(self, options)
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.