motion-prime/models/sync.rb in motion-prime-0.3.2 vs motion-prime/models/sync.rb in motion-prime-0.3.3
- old
+ new
@@ -36,10 +36,13 @@
sync_options[:fetch_associations]
else # do not need to fetch unless this is a GET request
should_fetch
end
+ should_fetch = persisted? if should_fetch.nil?
+ should_update ||= new_record? unless should_fetch
+
method = sync_options[:method]
method ||= if should_update
persisted? ? :put : :post
else
:get
@@ -49,13 +52,10 @@
if url.blank?
should_fetch = false
should_update = false
end
- should_fetch = !new_record? if should_fetch.nil?
- should_update ||= new_record? unless should_fetch
-
fetch_with_url url do |data, status_code|
save if sync_options[:save]
block.call(data, status_code) if use_callback
end if should_fetch
@@ -153,10 +153,10 @@
old_collection = self.send(key)
use_callback = block_given?
puts "SYNC: started sync for #{key} in #{self.class_name_without_kvo}"
api_client.get normalize_sync_url(options[:sync_url]) do |response, status_code|
- data = options.has_key?(:sync_key) ? response[options[:sync_key]] : response
+ data = options[:sync_key] && response ? response[options[:sync_key]] : response
if data
# Update/Create existing records
data.each do |attributes|
model = old_collection.detect{ |model| model.id == attributes[:id]}
unless model
\ No newline at end of file