avatar

30

Support both FormHelper formats when using form_for:
* f.text_field :website
* text_field :user, :website
Closes #3.

by chrisk, 30 Jul, 2007 05:21 PM
28 30  
8989       # Helper method to determine whether or not +templated_attribute+ has
9090       # been called for a given class and method.
9191       def templated?(class_name, method, options={})  # :nodoc:
92         klass = class_name.classify.constantize
92         klass = class_name_from_options_object_or_class_name(class_name, options)
9393         klass.respond_to?(:templated_attributes_options) && klass.templated_attributes_options[method.to_sym]
9494       end
9595 
9696 
97       # Utility method to figure out what class the user wants to use. This
98       # disambiguation is necessary so we can support both +FormHelper+ formats:
99       #   form_for(@user) do |f|
100       #     f.text_field :website
101       #     text_field :user, :website
102       #   end
103       def class_name_from_options_object_or_class_name(class_name, options={})
104         if options.has_key?(:object)
105           options[:object].class
106         else
107           klass = class_name.to_s.classify.constantize
108         end        
109       end
110 
111 
97112       # Behind the scenes, figure out the starting value for the field, then
98113       # render the field normally with that value (if applicable) and append a
99114       # touch of Javascript to add behavior.
100115       def field_with_templating(field_helper, class_name, method, options = {})   # :nodoc:
101116         options = { :templated_javascript => true }.merge(options)
102         template_options = class_name.classify.constantize.templated_attributes_options[method.to_sym]
117         klass = class_name_from_options_object_or_class_name(class_name, options)
118         instance = options[:object] || self.instance_variable_get("@#{class_name}")
119         template_options = klass.templated_attributes_options[method.to_sym]
103120 
104121         # If calling method on object returns nil, use template value instead
105         field_value = options[:object].__send__(method.to_sym)
122         field_value = instance.__send__(method.to_sym)
106123         if field_value.nil? || field_value.strip.empty?
107124           field_value = template_options[:value]
108125         end
------
118135         
119136         # output using the original method with our modified form field value,
120137         # plus the javascript
121         send("#{field_helper}_without_templating".to_sym, class_name, method, options.merge({:value => field_value})) + javascript_string
138         send("#{field_helper}_without_templating".to_sym, klass.to_s.downcase, method, options.merge({:value => field_value})) + javascript_string
122139       end
123140 
124141 
25 30  
2020   # for testing templated attributes on new records (where all fields have nil values)
2121   def new
2222     @user = User.new
23     render :inline => case params[:attribute]
24       when :bio            then '<% form_for @user do |f| %>  <%= f.text_area :bio %>  <% end %>'
25       when :bio_no_js      then '<% form_for @user do |f| %>  <%= f.text_area :bio, :templated_javascript => false %>  <% end %>'
26       when :favorite_books then '<% form_for @user do |f| %>  <%= f.text_area :favorite_books %>  <% end %>'
27       else                      ''
23     if (params[:style] == :short)
24       render :inline => case params[:attribute]
25         when :bio            then '<% form_for @user do |f| %>  <%= f.text_area :bio %>  <% end %>'
26         when :bio_no_js      then '<% form_for @user do |f| %>  <%= f.text_area :bio, :templated_javascript => false %>  <% end %>'
27         when :favorite_books then '<% form_for @user do |f| %>  <%= f.text_area :favorite_books %>  <% end %>'
28         else                      ''
29       end
30     else
31       render :inline => case params[:attribute]
32         when :bio            then '<% form_for @user do %>  <%= text_area :user, :bio %>  <% end %>'
33         when :bio_no_js      then '<% form_for @user do %>  <%= text_area :user, :bio, :templated_javascript => false %>  <% end %>'
34         when :favorite_books then '<% form_for @user do %>  <%= text_area :user, :favorite_books %>  <% end %>'
35         else                      ''
36       end
2837     end
2938   end
3039   
------
7281     assert !@response.body.include?('</script>')
7382   end
7483   
84   def test_short_form_style_should_produce_same_response_as_using_symbol_style
85     get :new, :attribute => :bio
86     symbol_form = @response.dup
87     get :new, :attribute => :bio, :style => :short
88     short_form = @response.dup
89     assert_equal symbol_form.body, short_form.body
90   end
91   
7592   # TODO: lots more tests to write here
7693   
7794 end