modules

14 views
Skip to first unread message

John Merlino

unread,
Sep 5, 2012, 9:38:25 PM9/5/12
to Ruby on Rails: Talk
module ActionView
module Helpers
module FormHelper
def form_for(foo, bar)
instantiate_builder(foo, bar)
end
def instantiate_builder(foo,bar)
self
end
end
end
end

"self" in instantiate_builder refers to
ActionView::Helpers::FormHelper. self it will have access to methods
in ActionView::Helpers (its parent module), correct?

Frederick Cheung

unread,
Sep 6, 2012, 5:32:49 AM9/6/12
to rubyonra...@googlegroups.com
Not necessarily. 

if you do 
class Foo
  include ActionView::Helpers::FormHelper
end

then 
f = Foo.new
f.instantiate_builder

then self in instantiate_builder is f. Whether or not other modules in ActionView::Helpers have been included in Foo is not guaranteed (in the particular case of view helpers in views then all the other submodules of ActionView::Helpers would normally have been included)

Fred

John Merlino

unread,
Sep 6, 2012, 11:45:32 AM9/6/12
to Ruby on Rails: Talk
thanks for response

On Sep 6, 5:32 am, Frederick Cheung <frederick.che...@gmail.com>
wrote:

John Merlino

unread,
Sep 6, 2012, 12:33:33 PM9/6/12
to Ruby on Rails: Talk
There's another situation like this:

  ActiveSupport.on_load(:action_view) do
    cattr_accessor(:default_form_builder)
{ ::ActionView::Helpers::FormBuilder }
  end

This is ActionView::Helpers::FormHelper. But I believe that self in
the context of the block refers to ActionView, because on_load
executes the block only after ActionView is loaded and then yields
ActionView to the block so that default_form_builder will become a
class method on ActionView, which returns the FormBuilder class
object.
Reply all
Reply to author
Forward
0 new messages