Skip to content

Commit

Permalink
fixing rspec and cleaned some codes
Browse files Browse the repository at this point in the history
  • Loading branch information
IMPATIENT89 committed Jan 2, 2024
1 parent d90b68a commit e316506
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 29 deletions.
6 changes: 3 additions & 3 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/letters/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 19 additions & 23 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e316506

Please sign in to comment.