module ActiveZuora::Persistence::ClassMethods

Public Instance Methods

create(attributes={}) click to toggle source
# File lib/active_zuora/persistence.rb, line 74
def create(attributes={})
  new(attributes).tap(&:save)
end
create!(attributes={}) click to toggle source
# File lib/active_zuora/persistence.rb, line 78
def create!(attributes={})
  new(attributes).tap(&:save!)
end
delete(*ids) click to toggle source
# File lib/active_zuora/persistence.rb, line 146
def delete(*ids)
  ids.flatten!
  deleted_records = 0
  ids.each_slice(MAX_BATCH_SIZE) do |batch|
    deleted_records += process_delete(batch)
  end
  deleted_records
end
process_delete(*ids) click to toggle source
# File lib/active_zuora/persistence.rb, line 155
def process_delete(*ids)
  ids.flatten!
  results = connection.request(:delete) do |soap|
    qualifier = soap.namespace_by_uri(soap.namespace)
    soap.body do |xml|
      xml.tag!(qualifier, :type, zuora_object_name)
      ids.map { |id| xml.tag!(qualifier, :ids, id) }.last
    end
  end[:delete_response][:result]
  results = [results] unless results.is_a?(Array)
  # Return the count of deletes that succeeded.
  results.select{ |result| result[:success] }.size
end
process_save(zobjects, action) click to toggle source
# File lib/active_zuora/persistence.rb, line 114
def process_save(zobjects, action)
  unless [:create, :update].include? action
    raise "Invalid action type for saving. Must be create or update." 
  end

  return 0 if zobjects.empty?

  results = connection.request(action) do |soap|
    soap.body do |xml|
      zobjects.map do |zobject|
        zobject.build_xml(xml, soap,
          :namespace => soap.namespace,
          :element_name => :zObjects,
          :force_type => true,
          :nil_strategy => :fields_to_null)
      end.last
    end
  end["#{action.to_s}_response".to_sym][:result]

  results = [results] unless results.is_a?(Array)
  zobjects.zip(results).each do |zobject, result|
    if result[:success]
      zobject.clear_changed_attributes
    else
      zobject.add_zuora_errors result[:errors]
    end
  end

  # Return the count of updates that succeeded.
  results.select{ |result| result[:success] }.size
end
save(*zobjects) click to toggle source

Takes an array of zobjects and batch saves new and updated records separately

# File lib/active_zuora/persistence.rb, line 83
def save(*zobjects)
  new_records = 0
  updated_records = 0

  # Get all new objects
  new_objects = zobjects.flatten.select do |zobject|
    zobject.new_record? && zobject.changed.present? && zobject.valid?
  end

  # Get all updated objects
  updated_objects = zobjects.flatten.select do |zobject|
    !zobject.new_record? && zobject.changed.present? && zobject.valid?
  end

  # Make calls in batches of 50
  new_objects.each_slice(MAX_BATCH_SIZE) do |batch|
    new_records += process_save(batch, :create)
  end

  updated_objects.each_slice(MAX_BATCH_SIZE) do |batch|
    updated_records += process_save(batch, :update)
  end

  new_records + updated_records
end
update(*zobjects) click to toggle source

For backwards compatability

# File lib/active_zuora/persistence.rb, line 110
def update(*zobjects)
  save(zobjects)
end