custom_error_message 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +52 -0
  3. data/init.rb +1 -0
  4. data/lib/custom_error_message.rb +36 -0
  5. metadata +74 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 David Easley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,52 @@
1
+ Custom Error Message
2
+ ====================
3
+
4
+ This plugin gives you the option to not have your custom validation error message
5
+ prefixed with the attribute name. Ordinarily, if you have, say:
6
+
7
+ validates_acceptance_of :accepted_terms, :message => 'Please accept the terms of service'
8
+
9
+ You'll get the following error message:
10
+
11
+ Accepted terms Please accept the terms of service
12
+
13
+ This plugin allows you to omit the attribute name for specific messages. All you have to do
14
+ is begin the message with a '^' character. Example:
15
+
16
+ validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
17
+
18
+ Nigel Ramsay added the ability to specify a proc to generate the message.
19
+
20
+ validates_presence_of :assessment_answer_option_id,
21
+ :message => Proc.new { |aa| "#{aa.label} (#{aa.group_label}) is required" }
22
+
23
+ which gives an error message like: Rate (Accuracy) is required
24
+
25
+
26
+
27
+
28
+ Detail
29
+ ------
30
+
31
+ Redefine the ActiveRecord::Errors::full_messages method:
32
+ Returns all the full error messages in an array. 'Base' messages are handled as usual.
33
+ Non-base messages are prefixed with the attribute name as usual UNLESS
34
+ (1) they begin with '^' in which case the attribute name is omitted.
35
+ E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
36
+ (2) the message is a proc, in which case the proc is invoked on the model object.
37
+ E.g. validates_presence_of :assessment_answer_option_id,
38
+ :message => Proc.new { |aa| "#{aa.label} (#{aa.group_label}) is required" }
39
+ which gives an error message like:
40
+ Rate (Accuracy) is required
41
+
42
+
43
+ Download
44
+ --------
45
+
46
+ http://rubyforge.org/projects/custom-error-message/
47
+
48
+
49
+ Bugs & feedback
50
+ ---------------
51
+
52
+ Please send bug reports, patches and feedback to David Easley at easleydp@gmail.com
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'custom_error_message'
@@ -0,0 +1,36 @@
1
+ module ActiveRecord
2
+ class Errors
3
+
4
+ # Redefine the ActiveRecord::Errors::full_messages method:
5
+ # Returns all the full error messages in an array. 'Base' messages are handled as usual.
6
+ # Non-base messages are prefixed with the attribute name as usual UNLESS
7
+ # (1) they begin with '^' in which case the attribute name is omitted.
8
+ # E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
9
+ # (2) the message is a proc, in which case the proc is invoked on the model object.
10
+ # E.g. validates_presence_of :assessment_answer_option_id,
11
+ # :message => Proc.new { |aa| "#{aa.label} (#{aa.group_label}) is required" }
12
+ # which gives an error message like:
13
+ # Rate (Accuracy) is required
14
+ def full_messages
15
+ full_messages = []
16
+
17
+ @errors.each_key do |attr|
18
+ @errors[attr].each do |msg|
19
+ next if msg.nil?
20
+ msg = msg.respond_to?(:message) ? msg.message : msg
21
+ if attr == "base"
22
+ full_messages << msg
23
+ elsif msg =~ /^\^/
24
+ full_messages << msg[1..-1]
25
+ elsif msg.is_a? Proc
26
+ full_messages << msg.call(@base)
27
+ else
28
+ full_messages << @base.class.human_attribute_name(attr) + " " + msg
29
+ end
30
+ end
31
+ end
32
+
33
+ return full_messages
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: custom_error_message
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - David Easley
14
+ - Jeremy Durham
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2009-12-12 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: |-
24
+ This plugin gives you the option to not have your custom validation error message
25
+ prefixed with the attribute name
26
+ email: jeremydurham@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - MIT-LICENSE
34
+ files:
35
+ - init.rb
36
+ - lib/custom_error_message.rb
37
+ - README
38
+ - MIT-LICENSE
39
+ has_rdoc: true
40
+ homepage: http://github.com/jeremydurham/custom-err-msg
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Custom Error Message plugin for Rails
73
+ test_files: []
74
+