error_messages_for 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,82 @@
1
+ ErrorMessagesFor
2
+ ================
3
+
4
+ This gem enchances the default rails functionality for the ActiveRecord helper 'error_messages_for'
5
+
6
+ What it does:
7
+
8
+ 1. It allows you to enter an array of AR objects as the first argument and will display them all in the same output block.
9
+
10
+ Example:
11
+
12
+ <%= error_messages_for [:user, :topic, :item] %>
13
+
14
+ With this bit of code you'll get something that looks like the following:
15
+
16
+ There were 3 errors that need to be fixed:
17
+
18
+ * User username can't be blank
19
+ * Topic title can't be blank
20
+ * Item body can't be blank
21
+
22
+ 2. The second argument allows you to pass in a partial to render the errors. By default the first thing it looks for is your partial, if that's nil, it'll look for the following partial: RAILS_ROOT/app/views/application/_error_messages.rhtml If it doesn't find that it'll default to some inline text.
23
+
24
+ Example:
25
+
26
+ <%= error_messages_for :user, "my_errors" %>
27
+
28
+ You had 2 errors fool!
29
+
30
+ - User username can't be blank
31
+ - User email can't be blank
32
+
33
+ This is cool if you want to change the way errors are displayed in a pop-up vs. your main site, or you want to change them on a whole section of the site, etc...
34
+
35
+ 3. A method is added to all of your AR objects called, 'business_name'. By default this method will return the class of the AR object.
36
+
37
+ Example:
38
+
39
+ user.business_name returns "User"
40
+ item.business_name returns "Item"
41
+
42
+ This is used when generating the error messages. Here's the way a message is rendered: "business_name column_name error_message". So the following model:
43
+
44
+ class Item < ActiveRecord::Base
45
+ validates_presence_of :body, :message => "can't be blank."
46
+ end
47
+
48
+ would produce the following error message:
49
+
50
+ "Item body can't be blank."
51
+
52
+ The business_name method allows you to override the first part of the error message if your business speak doesn't match your database speak. An example would be if your business wants to call Items, Articles. In that case you would do the following:
53
+
54
+ class Item < ActiveRecord::Base
55
+ validates_presence_of :body, :message => "can't be blank."
56
+
57
+ def business_name
58
+ "Article"
59
+ end
60
+ end
61
+
62
+ would produce the following error message:
63
+
64
+ "Article body can't be blank."
65
+
66
+ Pretty sweet, eh?
67
+
68
+ 4. The last thing you can do is override the entire message from start to finish. Let's say, using the last example, that the business doesn't like the message, "Article body can't be blank.", instead they want the message to be, "Oi, give us a body!!". That's easy. All you have to do is start your :message attribute with a "^".
69
+
70
+ Example:
71
+
72
+ class Item < ActiveRecord::Base
73
+ validates_presence_of :body, :message => "^Oi, give us a body!!"
74
+ end
75
+
76
+ would produce the following error message:
77
+
78
+ "Oi, give us a body!!"
79
+
80
+
81
+
82
+ This gem offers you great flexibility, and probably should've been done right from the start in Rails core. Hope it helps.
@@ -0,0 +1,65 @@
1
+ class ActiveRecord::Base
2
+
3
+ def business_name
4
+ self.class.name.titlecase
5
+ end
6
+
7
+ end
8
+
9
+ module ActionView
10
+ module Helpers
11
+ module ActiveRecordHelper
12
+ DEFAULT_PARTIAL = %{
13
+ <div>
14
+ <div class="errorExplanation" id="errorExplanation">
15
+ <h2><%= pluralize(errors.size, "error") %> <%= errors.size == 1 ? "needs" : "need" %> to be fixed.</h2>
16
+ <ul>
17
+ <% for error in errors -%>
18
+ <li><%= error %></li>
19
+ <% end -%>
20
+ </ul>
21
+ </div>
22
+ </div>
23
+ }
24
+
25
+ alias_method :old_error_messages_for, :error_messages_for
26
+
27
+ def error_messages_for(object_names = [], view_partial = nil)
28
+ object_names = [object_names]
29
+ object_names.flatten!
30
+ app_errors = []
31
+ object_names.each do |name|
32
+ object = instance_variable_get("@#{name}")
33
+ if object
34
+ object.errors.each do |key, value|
35
+ if value.match(/^\^/)
36
+ app_errors << value[1..value.length]
37
+ else
38
+ if key.class == String and key == "base"
39
+ app_errors << "#{value}"
40
+ else
41
+ app_errors << "#{object.business_name} #{key.underscore.split('_').join(' ').humanize} #{value}"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ unless app_errors.empty?
49
+ if view_partial.nil?
50
+ if File.exist?("#{RAILS_ROOT}/app/views/application/_error_messages.rhtml")
51
+ render :partial => "application/error_messages", :locals => {:errors => app_errors}
52
+ else
53
+ render :inline => DEFAULT_PARTIAL, :locals => {:errors => app_errors}
54
+ end
55
+ else
56
+ render :partial => view_partial, :locals => {:errors => app_errors}
57
+ end
58
+ else
59
+ ""
60
+ end
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class ErrorMessagesForTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ # replace this with tests of your own.
12
+ def test_truth
13
+ assert true
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ require "test/unit"
2
+
3
+ # place common methods, assertions, and other type things in this file so
4
+ # other tests will have access to them.
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: error_messages_for
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.4
7
+ date: 2007-02-28 00:00:00 -05:00
8
+ summary: Overrides the default error_messages_for helper and extends it to make it useful. It adds the ability to display messages for an error of AR objects, it can use a partial to render the errors, plus other enhancements.
9
+ require_paths:
10
+ - lib
11
+ - lib
12
+ email:
13
+ homepage:
14
+ rubyforge_project:
15
+ description: Overrides the default error_messages_for helper and extends it to make it useful. It adds the ability to display messages for an error of AR objects, it can use a partial to render the errors, plus other enhancements.
16
+ autorequire:
17
+ - error_messages_for
18
+ default_executable:
19
+ bindir: bin
20
+ has_rdoc: false
21
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
22
+ requirements:
23
+ - - ">"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.0.0
26
+ version:
27
+ platform: ruby
28
+ signing_key:
29
+ cert_chain:
30
+ post_install_message:
31
+ authors:
32
+ - markbates
33
+ files:
34
+ - lib/error_messages_for.rb
35
+ - README
36
+ test_files:
37
+ - test/error_messages_for_test.rb
38
+ - test/test_helper.rb
39
+ rdoc_options: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ requirements: []
48
+
49
+ dependencies:
50
+ - !ruby/object:Gem::Dependency
51
+ name: rails
52
+ version_requirement:
53
+ version_requirements: !ruby/object:Gem::Version::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 1.0.0
58
+ version: