Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 44 lines (42 sloc) 1.57 kB
c77794a @carlosantoniodasilva Add missing require to with_options [#4601 state:resolved]
carlosantoniodasilva authored
1 require 'active_support/option_merger'
2
6839883 @jeremy Break up misc Object extensions
jeremy authored
3 class Object
4 # An elegant way to factor duplication out of options passed to a series of
5 # method calls. Each method called in the block, with the block variable as
6 # the receiver, will have its options merged with the default +options+ hash
7 # provided. Each method called on the block variable must take an options
8 # hash as its final argument.
9 #
448845c @vijaydev replaced eg using old router api with the one in the guides. Add info…
vijaydev authored
10 # Without <tt>with_options></tt>, this code contains duplication:
b3e5e25 @r00k Add a clearer example for with_options (lifted from the Active Suppor…
r00k authored
11 #
12 # class Account < ActiveRecord::Base
13 # has_many :customers, :dependent => :destroy
14 # has_many :products, :dependent => :destroy
15 # has_many :invoices, :dependent => :destroy
16 # has_many :expenses, :dependent => :destroy
17 # end
18 #
448845c @vijaydev replaced eg using old router api with the one in the guides. Add info…
vijaydev authored
19 # Using <tt>with_options</tt>, we can remove the duplication:
b3e5e25 @r00k Add a clearer example for with_options (lifted from the Active Suppor…
r00k authored
20 #
21 # class Account < ActiveRecord::Base
22 # with_options :dependent => :destroy do |assoc|
23 # assoc.has_many :customers
24 # assoc.has_many :products
25 # assoc.has_many :invoices
26 # assoc.has_many :expenses
27 # end
6839883 @jeremy Break up misc Object extensions
jeremy authored
28 # end
29 #
cff0aeb @r00k Add a forgotten word.
r00k authored
30 # It can also be used with an explicit receiver:
6839883 @jeremy Break up misc Object extensions
jeremy authored
31 #
448845c @vijaydev replaced eg using old router api with the one in the guides. Add info…
vijaydev authored
32 # I18n.with_options :locale => user.locale, :scope => "newsletter" do |i18n|
33 # subject i18n.t :subject
34 # body i18n.t :body, :user_name => user.name
6839883 @jeremy Break up misc Object extensions
jeremy authored
35 # end
36 #
448845c @vijaydev replaced eg using old router api with the one in the guides. Add info…
vijaydev authored
37 # <tt>with_options</tt> can also be nested since the call is forwarded to its receiver.
38 # Each nesting level will merge inherited defaults in addition to their own.
39 #
6839883 @jeremy Break up misc Object extensions
jeremy authored
40 def with_options(options)
41 yield ActiveSupport::OptionMerger.new(self, options)
42 end
43 end
Something went wrong with that request. Please try again.