Module: CollectionErrors::Unify::ClassMethods

Defined in:
lib/collection_errors/unify.rb

Instance Method Summary collapse

Instance Method Details

#unify_errors_on(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/collection_errors/unify.rb', line 8

def unify_errors_on(*args)
  args.each do |collection_name|
    method_name = "unify_errors_on_#{collection_name}"

    define_method method_name do

      # Remove 'collection_name.xxx' attributes from errors
      targets = []
      errors.each do |attribute, message|
        targets << attribute if attribute.to_s =~ /^#{collection_name}\./
      end
      targets.each{|k| errors.delete(k) }

      # Add original errors
      errors.add(collection_name, :invalid) if targets.any?
    end

    after_validation method_name
  end
end