lib/active_remote/persistence.rb in active_remote-2.4.0 vs lib/active_remote/persistence.rb in active_remote-3.0.0.pre1

- old
+ new

@@ -47,12 +47,13 @@ end # Instantiate a record with the given remote attributes. Generally used # when retrieving records that already exist, so @new_record is set to false. # - def instantiate(attributes, options = {}) - new_object = self.new.instantiate(attributes) + def instantiate(new_attributes, options = {}) + attributes = self.build_from_rpc(new_attributes) + new_object = self.allocate.init_with(attributes) new_object.readonly! if options[:readonly] new_object end @@ -76,11 +77,11 @@ # def delete raise ReadOnlyRemoteRecord if readonly? response = rpc.execute(:delete, scope_key_hash) - add_errors_from_response(response) + add_errors(response.errors) if response.respond_to?(:errors) return success? ? freeze : false end # Deletes the record from the service (the service determines if the @@ -101,11 +102,11 @@ # def destroy raise ReadOnlyRemoteRecord if readonly? response = rpc.execute(:destroy, scope_key_hash) - add_errors_from_response(response) + add_errors(response.errors) if response.respond_to?(:errors) return success? ? freeze : false end # Destroys (hard deletes) the record from the service and freezes this @@ -125,23 +126,13 @@ end # Instantiate a record with the given remote attributes. Generally used # when retrieving records that already exist, so @new_record is set to false. # - def instantiate(record) - skip_dirty_tracking do - assign_attributes(record, :without_protection => true) - end - - # TODO: figure out how to safely run after_find/search callbacks here - # currently, several functions use this code path, so an alternate path - # may need to be added - - run_callbacks :initialize - - @new_record = false - self + def instantiate(new_attributes) + new_attributes = self.class.build_from_rpc(new_attributes) + init_with(new_attributes) end # Returns true if the remote record hasn't been saved yet; otherwise, # returns false. # @@ -250,11 +241,11 @@ new_attributes.delete(primary_key.to_s) response = rpc.execute(:create, new_attributes) assign_attributes(response.to_hash) - add_errors_from_response(response) + add_errors(response.errors) if response.respond_to?(:errors) @new_record = has_errors? success? end end @@ -280,10 +271,10 @@ updated_attributes.merge!(scope_key_hash) response = rpc.execute(:update, updated_attributes) assign_attributes(response.to_hash) - add_errors_from_response(response) + add_errors(response.errors) if response.respond_to?(:errors) success? end end end