custom_error_message 1.0.0 → 1.1.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/custom_error_message.rb +33 -12
- metadata +5 -4
data/lib/custom_error_message.rb
CHANGED
@@ -1,16 +1,5 @@
|
|
1
1
|
module ActiveRecord
|
2
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
3
|
def full_messages
|
15
4
|
full_messages = []
|
16
5
|
|
@@ -30,7 +19,39 @@ module ActiveRecord
|
|
30
19
|
end
|
31
20
|
end
|
32
21
|
|
33
|
-
|
22
|
+
full_messages
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module ActiveModel
|
28
|
+
class Errors < ActiveSupport::OrderedHash
|
29
|
+
def full_messages
|
30
|
+
full_messages = []
|
31
|
+
|
32
|
+
each do |attribute, messages|
|
33
|
+
messages = Array.wrap(messages)
|
34
|
+
next if messages.empty?
|
35
|
+
|
36
|
+
if attribute == :base
|
37
|
+
messages.each {|m| full_messages << m }
|
38
|
+
else
|
39
|
+
attr_name = attribute.to_s.gsub('.', '_').humanize
|
40
|
+
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
|
41
|
+
options = { :default => "%{attribute} %{message}", :attribute => attr_name }
|
42
|
+
|
43
|
+
|
44
|
+
messages.each do |m|
|
45
|
+
if m =~ /^\^/
|
46
|
+
full_messages << I18n.t(:"errors.format.full_message", options.merge(:message => m[1..-1], :default => "%{message}"))
|
47
|
+
else
|
48
|
+
full_messages << I18n.t(:"errors.format", options.merge(:message => m))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
full_messages
|
34
55
|
end
|
35
56
|
end
|
36
57
|
end
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_error_message
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 961915996
|
5
|
+
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
10
|
+
- pre
|
11
|
+
version: 1.1.0.pre
|
11
12
|
platform: ruby
|
12
13
|
authors:
|
13
14
|
- David Easley
|