Method: Databaseformalizer::EntitiesHelper.setAttrVals

Defined in:
app/helpers/databaseformalizer/entities_helper.rb

.setAttrVals(params, entity, parent = nil) ⇒ Object


12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/databaseformalizer/entities_helper.rb', line 12

def EntitiesHelper.setAttrVals(params, entity, parent = nil )

  if params == nil; params = []; end   
  params.each do |attrType,attrFormValue|
    attrDef = AttrDef.find(attrType);
    attrVal =  entity.attr_vals.find_by_attr_def_name(attrType)
    #check if item exist or if is in an insert or update
    if attrVal == nil || (attrVal != nil && parent != nil && parent.attrDef.dataType == "openList" )
      attrVal = AttrVal.new(:attrDef => attrDef)
      entity.attr_vals << attrVal
      if parent != nil
        parent.attrValChilds << attrVal
      end
    end

    case attrVal.attrDef.dataType
      when "String"
        attrVal.value = attrFormValue
      when "Date"
        attrVal.value = "25/10/2010"
      when "aChoice"
        attrVal.value = "selected"
      when "checkList"
        #first, we remove unselected items
        attrFormValue.reject!{ |k,val| val == "0" }
        EntitiesHelper.setAttrVals(attrFormValue, entity, attrVal)
        attrVal.value = "isList"
      when "selectOneInList"
        EntitiesHelper.setAttrVals({ attrFormValue => 1 }, entity, attrVal)
        attrVal.value = "isList"
      when "openList"
        attrFormValue.delete("_destroy")
        map = attrFormValue.map{|a| [a[0][0..-14], a[1]]}

        EntitiesHelper.setAttrVals(map, entity, attrVal)
        attrVal.value = "isList"
      when "entityDef"
        attrVal.value = attrFormValue
      else
        attrVal.value = attrFormValue
    end
    attrVal.save
  end
end