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"]
BLACKLIST =
%w[id write_date create_date write_ui create_ui]

Instance Method Summary collapse

Instance Method Details

#cast_association(k) ⇒ Object

talk OpenERP cryptic associations API



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ooor/type_casting.rb', line 163

def cast_association(k)
  if self.class.one2many_associations[k]
    if @loaded_associations[k]
      v = @loaded_associations[k].select {|i| i.changed?}
      v = @associations[k] if v.empty?
    else
      v = @associations[k]
    end
    cast_o2m_association(v)
  elsif self.class.many2many_associations[k]
    v = @associations[k]
    [[6, false, (v || []).map {|i| i.is_a?(Base) ? i.id : i}]]
  elsif self.class.many2one_associations[k]
    v = @associations[k] # TODO support for nested
    cast_m2o_association(v)
  end
end

#cast_m2o_association(v) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/ooor/type_casting.rb', line 212

def cast_m2o_association(v)
  if v.is_a?(Array)
    v[0]
  elsif v.is_a?(Base)
    v.id
  else
    v
  end
end

#cast_o2m_association(v) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ooor/type_casting.rb', line 181

def cast_o2m_association(v)
  v.collect do |value|
    if value.is_a?(Base)
      if value.new_record?
        [0, 0, value.to_openerp_hash]
      elsif value.marked_for_destruction?
        [2, value.id]
      elsif value.id
        [1, value.id , value.to_openerp_hash]
      end
    elsif value.is_a?(Hash)
      [0, 0, value]
    else #use case?
      [1, value, {}]
    end
  end
end

#cast_o2m_nested_attributes(v) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ooor/type_casting.rb', line 199

def cast_o2m_nested_attributes(v)
  v.keys.collect do |key|
    val = v[key]
    if !val["_destroy"].blank?
      [2, val[:id].to_i || val['id']]
    elsif val[:id] || val['id']
      [1, val[:id].to_i || val['id'], val]
    else
      [0, 0, val]
    end
  end
end

#get_changed_valuesObject



156
157
158
159
160
# File 'lib/ooor/type_casting.rb', line 156

def get_changed_values
  attribute_keys = changed.select {|k| self.class.fields.has_key?(k)} - BLACKLIST
  association_keys = changed.select {|k| self.class.associations_keys.index(k)}
  return attribute_keys, association_keys
end

#sanitize_association(skey, value) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ooor/type_casting.rb', line 119

def sanitize_association(skey, value)
  if value.is_a?(Ooor::Base) || value.is_a?(Array) && value.all? {|i| i.is_a?(Ooor::Base)}
    value
  elsif value.is_a?(Array) && !self.class.many2one_associations.keys.index(skey)
    value.reject {|i| i == ''}.map {|i| i.is_a?(String) ? i.to_i : i}
  elsif value.is_a?(String)
    sanitize_association_as_string(skey, value)
  else
    value
  end
end

#sanitize_association_as_string(skey, value) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/ooor/type_casting.rb', line 131

def sanitize_association_as_string(skey, value)
  if self.class.polymorphic_m2o_associations.has_key?(skey)
    value
  elsif self.class.many2one_associations.has_key?(skey)
    sanitize_m2o_association(value)
  else
    value.split(",").map {|i| i.to_i}
  end
end

#sanitize_attribute(skey, value) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ooor/type_casting.rb', line 104

def sanitize_attribute(skey, value)
  type = self.class.fields[skey]['type']
  if type == 'boolean' && value == 1 || value == "1"
    true
  elsif type == 'boolean'&& value == 0 || value == "0"
    false
  elsif value == false && type != 'boolean'
    nil
  elsif (type == 'char' || type == 'text') && value == "" && @attributes[skey] == nil
    nil
  else
    value
  end
end

#sanitize_m2o_association(value) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/ooor/type_casting.rb', line 141

def sanitize_m2o_association(value)
  if value.blank? || value == "0"
    false
  else
    value.to_i
  end
end

#to_openerp_hashObject



149
150
151
152
153
154
# File 'lib/ooor/type_casting.rb', line 149

def to_openerp_hash
  attribute_keys, association_keys = get_changed_values
  associations = {}
  association_keys.each { |k| associations[k] = self.cast_association(k) }
  @attributes.slice(*attribute_keys).merge(associations)
end