Index: rails_plugins/templated_attribute/lib/templated_attribute_helper.rb =================================================================== --- rails_plugins/templated_attribute/lib/templated_attribute_helper.rb (revision 28) +++ rails_plugins/templated_attribute/lib/templated_attribute_helper.rb (revision 30) @@ -89,20 +89,37 @@ # Helper method to determine whether or not +templated_attribute+ has # been called for a given class and method. def templated?(class_name, method, options={}) # :nodoc: - klass = class_name.classify.constantize + klass = class_name_from_options_object_or_class_name(class_name, options) klass.respond_to?(:templated_attributes_options) && klass.templated_attributes_options[method.to_sym] end + # Utility method to figure out what class the user wants to use. This + # disambiguation is necessary so we can support both +FormHelper+ formats: + # form_for(@user) do |f| + # f.text_field :website + # text_field :user, :website + # end + def class_name_from_options_object_or_class_name(class_name, options={}) + if options.has_key?(:object) + options[:object].class + else + klass = class_name.to_s.classify.constantize + end + end + + # Behind the scenes, figure out the starting value for the field, then # render the field normally with that value (if applicable) and append a # touch of Javascript to add behavior. def field_with_templating(field_helper, class_name, method, options = {}) # :nodoc: options = { :templated_javascript => true }.merge(options) - template_options = class_name.classify.constantize.templated_attributes_options[method.to_sym] + klass = class_name_from_options_object_or_class_name(class_name, options) + instance = options[:object] || self.instance_variable_get("@#{class_name}") + template_options = klass.templated_attributes_options[method.to_sym] # If calling method on object returns nil, use template value instead - field_value = options[:object].__send__(method.to_sym) + field_value = instance.__send__(method.to_sym) if field_value.nil? || field_value.strip.empty? field_value = template_options[:value] end @@ -118,7 +135,7 @@ # output using the original method with our modified form field value, # plus the javascript - send("#{field_helper}_without_templating".to_sym, class_name, method, options.merge({:value => field_value})) + javascript_string + send("#{field_helper}_without_templating".to_sym, klass.to_s.downcase, method, options.merge({:value => field_value})) + javascript_string end