Module: Ooor::Persistence

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Included in:
Base
Defined in:
lib/ooor/persistence.rb

Overview

Ooor Persistence

Note that at the moment it also includes the Validations stuff as it is quite superficial in Ooor Most of the time, when we talk about validation here we talk about extra Rails validations as OpenERP validations will happen anyhow when persisting records to OpenERP. some of the methods found in ActiveRecord Persistence which are identical in ActiveResource may be found in the Ooor::MiniActiveResource module instead

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

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

OpenERP copy method, load persisted copied Object



225
226
227
# File 'lib/ooor/persistence.rb', line 225

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

#deleteObject

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

no callbacks are executed.

To enforce the object’s before_destroy and after_destroy callbacks or any :dependent association options, use #destroy.



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

def delete
  rpc_execute('unlink', [id], context) if persisted?
  @destroyed = true
  freeze
end

#destroyObject

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

There’s a series of callbacks associated with destroy. If the before_destroy callback return false the action is cancelled and destroy returns false. See ActiveRecord::Callbacks for further details.



169
170
171
172
173
174
175
# File 'lib/ooor/persistence.rb', line 169

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

#destroy!Object

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

There’s a series of callbacks associated with destroy!. If the before_destroy callback return false the action is cancelled and destroy! raises ActiveRecord::RecordNotDestroyed. See ActiveRecord::Callbacks for further details.



184
185
186
# File 'lib/ooor/persistence.rb', line 184

def destroy! #TODO
  destroy || raise(ActiveRecord::RecordNotDestroyed)
end

#destroyed?Boolean

Returns true if this object has been destroyed, otherwise returns false.

Returns:

  • (Boolean)


80
81
82
# File 'lib/ooor/persistence.rb', line 80

def destroyed?
  @destroyed
end

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

takes care of reading OpenERP default field values.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ooor/persistence.rb', line 99

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

#load(attributes) ⇒ Object

Flushes the current object and loads the attributes Hash containing the attributes and the associations into the current object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ooor/persistence.rb', line 86

def load(attributes)
  self.class.reload_fields_definition(false)
  raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
  @associations ||= {}
  @attributes ||= {}
  @loaded_associations = {}
  attributes.each do |key, value|
    self.send "#{key}=", value if self.respond_to?("#{key}=")
  end
  self
end

#save(options = {}) ⇒ Object

Saves the model.

If the model is new a record gets created in OpenERP, otherwise the existing record gets updated.

By default, save always run validations. If any of them fail the action is cancelled and save returns false. However, if you supply validate: false, validations are bypassed altogether. In Ooor however, real validations always happen on the OpenERP side so the only validations you can bypass or not are extra pre-validations in Ruby if you have any.

There’s a series of callbacks associated with save. If any of the before_* callbacks return false the action is cancelled and save returns false. See ActiveRecord::Callbacks for further details.

Attributes marked as readonly are silently ignored if the record is being updated. (TODO)



137
138
139
# File 'lib/ooor/persistence.rb', line 137

def save(options = {})
  perform_validations(options) ? save_without_raising(options) : false
end

#save!(options = {}) ⇒ Object

Attempts to save the record just like save but will raise a RecordInvalid exception instead of returning false if the record is not valid.



143
144
145
# File 'lib/ooor/persistence.rb', line 143

def save!(options = {})
  perform_validations(options) ? save_without_raising(options) : raise(RecordInvalid.new(self))
end

#update(attributes, reload = true) ⇒ Object Also known as: update_attributes

Updates the attributes of the model from the passed-in hash and saves the record, all wrapped in a transaction. If the object is invalid, the saving will fail and false will be returned.



210
211
212
# File 'lib/ooor/persistence.rb', line 210

def update(attributes, reload=true)
  load(attributes) && save(reload)
end

#update!(attributes, reload = true) ⇒ Object Also known as: update_attributes!

Updates its receiver just like update but calls save! instead of save, so an exception is raised if the record is invalid.



218
219
220
# File 'lib/ooor/persistence.rb', line 218

def update!(attributes, reload=true)
  load(attributes) && save!(reload)
end

#update_attribute(name, value) ⇒ Object

Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Also note that

  • Validation is skipped.

  • Callbacks are invoked.

  • updated_at/updated_on column is updated if that column is available.

  • Updates all the attributes that are dirty in this object.

This method raises an ActiveRecord::ActiveRecordError if the attribute is marked as readonly.

See also update_column.



202
203
204
205
# File 'lib/ooor/persistence.rb', line 202

def update_attribute(name, value)
  send("#{name}=", value)
  save(validate: false)
end

#valid?(context = nil) ⇒ Boolean Also known as: validate

Runs all the validations within the specified context. Returns true if no errors are found, false otherwise.

Aliased as validate.

If the argument is false (default is nil), the context is set to :create if new_record? is true, and to :update if it is not.

Validations with no :on option will run no matter the context. Validations with some :on option will only run in the specified context.

Returns:

  • (Boolean)


239
240
241
242
243
# File 'lib/ooor/persistence.rb', line 239

def valid?(context = nil)
  context ||= (new_record? ? :create : :update)
  output = super(context)
  errors.empty? && output
end