Module: Ooor::TypeCasting

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/ooor/type_casting.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

OPERATORS =
["=", "!=", "<=", "<", ">", ">=", "=?", "=like", "=ilike", "like", "not like", "ilike", "not ilike", "in", "not in", "child_of"]

Instance Method Summary collapse

Instance Method Details

#cast_relation(k, v, one2many_associations, many2many_associations) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ooor/type_casting.rb', line 174

def cast_relation(k, v, one2many_associations, many2many_associations)
  if one2many_associations[k]
    return v.collect do |value|
      if value.is_a?(Base) #on the fly creation as in the GTK client
        [0, 0, value.to_openerp_hash]
      else
        if value.is_a?(Hash)
          [0, 0, value]
        else
          [1, value, {}]
        end
      end
    end
  elsif many2many_associations[k]
    return v = [[6, 0, v]]
  end
end

#cast_relations_to_openerp(associations = @associations) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ooor/type_casting.rb', line 137

def cast_relations_to_openerp(associations=@associations)
  associations2 = {}
  associations.each do |k, v|
    if k.match(/_ids$/) && !self.class.associations_keys.index(k) && self.class.associations_keys.index(rel = k.gsub(/_ids$/, ""))
      if v.is_a? Array
       v.reject! {|i| i == ''}.map! {|i| i.to_i}
      end
      associations2[rel] = v
    elsif v.is_a?(Array) && (v.size == 0 or v[1].is_a?(String)) #reject non assigned many2one or empty list
     next
    else
      if k.end_with?("_ids") && v.is_a?(String)
        v = v.split(",").map{|i| i.to_i}
      end
      associations2[k] = v
    end
  end

  associations2.each do |k, v| #see OpenERP awkward associations API
    #already casted, possibly before server error!
    next if (v.is_a?(Array) && v.size == 1 && v[0].is_a?(Array)) \
            || self.class.many2one_associations[k] \
            || !v.is_a?(Array)
    new_rel = self.cast_relation(k, v, self.class.one2many_associations, self.class.many2many_associations)
    if new_rel #matches a known o2m or m2m
      associations2[k] = new_rel
    else
      self.class.many2one_associations.each do |k2, field| #try to cast the association to an inherited o2m or m2m:
        linked_class = self.class.const_get(field['relation'])
        new_rel = self.cast_relation(k, v, linked_class.one2many_associations, linked_class.many2many_associations)
        associations2[k] = new_rel and break if new_rel
      end
    end
  end
  associations2
end

#to_openerp_hash(attributes = @attributes, associations = @associations) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ooor/type_casting.rb', line 123

def to_openerp_hash(attributes=@attributes, associations=@associations)
  associations = cast_relations_to_openerp(associations)
  blacklist = %w[id write_date create_date write_ui create_ui]
  r = {}
  attributes.reject {|k, v| blacklist.index(k)}.merge(associations).each do |k, v|
    if k.end_with?("_id") && !self.class.associations_keys.index(k) && self.class.associations_keys.index(k.gsub(/_id$/, ""))
      r[k.gsub(/_id$/, "")] = v && v.to_i || v
    else
      r[k] = v
    end
  end
  r
end