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.

Added ability to attach user avatars for admin users

Damian Legawieca86bc107b1c4
diff --git a/admin/app/helpers/spree/admin/base_helper.rb b/admin/app/helpers/spree/admin/base_helper.rb
index 8e81947a65..1af3ac6543 100644
--- a/admin/app/helpers/spree/admin/base_helper.rb
+++ b/admin/app/helpers/spree/admin/base_helper.rb
@@ -1,10 +1,24 @@
module Spree
module Admin
module BaseHelper
+ include Spree::ImagesHelper
+
def available_countries_iso
@available_countries_iso ||= current_store.countries_available_for_checkout.pluck(:iso)
end
+ def avatar_url_for(user)
+ return unless user.present?
+
+ 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]))
+ )
+ else
+ "https://eu.ui-avatars.com/api/?name=#{user.name&.initials}&background=random"
+ end
+ end
+
def display_on_options
Spree::DisplayOn::DISPLAY.map do |display_on|
[Spree.t("display_on_options.#{display_on}"), display_on]
diff --git a/admin/spec/helpers/spree/admin/base_helper_spec.rb b/admin/spec/helpers/spree/admin/base_helper_spec.rb
new file mode 100644
index 0000000000..8e9bb0b2e8
--- /dev/null
+++ b/admin/spec/helpers/spree/admin/base_helper_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe Spree::Admin::BaseHelper do
+ describe '#avatar_url_for' do
+ let(:user) { create(:admin_user) }
+
+ context 'when user has an avatar' do
+ before { user.avatar.attach(io: File.new(Spree::Core::Engine.root + 'spec/fixtures/thinking-cat.jpg'), filename: 'thinking-cat.jpg') }
+
+ 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/)
+ 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/)
+ end
+ end
+ end
+end
diff --git a/core/app/models/concerns/spree/user_methods.rb b/core/app/models/concerns/spree/user_methods.rb
index b76f0b0293..5158a19431 100644
--- a/core/app/models/concerns/spree/user_methods.rb
+++ b/core/app/models/concerns/spree/user_methods.rb
@@ -31,6 +31,11 @@ module Spree
has_many :wishlists, class_name: 'Spree::Wishlist', foreign_key: :user_id
+ #
+ # Attachments
+ #
+ has_one_attached :avatar, service: Spree.public_storage_service_name
+
self.whitelisted_ransackable_associations = %w[bill_address ship_address addresses tags]
self.whitelisted_ransackable_attributes = %w[id email]
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index af6d2539fe..f7560cad2c 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -149,9 +149,8 @@ module Spree
:taxonomy_id, :meta_description, :meta_keywords, :meta_title, :child_index
]
- # TODO: Should probably use something like Spree.user_class.attributes
@@user_attributes = [:email, :bill_address_id, :ship_address_id, :password, :first_name, :last_name,
- :password_confirmation, :selected_locale,
+ :password_confirmation, :selected_locale, :avatar,
{ public_metadata: {}, private_metadata: {}, tag_list: [] }]
@@variant_attributes = [