Module: Ooor::Associations

Included in:
Base
Defined in:
lib/ooor/associations.rb

Instance Method Summary collapse

Instance Method Details

#load_association(model_key, ids, substitute = nil, *arguments) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ooor/associations.rb', line 46

def load_association(model_key, ids, substitute=nil, *arguments)
  options = arguments.extract_options!
  related_class = self.class.const_get(model_key)
  fields = options[:fields] || options[:only] || nil
  context = options[:context] || object_session
  (related_class.send(:find, ids, fields: fields, context: context) || substitute).tap do |r|
    #TODO the following is a hack to minimally mimic the CollectionProxy of Rails 3.1+; this should probably be re-implemented
    def r.association=(association)
      @association = association
    end
    r.association = related_class
    def r.build(attrs={})
      @association.new(attrs)
    end
  end
end

#many2one_id_method(rel, *arguments) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/ooor/associations.rb', line 4

def many2one_id_method(rel, *arguments)
  if @associations[rel]
    @associations[rel][0]
  else
    obj = method_missing(rel.to_sym, *arguments)
    obj.is_a?(Base) ? obj.id : obj
  end
end

#relationnal_result(method_name, *arguments) ⇒ Object

fakes associations like much like ActiveRecord according to the cached OpenERP data model



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ooor/associations.rb', line 22

def relationnal_result(method_name, *arguments)
  self.class.reload_fields_definition(false, object_session)
  if self.class.many2one_associations.has_key?(method_name)
    if @associations[method_name]
      rel = self.class.many2one_associations[method_name]['relation']
      id = @associations[method_name].is_a?(Integer) ? @associations[method_name] : @associations[method_name][0]
      load_association(rel, id, nil, *arguments)
    else
      false
    end
  elsif self.class.one2many_associations.has_key?(method_name)
    rel = self.class.one2many_associations[method_name]['relation']
    load_association(rel, @associations[method_name], [], *arguments)
  elsif self.class.many2many_associations.has_key?(method_name)
    rel = self.class.many2many_associations[method_name]['relation']
    load_association(rel, @associations[method_name], [], *arguments)
  elsif self.class.polymorphic_m2o_associations.has_key?(method_name)
    values = @associations[method_name].split(',')
    load_association(values[0], values[1].to_i, nil, *arguments)
  else
    false
  end
end

#x_to_many_ids_method(rel, *arguments) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ooor/associations.rb', line 13

def x_to_many_ids_method(rel, *arguments)
  if @associations[rel]
    @associations[rel]
  else
    method_missing(rel.to_sym, *arguments)
  end
end