Module: Ooor::Persistence::ClassMethods

Defined in:
lib/ooor/persistence.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes = {}, default_get_list = false, reload = true, &block) ⇒ Object

Creates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.

The attributes parameter can be either a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.

the default_get_list parameter differs from the ActiveRecord API it is used to tell OpenERP the list of fields for which we want the default values false will request all default values while [] will not ask for any default value (faster) reload can be set to false to indicate you don’t want to reload the record after it is saved which will save a roundtrip to OpenERP and perform faster.



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

def create(attributes = {}, default_get_list = false, reload = true, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr, &block) }
  else
    object = new(attributes, default_get_list, &block)
    object.save(reload)
    object
  end
end

#create!(attributes = {}, default_get_list = false, reload = true, &block) ⇒ Object

Creates an object just like Base.create but calls save! instead of save so an exception is raised if the record is invalid.



66
67
68
69
70
71
72
73
74
75
# File 'lib/ooor/persistence.rb', line 66

def create!(attributes = {}, default_get_list = false, reload = true, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create!(attr, &block) }
  else
    object = new(attributes, default_get_list)
    yield(object) if block_given?
    object.save!(reload)
    object
  end
end