Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Unify rendering user avatars in admin, don't rely on any 3rd party APIs (#12597)

Damian Legawiec8aca01a9c8f9
diff --git a/admin/app/assets/stylesheets/spree/admin/shared/_base.scss b/admin/app/assets/stylesheets/spree/admin/shared/_base.scss
index 92e6c1bfbb..11bcaf034d 100644
--- a/admin/app/assets/stylesheets/spree/admin/shared/_base.scss
+++ b/admin/app/assets/stylesheets/spree/admin/shared/_base.scss
@@ -671,4 +671,15 @@ div.uppy-Root {
.tooltip-inner {
box-shadow: $dropdown-box-shadow;
-}
\ No newline at end of file
+}
+
+.avatar {
+ border-radius: 50%;
+ background-color: $gray-200;
+ color: $gray-500;
+ font-size: 1rem;
+ font-weight: 500;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+}
diff --git a/admin/app/helpers/spree/admin/base_helper.rb b/admin/app/helpers/spree/admin/base_helper.rb
index 9c167cdd2d..6c3788f889 100644
--- a/admin/app/helpers/spree/admin/base_helper.rb
+++ b/admin/app/helpers/spree/admin/base_helper.rb
@@ -28,15 +28,23 @@ module Spree
@available_countries_iso ||= current_store.countries_available_for_checkout.pluck(:iso)
end
- def avatar_url_for(user)
+ def render_avatar(user, options = {})
return unless user.present?
+ options[:width] ||= 128
+ options[:height] ||= 128
+ options[:class] ||= 'avatar'
+
if user.respond_to?(:avatar) && user.avatar.attached? && user.avatar.variable?
- main_app.cdn_image_url(
- user.avatar.variant(spree_image_variant_options(resize_to_fill: [128, 128]))
+ image_tag(
+ main_app.cdn_image_url(
+ user.avatar.variant(spree_image_variant_options(resize_to_fill: [options[:width] * 2, options[:height] * 2]))
+ ),
+ class: options[:class],
+ style: "width: #{options[:width]}px; height: #{options[:height]}px;"
)
else
- "https://eu.ui-avatars.com/api/?name=#{user.name&.initials}&background=random"
+ content_tag(:div, user.name&.initials, class: options[:class], style: "width: #{options[:width]}px; height: #{options[:height]}px;")
end
end
diff --git a/admin/app/helpers/spree/admin/stores_helper.rb b/admin/app/helpers/spree/admin/stores_helper.rb
index e891db3af8..a29d3dbe71 100644
--- a/admin/app/helpers/spree/admin/stores_helper.rb
+++ b/admin/app/helpers/spree/admin/stores_helper.rb
@@ -77,8 +77,9 @@ module Spree
)
else
initials = store.name.split.map(&:first).join.upcase
- image_tag("https://eu.ui-avatars.com/api/?name=#{initials}&background=random", width: opts[:height], height: opts[:height],
- class: 'rounded with-tip', title: store.name)
+ content_tag(:div, initials, class: "avatar rounded with-tip bg-light",
+ style: "width: #{opts[:height]}px; height: #{opts[:height]}px;",
+ title: store.name)
end
end
end
diff --git a/admin/app/views/spree/admin/orders/_customer_summary.html.erb b/admin/app/views/spree/admin/orders/_customer_summary.html.erb
index 028ee72c3b..79fc43f83f 100644
--- a/admin/app/views/spree/admin/orders/_customer_summary.html.erb
+++ b/admin/app/views/spree/admin/orders/_customer_summary.html.erb
@@ -1,8 +1,6 @@
<% if order %>
<% if order.user %>
<span class="d-flex">
- <%#= image_tag(avatar_url_for( order.user), width: 24, height: 24, class: 'mr-2 rounded') %>
-
<% if can?(:manage, order.user) %>
<%= link_to order.full_name, spree.admin_user_path(order.user), class: 'text-dark', data: { 'turbo-frame': '_top' } %>
<% else %>
diff --git a/admin/app/views/spree/admin/shared/_user.html.erb b/admin/app/views/spree/admin/shared/_user.html.erb
index 9763ea3efd..8562b1e59b 100644
--- a/admin/app/views/spree/admin/shared/_user.html.erb
+++ b/admin/app/views/spree/admin/shared/_user.html.erb
@@ -1,8 +1,8 @@
<%= link_to spree.admin_user_path(user), class: 'rounded hover-light p-2 d-flex justify-content-start align-items-center', data: {turbo_frame: :_top} do %>
<% if user.name.present? %>
- <%= image_tag(avatar_url_for(user), width: 48, height: 48, class: 'mr-3 rounded-lg') %>
+ <%= render_avatar(user, width: 48, height: 48, class: 'mr-3 avatar') %>
<% else %>
- <div style="width: 48px; height: 48px" class="mr-3 rounded-lg bg-light"></div>
+ <div style="width: 48px; height: 48px" class="mr-3 avatar bg-light"></div>
<% end %>
<div class="text-left">
<% if customer_full_name(user) %>
diff --git a/admin/app/views/spree/admin/shared/_user_dropdown.html.erb b/admin/app/views/spree/admin/shared/_user_dropdown.html.erb
index 8888f0d915..9d8c0e59b5 100644
--- a/admin/app/views/spree/admin/shared/_user_dropdown.html.erb
+++ b/admin/app/views/spree/admin/shared/_user_dropdown.html.erb
@@ -4,8 +4,8 @@
class="btn d-flex align-items-center justify-content-between text-left w-100"
data-toggle="dropdown" aria-expanded="false" id="accountNav">
<div class="d-flex align-items-center">
- <%= image_tag(avatar_url_for(try_spree_current_user), width: 34, height: 34, class: 'rounded-circle mr-lg-2') %>
- <span class="d-none d-lg-inline">
+ <%= render_avatar(try_spree_current_user, width: 34, height: 34) %>
+ <span class="d-none d-lg-inline ml-lg-2">
<%= try_spree_current_user.name&.full&.truncate(15)&.presence || try_spree_current_user.email.truncate(15) %><br />
<span class="text-muted small"><%= try_spree_current_user.email.truncate(22) %></span>
</span>
diff --git a/admin/spec/helpers/spree/admin/base_helper_spec.rb b/admin/spec/helpers/spree/admin/base_helper_spec.rb
index c9129d5f8a..3b0ac1875a 100644
--- a/admin/spec/helpers/spree/admin/base_helper_spec.rb
+++ b/admin/spec/helpers/spree/admin/base_helper_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
describe Spree::Admin::BaseHelper do
- describe '#avatar_url_for' do
+ describe '#render_avatar' do
let(:user) { create(:admin_user) }
context 'when user has an avatar' do
@@ -9,14 +9,14 @@ describe Spree::Admin::BaseHelper do
it 'returns the avatar url' do
ActiveStorage::Current.url_options = { host: 'localhost', port: 3000 }
- expect(avatar_url_for(user)).to match(/rails\/active_storage/)
- expect(avatar_url_for(user)).to match(/thinking-cat\.jpg/)
+ expect(render_avatar(user)).to match(/rails\/active_storage/)
+ expect(render_avatar(user)).to match(/thinking-cat\.jpg/)
end
end
context 'when user does not have an avatar' do
it 'returns initials' do
- expect(avatar_url_for(user)).to match(/eu\.ui-avatars\.com/)
+ expect(render_avatar(user)).to match(/avatar/)
end
end
end