Class: ActiveModel::Errors
- Inherits:
-
Object
- Object
- ActiveModel::Errors
- Defined in:
- lib/active_model/dynamic_errors.rb
Instance Method Summary collapse
-
#full_messages ⇒ Object
Redefine the ActiveModel::Errors::full_messages method: Returns all the full error messages in an array.
Instance Method Details
#full_messages ⇒ Object
Redefine the ActiveModel::Errors::full_messages method:
Returns all the full error messages in an array. 'Base' messages are handled as usual.
Non-base messages are prefixed with the attribute name as usual UNLESS
(1) they begin with ‘^’ in which case the attribute name is omitted.
E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
(2) the message is a proc, in which case the proc is invoked on the model object.
E.g. validates_presence_of :assessment_answer_option_id,
:message => Proc.new { |aa| "#{aa.label} (#{aa.group_label}) is required" }
which gives an error message like:
Rate (Accuracy) is required
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_model/dynamic_errors.rb', line 13 def = [] each do |attribute, | = Array.wrap() next if .empty? if attribute == :base .each {|m| << m } else attr_name = attribute.to_s.gsub('.', '_').humanize attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) = { :default => "%{attribute} %{message}", :attribute => attr_name } .each do |m| if m =~ /^\^/ [:default] = "%{message}" << I18n.t(:"errors.dynamic_format", .merge(:message => m[1..-1])) elsif m.is_a? Proc [:default] = "%{message}" << I18n.t(:"errors.dynamic_format", .merge(:message => m.call(@base))) else << I18n.t(:"errors.format", .merge(:message => m)) end end end end end |