Skip to content

Commit

Permalink
Added ability to pass a :locals hash to a field helper to provide cus…
Browse files Browse the repository at this point in the history
…tom local variables to the template

Signed-off-by: Ryan Heath <rpheath@gmail.com>
  • Loading branch information
scharfie authored and rpheath committed Mar 24, 2009
1 parent da5bc92 commit c944d43
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/form_assistant.rb
Expand Up @@ -102,9 +102,9 @@ def template_exists?(template)

protected
# renders the appropriate partial located in the template root
def render_partial_for(element, field, label, tip, template, helper, required, args)
def render_partial_for(element, field, label, tip, template, helper, required, extra_locals, args)
errors = self.class.ignore_errors ? nil : error_message_for(field)
locals = { :element => element, :field => field, :builder => self, :object => object, :object_name => object_name, :label => label, :errors => errors, :tip => tip, :helper => helper, :required => required }
locals = extra_locals.merge(:element => element, :field => field, :builder => self, :object => object, :object_name => object_name, :label => label, :errors => errors, :tip => tip, :helper => helper, :required => required)

@template.render :partial => "#{self.class.template_root}/#{template}.html.erb", :locals => locals
end
Expand Down Expand Up @@ -203,7 +203,7 @@ def widget(*args, &block)
@template.capture(&block)
end

partial = render_partial_for(element, field, label, tip, template_options[:template], 'widget', required, args)
partial = render_partial_for(element, field, label, tip, template_options[:template], 'widget', required, {}, args)
RPH::FormAssistant::Rules.binding_required? ? @template.concat(partial, block.binding) : @template.concat(partial)
end

Expand All @@ -214,6 +214,7 @@ def widget(*args, &block)
options = args.extract_options!
label_options = extract_options_for_label(field, options)
template_options = extract_options_for_template(helper_name, options)
extra_locals = options.delete(:locals) || {}

# build out the label element (if desired)
label = label_options[:label] === false ? nil : self.label(field, label_options.delete(:text), label_options)
Expand All @@ -236,7 +237,7 @@ def widget(*args, &block)
return render_element(element, field, helper_name, options, label_options[:label] === false) if self.class.ignore_templates

# render the partial template from the desired template root
render_partial_for(element, field, label, tip, template_options[:template], helper_name, required, args)
render_partial_for(element, field, label, tip, template_options[:template], helper_name, required, extra_locals, args)
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/form_assistant_test.rb
Expand Up @@ -143,4 +143,9 @@ def setup

expect_render :partial => template_path('field')
end

test "should pass extra locals" do
form.text_field :first_name, :locals => { :nickname => true }
expect_locals :nickname => true
end
end

0 comments on commit c944d43

Please sign in to comment.