From 23d4547e2bb6072bfbf07d4c41e980cc8f1a58da Mon Sep 17 00:00:00 2001 From: Matt Farnsworth Date: Mon, 14 Dec 2015 13:04:46 +0200 Subject: [PATCH] Story #105846070 - update existing record if present --- app/models/business_registry_cache.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/models/business_registry_cache.rb b/app/models/business_registry_cache.rb index 5d6010674..5defd4183 100644 --- a/app/models/business_registry_cache.rb +++ b/app/models/business_registry_cache.rb @@ -48,11 +48,16 @@ class BusinessRegistryCache < ActiveRecord::Base cache = BusinessRegistryCache.find_by(ident: ident_code, ident_country_code: ident_cc) # fetch new data if cache is expired return cache if cache.present? && cache.retrieved_on > (Time.zone.now - Setting.days_to_keep_business_registry_cache.days) - cache = [] # expired data is forbidden - data = business_registry.associated_businesses(ident_code, ident_cc) - unless data.nil? - cache = BusinessRegistryCache.new(data) + businesses = business_registry.associated_businesses(ident_code, ident_cc) + unless businesses.nil? + if cache.blank? + cache = BusinessRegistryCache.new(businesses) + else + cache.update businesses + end cache.save + else + cache = [] # expired data is forbidden end cache end