Module: ActiveZuora::Persistence::ClassMethods
- Defined in:
- lib/active_zuora/persistence.rb
Instance Method Summary collapse
- #create(attributes = {}) ⇒ Object
- #create!(attributes = {}) ⇒ Object
- #delete(*ids) ⇒ Object
- #process_delete(*ids) ⇒ Object
- #process_save(zobjects, action) ⇒ Object
-
#save(*zobjects) ⇒ Object
Takes an array of zobjects and batch saves new and updated records separately.
-
#update(*zobjects) ⇒ Object
For backwards compatability.
Instance Method Details
#create(attributes = {}) ⇒ Object
74 75 76 |
# File 'lib/active_zuora/persistence.rb', line 74 def create(attributes={}) new(attributes).tap(&:save) end |
#create!(attributes = {}) ⇒ Object
78 79 80 |
# File 'lib/active_zuora/persistence.rb', line 78 def create!(attributes={}) new(attributes).tap(&:save!) end |
#delete(*ids) ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/active_zuora/persistence.rb', line 150 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) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/active_zuora/persistence.rb', line 159 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) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# 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_nil) end.last end end["#{action.to_s}_response".to_sym][:result] results = [results] unless results.is_a?(Array) zobjects.each_with_index do |zobject, i| # If it's an update, grab by id, otherwise by index if action == :update result = results.find { |r| r[:id] == zobject.id } || { :errors => { :message => "No result returned." } } else result = results[i] || { :errors => { :message => "No result returned." } } end 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) ⇒ Object
Takes an array of zobjects and batch saves new and updated records separately
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
For backwards compatability
110 111 112 |
# File 'lib/active_zuora/persistence.rb', line 110 def update(*zobjects) save(zobjects) end |