From 9b3c12fb954e8b0b0179f83b5e21437410cf6df7 Mon Sep 17 00:00:00 2001 From: Madeline Collier Date: Mon, 14 Oct 2024 16:26:58 +0200 Subject: [PATCH] Extract last_login into helper and component This DRYs things up a bit so that all pages of the users admin can reuse the same helper, translations, and component. --- .../users/addresses/component.html.erb | 12 +------ .../users/addresses/component.rb | 11 ------- .../users/addresses/component.yml | 4 --- .../users/edit/component.html.erb | 12 +------ .../solidus_admin/users/edit/component.rb | 10 ------ .../solidus_admin/users/edit/component.yml | 4 --- .../solidus_admin/users/index/component.rb | 18 ++--------- .../solidus_admin/users/index/component.yml | 4 --- .../users/last_login/component.html.erb | 11 +++++++ .../users/last_login/component.rb | 9 ++++++ .../users/last_login/component.yml | 2 ++ .../users/orders/component.html.erb | 12 +------ .../solidus_admin/users/orders/component.rb | 10 ------ .../solidus_admin/users/orders/component.yml | 4 --- .../solidus_admin/last_login_helper.rb | 16 ++++++++++ admin/config/locales/users.en.yml | 4 +++ .../solidus_admin/last_login_helper_spec.rb | 31 +++++++++++++++++++ 17 files changed, 78 insertions(+), 96 deletions(-) create mode 100644 admin/app/components/solidus_admin/users/last_login/component.html.erb create mode 100644 admin/app/components/solidus_admin/users/last_login/component.rb create mode 100644 admin/app/components/solidus_admin/users/last_login/component.yml create mode 100644 admin/app/helpers/solidus_admin/last_login_helper.rb create mode 100644 admin/spec/helpers/solidus_admin/last_login_helper_spec.rb diff --git a/admin/app/components/solidus_admin/users/addresses/component.html.erb b/admin/app/components/solidus_admin/users/addresses/component.html.erb index b2d41f37e3c..2f4be025456 100644 --- a/admin/app/components/solidus_admin/users/addresses/component.html.erb +++ b/admin/app/components/solidus_admin/users/addresses/component.html.erb @@ -40,17 +40,7 @@ <% end %> <%= page_with_sidebar_aside do %> - <%= render component("ui/panel").new(title: t("spree.lifetime_stats")) do %> - <%= render component("ui/details_list").new( - items: [ - { label: t("spree.total_sales"), value: @user.display_lifetime_value.to_html }, - { label: t("spree.order_count"), value: @user.order_count.to_i }, - { label: t("spree.average_order_value"), value: @user.display_average_order_value.to_html }, - { label: t("spree.member_since"), value: @user.created_at.to_date }, - { label: t(".last_active"), value: last_login(@user) }, - ] - ) %> - <% end %> + <%= render component("users/last_login").new(user: @user) %> <% end %> <% end %> <% end %> diff --git a/admin/app/components/solidus_admin/users/addresses/component.rb b/admin/app/components/solidus_admin/users/addresses/component.rb index 6116312b866..0432aefba80 100644 --- a/admin/app/components/solidus_admin/users/addresses/component.rb +++ b/admin/app/components/solidus_admin/users/addresses/component.rb @@ -43,17 +43,6 @@ def tabs ] end - def last_login(user) - return t('.last_login.never') if user.try(:last_sign_in_at).blank? - - t( - '.last_login.login_time_ago', - # @note The second `.try` is here for the specs and for setups that use a - # custom User class which may not have this attribute. - last_login_time: time_ago_in_words(user.try(:last_sign_in_at)) - ).capitalize - end - def bill_address(address, type) if address.present? && type == "bill" address diff --git a/admin/app/components/solidus_admin/users/addresses/component.yml b/admin/app/components/solidus_admin/users/addresses/component.yml index 6872c1d5cd5..dfeaebe49a2 100644 --- a/admin/app/components/solidus_admin/users/addresses/component.yml +++ b/admin/app/components/solidus_admin/users/addresses/component.yml @@ -6,10 +6,6 @@ en: items: Items store_credit: Store Credit last_active: Last Active - last_login: - login_time_ago: "%{last_login_time} ago" - never: Never - invitation_sent: Invitation sent create_order_for_user: Create order for this user update: Update cancel: Cancel diff --git a/admin/app/components/solidus_admin/users/edit/component.html.erb b/admin/app/components/solidus_admin/users/edit/component.html.erb index b9b8cbe4488..c2eebab753e 100644 --- a/admin/app/components/solidus_admin/users/edit/component.html.erb +++ b/admin/app/components/solidus_admin/users/edit/component.html.erb @@ -46,17 +46,7 @@ <% end %> <%= page_with_sidebar_aside do %> - <%= render component("ui/panel").new(title: t("spree.lifetime_stats")) do %> - <%= render component("ui/details_list").new( - items: [ - { label: t("spree.total_sales"), value: @user.display_lifetime_value.to_html }, - { label: t("spree.order_count"), value: @user.order_count.to_i }, - { label: t("spree.average_order_value"), value: @user.display_average_order_value.to_html }, - { label: t("spree.member_since"), value: @user.created_at.to_date }, - { label: t(".last_active"), value: last_login(@user) }, - ] - ) %> - <% end %> + <%= render component("users/last_login").new(user: @user) %> <% end %> <% end %> <% end %> diff --git a/admin/app/components/solidus_admin/users/edit/component.rb b/admin/app/components/solidus_admin/users/edit/component.rb index 19ff9adbc8e..738764c5f4a 100644 --- a/admin/app/components/solidus_admin/users/edit/component.rb +++ b/admin/app/components/solidus_admin/users/edit/component.rb @@ -43,16 +43,6 @@ def tabs ] end - def last_login(user) - return t('.last_login.never') if user.try(:last_sign_in_at).blank? - - t( - '.last_login.login_time_ago', - # @note The second `.try` is only here for the specs to work. - last_login_time: time_ago_in_words(user.try(:last_sign_in_at)) - ).capitalize - end - def role_options Spree::Role.all.map do |role| { label: role.name, id: role.id } diff --git a/admin/app/components/solidus_admin/users/edit/component.yml b/admin/app/components/solidus_admin/users/edit/component.yml index 01b3cf230ac..4aa3d1056f1 100644 --- a/admin/app/components/solidus_admin/users/edit/component.yml +++ b/admin/app/components/solidus_admin/users/edit/component.yml @@ -6,10 +6,6 @@ en: items: Items store_credit: Store Credit last_active: Last Active - last_login: - login_time_ago: "%{last_login_time} ago" - never: Never - invitation_sent: Invitation sent create_order_for_user: Create order for this user update: Update cancel: Cancel diff --git a/admin/app/components/solidus_admin/users/index/component.rb b/admin/app/components/solidus_admin/users/index/component.rb index 0b911061d90..2d6db499bc9 100644 --- a/admin/app/components/solidus_admin/users/index/component.rb +++ b/admin/app/components/solidus_admin/users/index/component.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class SolidusAdmin::Users::Index::Component < SolidusAdmin::UsersAndRoles::Component + include SolidusAdmin::LastLoginHelper + def model_class Spree.user_class end @@ -93,20 +95,4 @@ def columns }, ] end - - private - - # @todo add logic to display "Invitation sent" when the user has not yet - # accepted the invitation and filled out account details. To be implemented - # in conjunction with the invitation logic. - def last_login(user) - return t('.last_login.never') if user.try(:last_sign_in_at).blank? - - t( - '.last_login.login_time_ago', - # @note The second `.try` is here for the specs and for setups that use a - # custom User class which may not have this attribute. - last_login_time: time_ago_in_words(user.try(:last_sign_in_at)) - ).capitalize - end end diff --git a/admin/app/components/solidus_admin/users/index/component.yml b/admin/app/components/solidus_admin/users/index/component.yml index 789fbb6a3cf..ba63d2d41f6 100644 --- a/admin/app/components/solidus_admin/users/index/component.yml +++ b/admin/app/components/solidus_admin/users/index/component.yml @@ -13,7 +13,3 @@ en: status: active: Active inactive: Inactive - last_login: - login_time_ago: "%{last_login_time} ago" - never: Never - invitation_sent: Invitation sent diff --git a/admin/app/components/solidus_admin/users/last_login/component.html.erb b/admin/app/components/solidus_admin/users/last_login/component.html.erb new file mode 100644 index 00000000000..888473b400b --- /dev/null +++ b/admin/app/components/solidus_admin/users/last_login/component.html.erb @@ -0,0 +1,11 @@ +<%= render component("ui/panel").new(title: t("spree.lifetime_stats")) do %> + <%= render component("ui/details_list").new( + items: [ + { label: t("spree.total_sales"), value: @user.display_lifetime_value.to_html }, + { label: t("spree.order_count"), value: @user.order_count.to_i }, + { label: t("spree.average_order_value"), value: @user.display_average_order_value.to_html }, + { label: t("spree.member_since"), value: @user.created_at.to_date }, + { label: t(".last_active"), value: last_login(@user) }, + ] + ) %> +<% end %> diff --git a/admin/app/components/solidus_admin/users/last_login/component.rb b/admin/app/components/solidus_admin/users/last_login/component.rb new file mode 100644 index 00000000000..cd51dcfc3e9 --- /dev/null +++ b/admin/app/components/solidus_admin/users/last_login/component.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class SolidusAdmin::Users::LastLogin::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::LastLoginHelper + + def initialize(user:) + @user = user + end +end diff --git a/admin/app/components/solidus_admin/users/last_login/component.yml b/admin/app/components/solidus_admin/users/last_login/component.yml new file mode 100644 index 00000000000..f01f52d773e --- /dev/null +++ b/admin/app/components/solidus_admin/users/last_login/component.yml @@ -0,0 +1,2 @@ +en: + last_active: Last Active diff --git a/admin/app/components/solidus_admin/users/orders/component.html.erb b/admin/app/components/solidus_admin/users/orders/component.html.erb index a386d089b84..21e6b518fdc 100644 --- a/admin/app/components/solidus_admin/users/orders/component.html.erb +++ b/admin/app/components/solidus_admin/users/orders/component.html.erb @@ -36,17 +36,7 @@ <% end %> <%= page_with_sidebar_aside do %> - <%= render component("ui/panel").new(title: t("spree.lifetime_stats")) do %> - <%= render component("ui/details_list").new( - items: [ - { label: t("spree.total_sales"), value: @user.display_lifetime_value.to_html }, - { label: t("spree.order_count"), value: @user.order_count.to_i }, - { label: t("spree.average_order_value"), value: @user.display_average_order_value.to_html }, - { label: t("spree.member_since"), value: @user.created_at.to_date }, - { label: t(".last_active"), value: last_login(@user) }, - ] - ) %> - <% end %> + <%= render component("users/last_login").new(user: @user) %> <% end %> <% end %> <% end %> diff --git a/admin/app/components/solidus_admin/users/orders/component.rb b/admin/app/components/solidus_admin/users/orders/component.rb index ee57928111b..917156abfca 100644 --- a/admin/app/components/solidus_admin/users/orders/component.rb +++ b/admin/app/components/solidus_admin/users/orders/component.rb @@ -42,16 +42,6 @@ def tabs ] end - def last_login(user) - return t('.last_login.never') if user.try(:last_sign_in_at).blank? - - t( - '.last_login.login_time_ago', - # @note The second `.try` is only here for the specs to work. - last_login_time: time_ago_in_words(user.try(:last_sign_in_at)) - ).capitalize - end - def model_class Spree::Order end diff --git a/admin/app/components/solidus_admin/users/orders/component.yml b/admin/app/components/solidus_admin/users/orders/component.yml index 6c4b6f1368a..98c4724fe23 100644 --- a/admin/app/components/solidus_admin/users/orders/component.yml +++ b/admin/app/components/solidus_admin/users/orders/component.yml @@ -6,10 +6,6 @@ en: items: Items store_credit: Store Credit last_active: Last Active - last_login: - login_time_ago: "%{last_login_time} ago" - never: Never - invitation_sent: Invitation sent create_order_for_user: Create order for this user no_orders_found: No Orders found. create_one: Create One diff --git a/admin/app/helpers/solidus_admin/last_login_helper.rb b/admin/app/helpers/solidus_admin/last_login_helper.rb new file mode 100644 index 00000000000..95f49b00c00 --- /dev/null +++ b/admin/app/helpers/solidus_admin/last_login_helper.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module SolidusAdmin + module LastLoginHelper + def last_login(user) + return t('solidus_admin.users.last_login.never') if user.try(:last_sign_in_at).blank? + + t( + 'solidus_admin.users.last_login.login_time_ago', + # @note The second `.try` is here for the specs and for setups that use a + # custom User class which may not have this attribute. + last_login_time: time_ago_in_words(user.try(:last_sign_in_at)) + ).capitalize + end + end +end diff --git a/admin/config/locales/users.en.yml b/admin/config/locales/users.en.yml index 5fda686929c..8a08a99a8aa 100644 --- a/admin/config/locales/users.en.yml +++ b/admin/config/locales/users.en.yml @@ -10,3 +10,7 @@ en: ship: success: "Shipping Address has been successfully updated." error: "Address could not be updated." + last_login: + never: "Never" + login_time_ago: "%{last_login_time} ago" + invitation_sent: "Invitation sent" diff --git a/admin/spec/helpers/solidus_admin/last_login_helper_spec.rb b/admin/spec/helpers/solidus_admin/last_login_helper_spec.rb new file mode 100644 index 00000000000..9b2099f6973 --- /dev/null +++ b/admin/spec/helpers/solidus_admin/last_login_helper_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe SolidusAdmin::LastLoginHelper, type: :helper do + describe "#last_login" do + let(:user) { double("User") } + + context "when user has never logged in" do + it "returns 'Never'" do + allow(user).to receive(:last_sign_in_at).and_return(nil) + + expect(helper.last_login(user)).to eq("Never") + end + end + + context "when user has logged in before" do + it "returns the time ago since the last login, capitalized" do + last_sign_in_time = 2.days.ago + allow(user).to receive(:last_sign_in_at).and_return(last_sign_in_time) + + expect(helper) + .to receive(:time_ago_in_words) + .with(last_sign_in_time) + .and_return("2 days") + + expect(helper.last_login(user)).to eq("2 days ago") + end + end + end +end