From e316506a70558bb978820b955918033ea423a89d Mon Sep 17 00:00:00 2001 From: IMPATIENT89 Date: Tue, 2 Jan 2024 14:44:33 +0530 Subject: [PATCH] fixing rspec and cleaned some codes --- app/helpers/application_helper.rb | 6 ++-- app/views/home/index.html.haml | 2 +- app/views/layouts/application.html.haml | 2 +- app/views/letters/show.html.haml | 2 +- spec/helpers/application_helper_spec.rb | 42 +++++++++++-------------- 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 290de7e..2326221 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,12 +1,12 @@ module ApplicationHelper - def get_twitter_image(user) - image_tag image_for_twitter_handle(user), onerror: 'this.error=null;this.src="https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"' + def get_profile_image(user) + image_tag image_for_handle(user), onerror: 'this.error=null;this.src="https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"' end private - def image_for_twitter_handle(user) + 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? diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index f4e6e49..1ae49bc 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -46,7 +46,7 @@ = time_ago_in_words letter.created_at ago .pull-right.thumbnail.imageWrapper{ style: 'max-height: 50px; max-width: 50px' } - = get_twitter_image(letter.user) + = get_profile_image(letter.user) -# TODO refactor this diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 46cb078..a762951 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -116,7 +116,7 @@ %tr %td .thumbnail.pull-left{ style: 'max-height: 50px; max-width: 50px' } - = get_twitter_image(l.user) + = get_profile_image(l.user) .info.pull-left - if l.user.twitter_identity.present? - twitter_identity = l.user.twitter_identity diff --git a/app/views/letters/show.html.haml b/app/views/letters/show.html.haml index 4fd8211..ceb5ba9 100644 --- a/app/views/letters/show.html.haml +++ b/app/views/letters/show.html.haml @@ -29,7 +29,7 @@ = time_ago_in_words @letter.created_at ago .pull-right.thumbnail.imageWrapper{ style: 'max-height: 50px; max-width: 50px' } - = get_twitter_image(@letter.user) + = get_profile_image(@letter.user) = markdown(@letter.description) %br %div diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 05ce58c..097dafb 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -15,32 +15,28 @@ # actual context: We realized just in time before RubyConfIndia2023 that due to Twitter API changes # we are unable to retrieve profile pictures for new twitter sign_ups through Cloudinary - context 'get_twitter_image for User who last signed in before RubyIndiaConf 2023' do - it 'should render image tag with the twitter handle' do + 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.external_identities.create(handle: "my_twitter_handle", provider: "twitter", uid: "252152") - expect(helper.get_twitter_image(user)).to match("cloudinary.*my_twitter_handle\.jpg") - end - end - context 'get_twitter_image for User who last signed in after RubyIndiaConf 2023' do - it 'should render image tag with twitter handle (in the unlikely case, our User model does not contain the image url)' do - user = User.create(updated_at: Date.parse('2024-01-01').end_of_day) - user.external_identities.create(handle: "my_twitter_handle", provider: "twitter", uid: "252152") - expect(helper.get_twitter_image(user)).to match("cloudinary.*my_twitter_handle\.jpg") + + image_path = Rails.root.join('app', 'assets', 'images', 'rails.png') + user.twitter_identity.profile_image.attach(io: File.open(image_path), filename: 'my_twitter_handle.png') + + generated_url = helper.get_profile_image(user) + + expect(generated_url).to match("profile_image.*my_twitter_handle\.png") end - it 'should render image tag using the URL saved on the ExternalIdentity model' do - user = User.create( - updated_at: Date.parse('2024-01-01').end_of_day - ) - user.external_identities.create( - handle: 'my_twitter_handle', - provider: 'twitter', - uid: '123456', - image: 'https://pbs.twimg.com/profile_images/123456789/my_selfie.jpg' - ) - image_url = helper.get_twitter_image(user) - expect(image_url).to_not match("my_twitter_handle\.jpg") - expect(image_url).to match("my_selfie\.jpg") + 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.external_identities.create(handle: "my_github_handle", provider: "github", uid: "252150") + + image_path = Rails.root.join('app', 'assets', 'images', 'rails.png') + user.github_identity.profile_image.attach(io: File.open(image_path), filename: 'my_github_handle.png') + + generated_url = helper.get_profile_image(user) + + expect(generated_url).to match("profile_image.*my_github_handle\.png") end end end