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, v) ⇒ Object

talk OpenERP cryptic associations API



176
177
178
179
180
181
182
183
184
# File 'lib/ooor/type_casting.rb', line 176

def cast_association(k, v)
  if self.class.one2many_associations[k]
    cast_o2m_assocation(v)
  elsif self.class.many2many_associations[k]
    [[6, false, (v || []).map {|i| i.is_a?(Base) ? i.id : i}]]
  elsif self.class.many2one_associations[k]
    cast_m2o_association(v)
  end
end

#cast_associations_to_openerp(associations = @associations) ⇒ Object



169
170
171
172
173
# File 'lib/ooor/type_casting.rb', line 169

def cast_associations_to_openerp(associations=@associations)
  associations.each do |k, v|
    associations[k] = self.cast_association(k, v)
  end
end

#cast_m2o_association(v) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/ooor/type_casting.rb', line 215

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_assocation(v) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ooor/type_casting.rb', line 186

def cast_o2m_assocation(v)
  if v.is_a?(Hash)
    cast_o2m_nested_attributes(v)
  else
    v.collect do |value|
      if value.is_a?(Base)
        [0, 0, value.to_openerp_hash]
      elsif value.is_a?(Hash)
        [0, 0, value]
      else
        [1, value, {}]
      end
    end
  end
end

#cast_o2m_nested_attributes(v) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ooor/type_casting.rb', line 202

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ooor/type_casting.rb', line 153

def get_changed_values
  attributes = {}
  associations = {}

  changed.each do |k|
    if self.class.associations_keys.index(k)
      associations[k] = @associations[k]#changes[k][1]
    elsif self.class.fields.has_key?(k)
      attributes[k]= @attributes[k]
    elsif !BLACKLIST.index(k)
      attributes[k] = changes[k][1]
    end
  end
  return attributes, associations
end

#sanitize_associatio_as_string(skey, value) ⇒ Object



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

def sanitize_associatio_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_association(skey, value) ⇒ Object



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

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_associatio_as_string(skey, value)
  else
    value
  end
end

#sanitize_attribute(skey, value) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# 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 and type != 'boolean'
    nil
  else
    value
  end
end

#sanitize_m2o_association(value) ⇒ Object



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

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

#to_openerp_hashObject



147
148
149
150
151
# File 'lib/ooor/type_casting.rb', line 147

def to_openerp_hash
  attributes, associations = get_changed_values
  associations = cast_associations_to_openerp(associations)
  attributes.merge(associations)
end