Changeset 1171

Show
Ignore:
Timestamp:
01/05/08 12:41:40 (9 months ago)
Author:
rogelio.samo..@gmail.com
Message:

- Allows form_for in merb_helpers to be used with non-AR model objects. closes #398 [paul.dlug@gmail.com]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_helpers/lib/merb_helpers/form_helpers.rb

    r1169 r1171  
    4949      #   <%= error_messages_for :person %>       
    5050      def error_messages_for(obj, error_li = nil, html_class='submittal_failed') 
    51         return "" if obj.errors.empty? 
     51        return "" if !obj.respond_to?(:errors) || obj.errors.empty? 
    5252        header_message = block_given? ? yield(obj.errors) : "<h2>Form submittal failed because of #{obj.errors.size} #{obj.errors.size == 1 ? 'problem' : 'problems'}</h2>" 
    5353        ret = %Q{ 
     
    552552      # Fall silently back to post if a method is given that is not supported here 
    553553      def set_form_method(options = {}, obj = nil) 
    554         options[:method] ||= (!obj || obj.new_record? ? :post : :put) 
     554        options[:method] ||= (!obj || (obj.respond_to?(:new_record?) && !obj.new_record?) ? :put : :post) 
    555555        if ![:get,:post].include?(options[:method]) 
    556556          fake_form_method = options[:method] if [:put, :delete].include?(options[:method]) 
     
    576576       
    577577      def errorify_field(attrs, col) 
    578         attrs.add_html_class!("error") if @_obj.errors.on(col) 
     578        attrs.add_html_class!("error") if @_obj.respond_to?(:errors) && @_obj.errors.on(col) 
    579579      end    
    580580       
  • plugins/merb_helpers/spec/merb_helpers_spec.rb

    r1169 r1171  
    109109    @obj2 = FakeModel2.new 
    110110    form_for(:obj2) do 
     111    end 
     112    _buffer.should match_tag(:form, :method => "post") 
     113    _buffer.should_not match_tag(:input, :type => "hidden", :name => "_method") 
     114  end 
     115   
     116  it "should set the method to post if the object does not respond to new_record?" do 
     117    @obj3 = FakeModel3.new 
     118    form_for(:obj3) do 
    111119    end 
    112120    _buffer.should match_tag(:form, :method => "post") 
  • plugins/merb_helpers/spec/spec_helper.rb

    r1146 r1171  
    8282end 
    8383 
     84class FakeModel3 
     85  attr_accessor :foo, :bar 
     86end 
     87 
     88 
    8489class FakeErrors 
    8590