Skip to content

Commit

Permalink
Merge pull request #15 from performant-software/feature/archnet1112_e…
Browse files Browse the repository at this point in the history
…mpty_attachments

Archnet #1112 - Empty Attachments
  • Loading branch information
dleadbetter authored Aug 1, 2024
2 parents e36a09b + 505b7e0 commit 4a2a6b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
10 changes: 9 additions & 1 deletion app/services/triple_eye_effable/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
31 changes: 9 additions & 22 deletions lib/tasks/triple_eye_effable_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4a2a6b4

Please sign in to comment.