Class: Kludge::One

Inherits:
Part
  • Object
show all
Defined in:
lib/kludge/one.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
8
# File 'lib/kludge/one.rb', line 4

def save
  parent.value.send("#{name}=", value) if parent
  value.save
  super
end

#validateObject



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

def validate
  if !value.valid? 
    value.errors.each do |attribute, errors_array|
      errors_array.each do |msg|
        errors.add(attribute, msg) unless errors.added?(attribute, msg)
      end
    end
    false
  else
    true
  end
end

#value=(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kludge/one.rb', line 23

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