So I tried last night to get hack some code around Formtastic and the
Rails form helpers to so the forms can be pretty printed, because
they're the only thing in my apps that aren't and it would be cool
if...
I think it's probably pretty easy to do. I looked through the haml
gem and found this piece:
module FormHelper
def form_for_with_haml(object_name, *args, &proc)
wrap_block = block_given? && is_haml? && block_is_haml?(proc)
if wrap_block
oldproc = proc
proc = haml_bind_proc do |*args|
tab_up
oldproc.call(*args)
tab_down
concat haml_indent
end
concat haml_indent
end
form_for_without_haml(object_name, *args, &proc)
concat "\n" if wrap_block
Haml::Helpers::ErrorReturn.new("form_for") if is_haml?
end
alias_method :form_for_without_haml, :form_for
alias_method :form_for, :form_for_with_haml
end
And that `haml_bind_proc` method looks like this:
def haml_bind_proc(&proc)
_hamlout = haml_buffer
_erbout = _hamlout.buffer
proc { |*args| proc.call(*args) }
end
I still haven't quite wrapped my head around that, but it looks like
it's basically saying execute the form block in the context of the
haml buffer's current position/indentation... Still figuring out how
the block.binding references are passed around and how they work.
My question is this basically, why isn't the indentation working in
the following situation?
In ActionView::Helpers::FormHelper#form_for, I tried replacing this:
concat(form_tag(options.delete(:url) || {}, options.delete(:html)
|| {}))
fields_for(object_name, *(args << options), &proc)
concat('</form>'.html_safe)
with this
result = capture_haml do
haml_tag :form, the_right_options do
fields_for(object_name, *(args << options), &proc)
end
end
concat(result)
And then in
Formtastic::SemanticFormBuilder#field_set_and_list_wrapping, which
creates the fieldset/li/input elements, I did the same.
The problem is, indentation looks like this:
<form>
<fieldset>
<li>
...proper indentation
</li>
</fieldset>
</form>
I think because the `haml_tag :form, block` binding isn't being passed
through to the FormBuilder. Don't really know where to start there,
any ideas how to get this to work? If this is easy to solve, then
it'll be pretty easy to write a helper gem to pretty-print forms in
Rails using the built-in form helpers!
Thanks,
Lance
--
You received this message because you are subscribed to the Google Groups
"Haml" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/haml?hl=en.