Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8270: more_options_on_error_messages_for_with_tests_rebased.diff

File more_options_on_error_messages_for_with_tests_rebased.diff, 4.6 kB (added by rmm5t, 1 year ago)

Rebased after Koz's suggestion

  • actionpack/test/template/active_record_helper_test.rb

    old new  
    207207 
    208208    # should space object name 
    209209    assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this chunky bacon from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "chunky_bacon") 
     210 
     211    # hide header and explanation messages with nil or empty string 
     212    assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => nil, :message => "") 
     213 
     214    # override header and explanation messages 
     215    header_message = "Yikes! Some errors" 
     216    message = "Please fix the following fields and resubmit:" 
     217    assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>#{header_message}</h2><p>#{message}</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => header_message, :message => message) 
    210218  end 
    211219   
    212220  def test_error_messages_for_non_instance_variable 
  • actionpack/lib/action_view/helpers/active_record_helper.rb

    old new  
    106106      # * <tt>header_tag</tt> - Used for the header of the error div (default: h2) 
    107107      # * <tt>id</tt> - The id of the error div (default: errorExplanation) 
    108108      # * <tt>class</tt> - The class of the error div (default: errorExplanation) 
    109       # * <tt>object</tt> - The object (or array of objects) for which to display errors,  
    110       # if you need to escape the instance variable convention 
    111       # * <tt>object_name</tt> - The object name to use in the header, or 
    112       # any text that you prefer. If <tt>object_name</tt> is not set, the name of 
    113       # the first object will be used. 
     109      # * <tt>object</tt> - The object (or array of objects) for which to display errors, if you need to escape the instance variable convention 
     110      # * <tt>object_name</tt> - The object name to use in the header, or any text that you prefer. If <tt>object_name</tt> is not set, the name of the first object will be used. 
     111      # * <tt>header_message</tt> - The message in the header of the error div.  Pass +nil+ or an empty string to avoid the header message altogether. (default: X errors prohibited this object from being saved) 
     112      # * <tt>message</tt> - The explanation message after the header message and before the error list.  Pass +nil+ or an empty string to avoid the explanation message altogether.  (default: There were problems with the following fields:) 
    114113      # 
    115114      # To specify the display for one object, you simply provide its name as a parameter.  For example, for the +User+ model: 
    116115      #  
     
    147146              html[key] = 'errorExplanation' 
    148147            end 
    149148          end 
    150           header_message = "#{pluralize(count, 'error')} prohibited this #{(options[:object_name] || params.first).to_s.gsub('_', ' ')} from being saved" 
     149          options[:object_name] ||= params.first 
     150          options[:header_message] = "#{pluralize(count, 'error')} prohibited this #{options[:object_name].to_s.gsub('_', ' ')} from being saved" unless options.include?(:header_message) 
     151          options[:message] ||= 'There were problems with the following fields:' unless options.include?(:message) 
    151152          error_messages = objects.map {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } } 
    152           content_tag(:div, 
    153             content_tag(options[:header_tag] || :h2, header_message) << 
    154               content_tag(:p, 'There were problems with the following fields:') << 
    155               content_tag(:ul, error_messages), 
    156             html 
    157           ) 
     153 
     154          contents = '' 
     155          contents << content_tag(options[:header_tag] || :h2, options[:header_message]) unless options[:header_message].blank? 
     156          contents << content_tag(:p, options[:message]) unless options[:message].blank? 
     157          contents << content_tag(:ul, error_messages) 
     158 
     159          content_tag(:div, contents, html) 
    158160        else 
    159161          '' 
    160162        end