From 505b7e04e5f97c0c7a8eedc1b31cecc7e5bec8c2 Mon Sep 17 00:00:00 2001 From: dleadbetter Date: Thu, 1 Aug 2024 12:14:11 -0400 Subject: [PATCH] Archnet #1112 - Updating cloud#update method to set content_type; Refactoring set_content_type task --- app/services/triple_eye_effable/cloud.rb | 10 +++++++- lib/tasks/triple_eye_effable_tasks.rake | 31 +++++++----------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/app/services/triple_eye_effable/cloud.rb b/app/services/triple_eye_effable/cloud.rb index 175689c..1605614 100644 --- a/app/services/triple_eye_effable/cloud.rb +++ b/app/services/triple_eye_effable/cloud.rb @@ -56,11 +56,15 @@ def download_resource(resourceable) return if resourceable.nil? || resourceable.resource_description.nil? resource_description = resourceable.resource_description - response = self.class.get("#{base_url}/#{resource_description.resource_id}", headers: headers) + response = get_resource(resource_description.resource_id) parse_response(response) end + def get_resource(resource_id) + self.class.get("#{base_url}/#{resource_id}", headers: headers) + end + def load_description(resource_description) id = resource_description.resource_id @@ -84,6 +88,10 @@ def update_resource(resourceable) response = self.class.put("#{base_url}/#{id}", body: request_body(resourceable), headers: headers) add_error(resourceable, response) and return unless response.success? + resource_id, data = parse_response(response) + resource_description.content_type = data[:content_type] + resource_description.save if resource_description.content_type_changed? + load_description resource_description end diff --git a/lib/tasks/triple_eye_effable_tasks.rake b/lib/tasks/triple_eye_effable_tasks.rake index 302fb5a..26307c2 100644 --- a/lib/tasks/triple_eye_effable_tasks.rake +++ b/lib/tasks/triple_eye_effable_tasks.rake @@ -6,32 +6,19 @@ namespace :triple_eye_effable do task :set_content_type => :environment do service = TripleEyeEffable::Cloud.new(read_only: true) - # Build the list of classes that include the Resourceable concern - classes = [] - - Rails.application.eager_load! if Rails.env.development? - - ActiveRecord::Base.descendants.each do |model| - next unless model.include?(TripleEyeEffable::Resourceable) - classes << model - end + query = TripleEyeEffable::ResourceDescription.where(content_type: nil) - classes.each do |klass| - query = klass - .joins(:resource_description) - .preload(:resource_description) - .where(resource_description: { content_type: nil }) - - query.find_each do |resourceable| - resource_id, data = service.download_resource(resourceable) - next if data.nil? + query.find_in_batches do |resource_descriptions| + content_types = {} - content_type = data[:content_type] - next if content_type.nil? + resource_descriptions.pluck(:id, :resource_id).each do |id, resource_id| + r_id, data = service.get_resource(resource_id) + next unless data.present? - resource_description = resourceable.resource_description - resource_description.update_attribute(:content_type, content_type) + content_types[id] = { content_type: data[:content_type] } end + + TripleEyeEffable::ResourceDescription.update(content_types.keys, content_types.values) end end