Skip to content

Commit

Permalink
added :render options to form.input and .inputs for ErbRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
linojon committed Mar 17, 2010
1 parent 8aeb6b7 commit 2a12a2d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
24 changes: 19 additions & 5 deletions RENDERER.textile
Expand Up @@ -12,8 +12,8 @@ h2. Renderer API
input hash may include keys

:method => symbol
:as => the input type (eg :string, :textarea, etc)
:options => original set of options
:as => the input type (eg :string, :text, etc)
:options => original options
:label => string with html label tag
:hint => hints string (pre-rendered)
:errors => actual errors on the method
Expand Down Expand Up @@ -47,6 +47,7 @@ I am also leaving how Formtastic renders _required_ labels (with attr tag). If y
<pre>
render_field_set( fieldset )

:options => original options
:legend => string that should go into the legend
:contents => string or array of html tag strings that make the content of the field set
:wrapper => hash of html attributes for the fieldset
Expand Down Expand Up @@ -77,19 +78,32 @@ The ERB renderer renders an ERB partial for each input. There are 4 built-in par
_fieldset.html.erb field set wrapper
</pre>

You can create a partial for each and any specific input (the :as value), such as "_textarea.html.erb". If not found, it then looks for a partial for the input category ("chronos" for date and time selects; "items" for radio and check boxes' and "input" for all others).
You can create a partial for each and any specific input (the :as value), such as "_text.html.erb" for a text area. If not found, it then looks for a partial for the input category ("chronos" for date and time selects; "items" for radio and check boxes' and "input" for all others).

The renderer looks for partials in the current controller's view directory (eg app/views/foos/_text.html.erb), then in a Formatastic view directory (app/views/formtastic/_text.html.erb), and then in the plug-in's directory.

Using the ERB renderer, form.input takes the following new options:

<pre>
:partial => "partial" name of partial to use (overrides the defaults)
</pre>

Similarly, , form.inputs take the following new options:

<pre>
:partial => "partial" name of partial to use (overrides the default "fieldset")
</pre>

The renderer looks for partials in the current controller's view directory (eg app/views/foos/_textarea.html.erb), then in a Formatastic view directory (app/views/formtastic/_textarea.html.erb), and then in the plug-in's directory.

h3. InspectorRenderer

Dumps the arguments to the renderer api (in yaml format). Useful for debugging.


h2. To Do

* write rspecs for renderers
* add partial layouts
* option to specify partial name in the DSL
* option to specify inline template string in the DSL
* option to pass through arbitrary options to the renderer
* add mustache renderer
Expand Down
2 changes: 1 addition & 1 deletion app/views/formtastic/_chronos.html.erb
Expand Up @@ -5,7 +5,7 @@
</legend>
<ol>
<% chronos.each do |item| %>
<li <%= "class=\"#{item[:wrapper][:class]}\"" if item[:wrapper][:class].present? %>>
<li <%= raw "class=\"#{item[:wrapper][:class]}\"" if item[:wrapper][:class].present? %>>
<%= item[:label] -%>
<%= item[:input] -%>
</li>
Expand Down
2 changes: 1 addition & 1 deletion app/views/formtastic/_fieldset.html.erb
@@ -1,4 +1,4 @@
<fieldset <%= wrapper.map {|atr,value| "#{atr}=\"#{value}\""}.join(' ') %>>
<fieldset <%= raw wrapper.map {|atr,value| "#{atr}=\"#{value}\""}.join(' ') %>>
<% unless legend.blank? -%>
<legend>
<span><%= legend %></span>
Expand Down
2 changes: 1 addition & 1 deletion app/views/formtastic/_items.html.erb
Expand Up @@ -5,7 +5,7 @@
</legend>
<ol>
<% items.each do |item| -%>
<li <%= "class=\"#{item[:wrapper][:class]}\"" if item[:wrapper][:class].present? %>>
<li <%= raw "class=\"#{item[:wrapper][:class]}\"" if item[:wrapper][:class].present? %>>
<label for="<%= item[:input_id] %>">
<%= item[:input] -%>
<%= item[:label] -%>
Expand Down
7 changes: 4 additions & 3 deletions lib/formtastic.rb
Expand Up @@ -1309,8 +1309,8 @@ def required_or_optional_string(required) #:nodoc:
#
def field_set_and_list_wrapping(*args, &block) #:nodoc:
contents = args.last.is_a?(::Hash) ? '' : args.pop.flatten
html_options = args.extract_options!

options = args.extract_options!
html_options = options.dup
legend = html_options.delete(:name).to_s
legend %= parent_child_index(html_options[:parent]) if html_options[:parent]

Expand All @@ -1323,9 +1323,10 @@ def field_set_and_list_wrapping(*args, &block) #:nodoc:
end

fieldset = render_field_set({
:options => options,
:legend => legend,
:contents => contents,
:wrapper => html_options.except(:builder, :parent)
:wrapper => html_options.except(:builder, :parent, :partial) #is there a better way to remove render-specific options?
})

template.concat(fieldset) if block_given?
Expand Down
14 changes: 9 additions & 5 deletions lib/formtastic/renderers/erb_renderer.rb
Expand Up @@ -12,26 +12,30 @@ module ErbRenderer
def render_input(input)
#debugger
begin
template.render( input[:as].to_s, input )
partial = input[:options][:partial] if input[:options]
partial ||= input[:as].to_s
template.render( partial, input )
rescue ActionView::MissingTemplate
begin
template.render( "formtastic/#{input[:as]}", input )
template.render( "formtastic/#{partial}", input )
rescue ActionView::MissingTemplate
partial = input[:chronos].present? ? 'chronos' : (input[:items].present? ? 'items' : 'input')
begin
template.render( partial, input )
rescue ActionView::MissingTemplate
template.render( "formtastic/"+partial, input )
template.render( "formtastic/#{partial}", input )
end
end
end
end

def render_field_set(fieldset)
begin
template.render( 'fieldset', fieldset )
partial = fieldset[:options][:partial] if fieldset[:options]
partial ||= 'fieldset'
template.render( partial, fieldset )
rescue ActionView::MissingTemplate
template.render( 'formtastic/fieldset', fieldset )
template.render( "formtastic/#{partial}", fieldset )
end
end

Expand Down

0 comments on commit 2a12a2d

Please sign in to comment.