Class: Kludge::Many

Inherits:
Part
  • Object
show all
Defined in:
lib/kludge/many.rb

Instance Attribute Summary

Attributes inherited from Part

#children, #errors, #name, #parent, #value

Instance Method Summary collapse

Methods inherited from Part

#dependency, #dependent?, #initialize, #many?, #one?, #valid?

Constructor Details

This class inherits a constructor from Kludge::Part

Instance Method Details

#saveObject



4
5
6
7
# File 'lib/kludge/many.rb', line 4

def save
  value.each(&:save)
  super
end

#validateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kludge/many.rb', line 9

def validate
  if value.any? { |v| !v.valid? }
    value.map(&:errors).each do |e|
      e.each do |attribute, errors_array|
        errors_array.each do |msg|
          errors.add(attribute, msg) unless errors.added?(attribute, msg)
        end
      end
    end
    false
  else
    true
  end
end

#value=(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kludge/many.rb', line 24

def value=(value)
  p value
  @value = value.map do |val|
    if val.kind_of?(Hash)
      if val[:id].present?
        @name.to_s.classify.constantize.find(val.delete(:id)).tap { |v| v.assign_attributes(val) }
      else
        @name.to_s.classify.constantize.new(val)
      end
    else
      val
    end
  end
end