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

Ticket #7312: active_record_helper_error_messages_on.diff

File active_record_helper_error_messages_on.diff, 3.9 kB (added by stevenbristol, 2 years ago)
  • test/template/active_record_helper_test.rb

    old new  
    3535    @post = Post.new     
    3636    def @post.errors 
    3737      Class.new { 
    38         def on(field) field == "author_name" || field == "body" end 
     38        def on(field) 
     39          case field.to_s 
     40          when "author_name" 
     41            ["can't be empty", "another message"] 
     42          when "body" 
     43            true 
     44          else 
     45            false 
     46         end 
     47        end 
    3948        def empty?() false end  
    4049        def count() 1 end  
    4150        def full_messages() [ "Author name can't be empty" ] end 
     
    179188    assert error_message_on(:post, :author_name) 
    180189  end 
    181190   
     191   
     192   
     193  def test_error_messages_on 
     194    assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><ul><li>Author name can't be empty</li><li>Author name another message</li></ul></div>), error_messages_on("post", "author_name") 
     195  end 
     196   
    182197  def test_error_messages_for_many_objects 
    183198    assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li><li>User email can't be empty</li></ul></div>), error_messages_for("post", "user") 
    184199 
  • lib/action_view/helpers/active_record_helper.rb

    old new  
    9191          '' 
    9292        end 
    9393      end 
     94       
     95       
     96       
     97       
     98      # Returns a string with a div containing all of the error messages for the object and method located as instance variables by the names 
     99      # given.  This method mimics the output of +error_messages_for+, and is meant to be used in leau of +error_message_on+. 
     100      # 
     101      # This div can be tailored by the following options: 
     102      # 
     103      # * <tt>id</tt> - The id of the error div (default: errorExplanation) 
     104      # * <tt>class</tt> - The class of the error div (default: errorExplanation) 
     105      # * <tt>method_name</tt> - The object name to use in the header, or 
     106      # any text that you prefer. If <tt>method_name</tt> is not set, the name of 
     107      # the method will be used. 
     108      # 
     109      # Specifying one object: 
     110      #  
     111      #   error_messages_on 'user', 'login' 
     112      # 
     113      # 
     114      # NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what 
     115      # you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors 
     116      # instance yourself and set it up. View the source of this method to see how easy it is. 
     117      def error_messages_on(object, method, options = {}) 
     118        html = {} 
     119        options.stringify_keys! 
     120        ['id', 'class'].each do |key| 
     121          if options.include?(key) 
     122            value = options[key] 
     123            html[key] = value unless value.blank? 
     124          else 
     125            html[key] = 'errorExplanation' 
     126          end 
     127        end 
     128        name = options['method_name'] ||= method.to_s.humanize.capitalize 
     129        if (obj = instance_variable_get("@#{object}")) && (errors = obj.errors.on(method)) 
     130          [errors].flatten! 
     131        content_tag(:div, 
     132          content_tag(:ul, errors.map {|msg| content_tag(:li, "#{name} #{msg}") }), 
     133          html 
     134        ) 
     135        else  
     136          '' 
     137        end 
     138      end 
     139       
     140       
    94141 
    95142      # Returns a string with a div containing all of the error messages for the objects located as instance variables by the names 
    96143      # given.  If more than one object is specified, the errors for the objects are displayed in the order that the object names are