Module: Ooor::Persistence

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

Overview

the base class for proxies to OpenERP objects

Instance Method Summary collapse

Instance Method Details

#copy(defaults = {}, context = {}) ⇒ Object

OpenERP copy method, load persisted copied Object



111
112
113
# File 'lib/ooor/persistence.rb', line 111

def copy(defaults={}, context={})
  self.class.find(rpc_execute('copy', self.id, defaults, context), context: context)
end

#create(context = {}, reload = true) ⇒ Object

Create (i.e., save to OpenERP service) the new resource.



66
67
68
# File 'lib/ooor/persistence.rb', line 66

def create(context={}, reload=true)
  create_or_update(context, reload)
end

#create_or_update(context = {}, reload = true) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/ooor/persistence.rb', line 56

def create_or_update(context={}, reload=true)
  run_callbacks :save do
    new? ? create_record(context, reload) : update_record(context, reload)
  end
rescue ValidationError => e
  e.extract_validation_error!(errors)
  return false
end

#create_record(context = {}, reload = true) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ooor/persistence.rb', line 70

def create_record(context={}, reload=true)
  run_callbacks :create do
    self.id = rpc_execute('create', to_openerp_hash, context)
    if @ir_model_data_id
      IrModelData.create(model: self.class.openerp_model,
        'module' => @ir_model_data_id[0],
        'name' => @ir_model_data_id[1],
        'res_id' => self.id)
    end
    @persisted = true
    reload_fields(context) if reload
  end
end

#destroy(context = {}) ⇒ Object

Deletes the record in OpenERP and freezes this instance to reflect that no changes should be made (since they can’t be persisted).



102
103
104
105
106
107
108
# File 'lib/ooor/persistence.rb', line 102

def destroy(context={})
  run_callbacks :destroy do
    rpc_execute('unlink', [self.id], context)
    @destroyed = true
    freeze 
  end
end

#initialize(attributes = {}, default_get_list = false, context = {}, persisted = false) ⇒ Object

takes care of reading OpenERP default field values.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ooor/persistence.rb', line 30

def initialize(attributes = {}, default_get_list=false, context={}, persisted=false)
  @attributes = {}
  @prefix_options = {}
  @ir_model_data_id = attributes.delete(:ir_model_data_id)
  @object_session = {}
  @object_session = HashWithIndifferentAccess.new(context)
  @persisted = persisted
  self.class.reload_fields_definition(false, @object_session)
  if default_get_list == []
    load(attributes)
  else
    load_with_defaults(attributes, default_get_list)
  end.tap do
    if id
      @previously_changed = ActiveSupport::HashWithIndifferentAccess.new # see ActiveModel::Dirty reset_changes
      @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
    end
  end
end

#load(attributes, remove_root = false, persisted = false) ⇒ Object

an attribute might actually be a association too, will be determined here

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ooor/persistence.rb', line 16

def load(attributes, remove_root=false, persisted=false)#an attribute might actually be a association too, will be determined here
  self.class.reload_fields_definition(false, object_session)
  raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
  @prefix_options, attributes = split_options(attributes)
  @associations ||= {}
  @attributes ||= {}
  @loaded_associations = {}
  attributes.each do |key, value|
    self.send "#{key}=".to_sym, value if self.respond_to?("#{key}=".to_sym)
  end
  self
end

#save(context = {}, reload = true) ⇒ Object

Saves (create) or updates (write) a resource. Delegates to create if the object is new, update if it exists.



52
53
54
# File 'lib/ooor/persistence.rb', line 52

def save(context={}, reload=true)
  create_or_update(context, reload)
end

#update(context = {}, reload = true, keys = nil) ⇒ Object

Update the resource on the remote service.



89
90
91
# File 'lib/ooor/persistence.rb', line 89

def update(context={}, reload=true, keys=nil)
  create_or_update(context, reload, keys)
end

#update_attributes(attributes, context = {}, reload = true) ⇒ Object



84
85
86
# File 'lib/ooor/persistence.rb', line 84

def update_attributes(attributes, context={}, reload=true)
  load(attributes, false) && save(context, reload)
end

#update_record(context = {}, reload = true) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/ooor/persistence.rb', line 93

def update_record(context={}, reload=true)
  run_callbacks :update do
    rpc_execute('write', [self.id], to_openerp_hash, context)
    reload_fields(context) if reload
    @persisted = true
  end
end