From c944d439228cddd2c4a633a443447f2917675386 Mon Sep 17 00:00:00 2001 From: Chris Scharf Date: Tue, 24 Mar 2009 22:46:44 +0800 Subject: [PATCH] Added ability to pass a :locals hash to a field helper to provide custom local variables to the template Signed-off-by: Ryan Heath --- lib/form_assistant.rb | 9 +++++---- test/form_assistant_test.rb | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/form_assistant.rb b/lib/form_assistant.rb index 594c909..86fe9bf 100644 --- a/lib/form_assistant.rb +++ b/lib/form_assistant.rb @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/test/form_assistant_test.rb b/test/form_assistant_test.rb index dc1c4c3..6f16c66 100644 --- a/test/form_assistant_test.rb +++ b/test/form_assistant_test.rb @@ -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 \ No newline at end of file