Skip to content

Commit

Permalink
Add the ability to use a gravatar for the profile image
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaelCodes committed Jan 4, 2024
1 parent 1443bbc commit 4f1c775
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
4 changes: 4 additions & 0 deletions app/helpers/profiles_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

# Helper methods for managing Profiles
module ProfilesHelper
def profile_picture(email)
"https://gravatar.com/avatar/#{Digest::SHA2.hexdigest(email.downcase.strip)}" \
"?s=80&d=retro&r=pg"
end
end
2 changes: 2 additions & 0 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Profile < ApplicationRecord
# Relationships
belongs_to :user

delegate :email, to: :user

has_many :event_attendees, dependent: :destroy
has_many :events, through: :event_attendees
# Friendship has a buddy_id and a friend_id (These are both profiles)
Expand Down
1 change: 1 addition & 0 deletions app/views/profiles/_profile.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
json.extract! profile, :id, :name, :handle, :bio, :created_at, :updated_at
json.url profile_url(profile, format: :json)
json.profile_picture_url profile_picture(profile.email)
41 changes: 28 additions & 13 deletions app/views/profiles/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<div class="container">
<div class="block">
<h1 class="title"><%= @profile.name %></h1>
<h2 class="subtitle"><%= "@#{@profile.handle}" %></h2>
<%= friendship_button(@profile) %>
</div>
<div class="block">
<p><%= @profile.bio %></p>
</div>
<% if @profile.events.present? %>
<div class="section">
<div class="container">
<div class="media is-flex-wrap-wrap mb-3" style="row-gap:1em">
<figure class="media-left image">
<img src="<%= profile_picture(@profile.email) %>" alt="Profile pic">
</figure>
<div class="media-content">
<p class="has-text-weight-bold is-size-4"><%= @profile.name %></p>
<p class="has-text-weight-semi-bold is-size-6"><%= "@#{@profile.handle}" %></p>
</div>
<div class="media-right">
<%= friendship_button(@profile) %>
</div>
</div>
<div class="block">
<%= render 'events/list', events: @events %>
<p><%= @profile.bio %></p>
</div>
<% end %>
<div>
</div>
</div>
<% if @profile.events.present? %>
<div class="section">
<div class="container">
<div class="block">
<%= render 'events/list', events: @events %>
</div>
</div>
</div>
<% end %>
<div class="section">
<div class="container">
<%= buttons(@profile, include_nav: true) %>
</div>
</div>
20 changes: 9 additions & 11 deletions spec/helpers/profiles_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

require "rails_helper"

# Specs in this file have access to a helper object that includes
# the ProfilesHelper. For example:
#
# describe ProfilesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe ProfilesHelper do
pending "add some examples to (or delete) #{__FILE__}"
describe "#profile_picture" do
subject { helper.profile_picture(email) }

let(:email) { " [email protected] " }

it "chomps, lowercases, and hashes the email" do
is_expected.to eq "https://gravatar.com/avatar/973dfe463ec85785f5f95af5ba3906eedb2d931c24e69824a89ea65dba4e813b?s=80&d=retro&r=pg"
end
end
end

0 comments on commit 4f1c775

Please sign in to comment.