Larry Kelly wrote:
I'm trying to understand how the Rails scaffold generator works well
enough that I can teach it to write Markaby files. Most of the
templates were easy, but I'm having problems with the form templates.
I have not delved into the scaffold generator code before, but I think
I can answer some of these questions.
1. The template/form.rhtml file refers to
<%= template_for_inclusion%>
I'm guessing that 'template_for' is a method and 'inclusion' is a
variable. Where does this variable come from? How can I tell where it
gets referenced/populated?
No, 'template_for_inclusion' is all one word, so it's either a local
variable or a method call.
A search for 'template_for_inclusion' suggests that it's assigned in
line 284 of rails_generator/commands.rb:
options[:assigns]['template_for_inclusion'] =
render_template_part(template_options)
That hash then gets turned into local variables in the template with
these lines (272-274) in command.rb:
vars = template_options[:assigns] || {}
b = binding
vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }
and that's where your local variable template_for_inclusion comes from.
2. How do the ' <table><tbody> ' tags get generated? I don't see
them in any of the scaffold_generator files.
I can't see any <table> or <tbody> tags in the form view. The only
table I can find is in the list view, and that template is defined in
view_list.rhtml.
3. What does 'lamda(method_name)' do? Is it saying: 'include the
output from method_name here' ?
No. I'm guessing you're looking at this line in scaffold_generator.rb:
:sandbox => lambda { create_sandbox }
Calling "lambda {some_code}" creates a new Proc object. It sort of
encapsulates that code along with the current context (i.e. the current
local variables) and lets you call it later. So in this line (line 106)
in commands.rb:
part_binding = template_options[:sandbox].call.sandbox_binding
effectively 'runs' the "create_sandbox" code, but in the context that
it was originally created back in scaffold_generator.rb.
If you have Programming Ruby by Dave Thomas, pages 56 and 356 onwards
have more about this.
If you don't have the book, go out and by it now -- you are digging
into some pretty deep Ruby (and Rails!) here, and a pickaxe would help.
Chris