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