Module: ActiveRecord::ActsAsEntity::InstanceMethods

Defined in:
lib/acts_as_entity/base.rb

Instance Method Summary collapse

Instance Method Details

#setAttrs(args = nil) ⇒ Object

Used to initialize the parameters of an instance before read them in the child app



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/acts_as_entity/base.rb', line 34

def setAttrs( args = nil)
  self.attr_vals.each do |attrVal|
    
    #if attrVal.attrDef.dataType == 'entityDef' && ttrVal.attr_def_name.end_with('Id')
      #we add a virtual attr
    #  write_attribute(attrVal.attr_def_name.remove('Id'), Databaseformalizer::Entity.find(attrVal.value))
    #end
      
    #if the attribut already exist we add it to the list
    if read_attribute(attrVal.attr_def_name) != nil
      result = read_attribute(attrVal.attr_def_name)
      
      #we check if the result is a list of elements or just an item
      if result.class == Array
        result.push(attrVal.value)
      else
        tmp = result
        result = [tmp,attrVal.value]
      end

      write_attribute(attrVal.attr_def_name, result)
    else
      write_attribute(attrVal.attr_def_name, attrVal.value)
    end
  end
end

#update_attribute(key, value) ⇒ Object

update the instance elements attributs after edit on instances from the child app



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/acts_as_entity/base.rb', line 76

def update_attribute(key,value)
  #check if have an entity 
  #hard bug fix because update_attributes is launch for ALL items...
  if self.class.to_s.include? 'Databaseformalizer::'
    self.attributes = attrs
    save
  else
                                                                                                                             
    attrVal = self.attr_vals.find_by_attr_def_name(key)
    if attrVal == nil
      attrDef = Databaseformalizer::AttrDef.find(key);
      attrVal = Databaseformalizer::AttrVal.new(:attrDef => attrDef)
      self.attr_vals << attrVal
    end
    attrVal.value = value
    attrVal.save!
  end
return true
end

#update_attributes(attrs) ⇒ Object

update the instance elements attributs after edit on instances from the child app



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/acts_as_entity/base.rb', line 62

def update_attributes(attrs)
  #check if have an entity 
  #hard bug fix because update_attributes is launch for ALL items...
  if self.class.to_s.include? 'Databaseformalizer::'
    self.attributes = attrs
    save
  else
    for attr in attrs
      update_attribute(attr[0],attr[1])
    end
  end
return true
end