Skip to content

Commit

Permalink
updating rake task and rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
IMPATIENT89 committed Jan 9, 2024
1 parent e316506 commit d454693
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
12 changes: 7 additions & 5 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ def get_profile_image(user)
private

def image_for_handle(user)
if user.twitter_identity.present?
url_for(user.twitter_identity.profile_image) if user.twitter_identity.profile_image.attached?
elsif user.github_identity.present?
url_for(user.github_identity.profile_image) if user.github_identity.profile_image.attached?
if user.twitter_identity.present? && user.twitter_identity.profile_image.attached?
url_for(user.twitter_identity.profile_image)
elsif user.github_identity.present? && user.github_identity.profile_image.attached?
url_for(user.github_identity.profile_image)
else
'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png'
end
end
end
end
39 changes: 29 additions & 10 deletions lib/tasks/images.rake
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
namespace :images do
desc 'Load images from Cloudinary and store in Active Storage'
task load_images: :environment do
User.where('updated_at < ?', '2023-08-26').each do |user|
cloudinary_url = "https://res.cloudinary.com/whyloveruby/image/twitter_name/#{user.twitter_identity.handle}.jpg"
saved_count = 0
error_count = 0
batch_count = 0
User.where('updated_at < ?', '2023-08-26').find_in_batches(batch_size: 20) do |batch|
batch.each do |user|
cloudinary_url = "https://res.cloudinary.com/whyloveruby/image/twitter_name/#{user.twitter_identity.handle}.jpg"
begin
downloaded_image = URI.parse(cloudinary_url).open
downloaded_image.rewind

user.twitter_identity.profile_image.attach(io: downloaded_image, filename: "#{user.twitter_identity.handle}.jpg")

downloaded_image = URI.parse(cloudinary_url).open

downloaded_image.rewind

user.twitter_identity.profile_image.attach(io: downloaded_image, filename: "#{user.twitter_identity.handle}.jpg")

puts "Image for #{user.twitter_identity.name} loaded and attached."
if user.twitter_identity.profile_image.attached?
saved_count += 1
else
error_count += 1
puts "\nError saving profile image for user #{user.twitter_identity.name}:"
puts user.twitter_identity.errors_full_message.to_sentence
end
rescue StandardError => e
# Handling the exception
error_count += 1
puts "\nError downloading image for user #{user.id}: #{e.message}"
end
end
batch_count += 1
puts "\nBatch #{batch_count} completed..."
puts "#{error_count} errors..."
puts "#{saved_count} saved..."
end
puts "\n\nProcess Completed"
end
end

4 changes: 2 additions & 2 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

context 'get_profile_image for User' do
it 'should render image tag with the twitter handle from active storage' do
user = User.create(updated_at: Date.parse('2022-12-31').end_of_day)
user = User.create(name: "Test")
user.external_identities.create(handle: "my_twitter_handle", provider: "twitter", uid: "252152")

image_path = Rails.root.join('app', 'assets', 'images', 'rails.png')
Expand All @@ -28,7 +28,7 @@
expect(generated_url).to match("profile_image.*my_twitter_handle\.png")
end
it 'should render image tag with the github handle from active storage' do
user = User.create(updated_at: Date.parse('2022-12-31').end_of_day)
user = User.create(name: "Test")
user.external_identities.create(handle: "my_github_handle", provider: "github", uid: "252150")

image_path = Rails.root.join('app', 'assets', 'images', 'rails.png')
Expand Down

0 comments on commit d454693

Please sign in to comment.