Module: Mongoid::EmbeddedErrors
- Defined in:
- lib/mongoid/embedded_errors/version.rb,
lib/mongoid/embedded_errors.rb
Constant Summary collapse
- VERSION =
'3.0.1'
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(klass) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/mongoid/embedded_errors.rb', line 8 def self.included(klass) return if klass.instance_methods.include?(:errors_without_embedded_errors) klass.send :alias_method, :errors_without_embedded_errors, :errors klass.send :alias_method, :errors, :errors_with_embedded_errors end |
Instance Method Details
#errors_with_embedded_errors ⇒ Object
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 43 44 45 46 47 48 |
# File 'lib/mongoid/embedded_errors.rb', line 15 def .tap do |errs| .each do |name, | # name is something like pages or sections # if there is an 'is invalid' message for the relation then let's work it: next unless Array(public_send(name)).any? { |doc| doc.errors.any? } # first delete the unless 'is invalid' error for the relation errs[name].delete 'is invalid' errs.delete name.to_sym if errs[name].empty? # next, loop through each of the relations (pages, sections, etc...) [public_send(name)].flatten.reject(&:nil?).each_with_index do |rel, i| next unless rel.errors.any? # get each of their individual message and add them to the parent's errors: rel.errors.each do |k, v| relation = if Gem::Version.new(Mongoid::VERSION) >= Gem::Version.new('7.0.0') .class else .relation end key = if relation.equal? EMBEDS_MANY "#{name}[#{i}].#{k}" else "#{name}.#{k}" end.to_sym errs.delete(key) errs.add key, v if v.present? end end end end end |