Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<%= page_with_sidebar do %>
<%= page_with_sidebar_main do %>

<%= render component('ui/panel').new(title: Spree.user_class.model_name.human) do %>
<%= render component('ui/panel').new(title: Spree.admin_user_class.model_name.human) do %>
<%= solidus_form_for @user, url: solidus_admin.user_path(@user), html: { id: form_id, autocomplete: "off" } do |f| %>
<div class="py-1.5">
<%= f.text_field(:email) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SolidusAdmin::Users::Index::Component < SolidusAdmin::UsersAndRoles::Compo
include SolidusAdmin::LastLoginHelper

def model_class
Spree.user_class
Spree.admin_user_class
end

def search_key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def title
def tabs
[
{
text: Spree.user_class.model_name.human(count: 2),
text: Spree.admin_user_class.model_name.human(count: 2),
href: solidus_admin.users_path,
current: model_class == Spree.user_class,
current: model_class == Spree.admin_user_class,
},
{
text: Spree::Role.model_name.human(count: 2),
Expand Down
2 changes: 1 addition & 1 deletion admin/app/controllers/solidus_admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def variants_for
def customers_for
load_order

@users = Spree.user_class
@users = Spree.admin_user_class
.where.not(id: @order.user_id)
.order(created_at: :desc, id: :desc)
.ransack(params[:q])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def set_store_credit
end

def set_user
@user = Spree.user_class.find(params[:user_id])
@user = Spree.admin_user_class.find(params[:user_id])
end

def set_store_credit_reasons
Expand Down
10 changes: 5 additions & 5 deletions admin/app/controllers/solidus_admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UsersController < SolidusAdmin::BaseController

def index
users = apply_search_to(
Spree.user_class.order(created_at: :desc, id: :desc),
Spree.admin_user_class.order(created_at: :desc, id: :desc),
param: :q,
)

Expand Down Expand Up @@ -73,9 +73,9 @@ def edit
end

def destroy
@users = Spree.user_class.where(id: params[:id])
@users = Spree.admin_user_class.where(id: params[:id])

Spree.user_class.transaction { @users.destroy_all }
Spree.admin_user_class.transaction { @users.destroy_all }

flash[:notice] = t('.success')
redirect_back_or_to users_path, status: :see_other
Expand All @@ -84,7 +84,7 @@ def destroy
private

def set_user
@user = Spree.user_class.find(params[:id])
@user = Spree.admin_user_class.find(params[:id])
end

def user_params
Expand Down Expand Up @@ -123,7 +123,7 @@ def set_items
end

def authorization_subject
Spree.user_class
Spree.admin_user_class
end
end
end
12 changes: 6 additions & 6 deletions admin/docs/index_pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class SolidusAdmin::UsersController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
users = apply_search_to(Spree.user_class.order(id: :desc), param: :q)
users = apply_search_to(Spree.admin_user_class.order(id: :desc), param: :q)
# ...
```

For pagination support, the index action should also call the `set_page_and_extract_portion_from` method provided by the `geared_pagination` gem. This method sets the `@page` instance variable to the paginated collection and returns the portion of the collection to be displayed on the current page.

```ruby
def index
users = apply_search_to(Spree.user_class.order(id: :desc), param: :q)
users = apply_search_to(Spree.admin_user_class.order(id: :desc), param: :q)
set_page_and_extract_portion_from(users)
# ...
```
Expand All @@ -33,7 +33,7 @@ Finally, the index action should render the `index` component passing the `@page

```ruby
def index
users = apply_search_to(Spree.user_class.order(id: :desc), param: :q)
users = apply_search_to(Spree.admin_user_class.order(id: :desc), param: :q)
set_page_and_extract_portion_from(users)
render component('users/index').new(page: @page)
end
Expand All @@ -50,7 +50,7 @@ The index component requires only the `page` argument during initialization, all
```ruby
class SolidusAdmin::Users::Index < Solidus::Admin::UI::Pages::Index
def model_class
Spree.user_class
Spree.admin_user_class
end
end

Expand Down Expand Up @@ -92,7 +92,7 @@ end
```ruby
# in the controller
def delete
@users = Spree.user_class.where(id: params[:id])
@users = Spree.admin_user_class.where(id: params[:id])
@users.destroy_all
flash[:notice] = "Admin users deleted"
redirect_to solidus_admin.users_path, status: :see_other
Expand Down Expand Up @@ -124,7 +124,7 @@ module SolidusAdmin
search_scope(:all)

def index
users = apply_search_to(Spree.user_class.order(id: :desc), param: :q)
users = apply_search_to(Spree.admin_user_class.order(id: :desc), param: :q)
# ...
```

Expand Down
1 change: 1 addition & 0 deletions admin/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
Capybara.disable_animation = true
Capybara.default_max_wait_time = ENV['DEFAULT_MAX_WAIT_TIME'].to_f if ENV['DEFAULT_MAX_WAIT_TIME'].present?
Capybara.enable_aria_label = true
Capybara.server = :puma, { Silent: true } # A workaround for https://github.com/rspec/rspec-rails/issues/1897

# DATABASE CLEANER
require 'database_cleaner'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def update
if @order.contents.update_cart(order_params)

if should_associate_user?
requested_user = Spree.user_class.find(params[:user_id])
requested_user = Spree.admin_user_class.find(params[:user_id])
@order.associate_user!(requested_user, @order.email.blank?)
end

Expand Down
2 changes: 1 addition & 1 deletion backend/app/controllers/spree/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def index
end

def new
user = Spree.user_class.find_by(id: params[:user_id]) if params[:user_id]
user = Spree.admin_user_class.find_by(id: params[:user_id]) if params[:user_id]
order_importer_params = order_params
order_importer_params[:bill_address] = user&.bill_address
order_importer_params[:ship_address] = user&.ship_address
Expand Down
4 changes: 2 additions & 2 deletions backend/app/controllers/spree/admin/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class SearchController < Spree::Admin::BaseController
def users
if params[:ids]
# split here may be String#split or Array#split, so we must flatten the results
@users = Spree.user_class.where(id: params[:ids].split(',').flatten)
@users = Spree.admin_user_class.where(id: params[:ids].split(',').flatten)
else
@users = Spree.user_class.ransack({
@users = Spree.admin_user_class.ransack({
m: 'or',
email_start: params[:q],
name_start: params[:q]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Spree
module Admin
class StoreCreditsController < ResourceController
belongs_to 'spree/user', model_class: Spree.user_class
belongs_to 'spree/user', model_class: Spree.admin_user_class
before_action :load_categories, only: [:new]
before_action :load_reasons, only: [:edit_amount, :edit_validity]
before_action :ensure_store_credit_reason, only: [:update_amount, :invalidate]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def destroy
private

def user
@user ||= Spree.user_class.find(params[:user_id])
@user ||= Spree.admin_user_class.find(params[:user_id])
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions backend/app/controllers/spree/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def show
end

def create
@user = Spree.user_class.new(user_params)
@user = Spree.admin_user_class.new(user_params)
if @user.save
set_roles
set_stock_locations
Expand Down Expand Up @@ -82,7 +82,7 @@ def items
end

def model_class
Spree.user_class
Spree.admin_user_class
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def store_credit_event_originator_link(store_credit_event)
private

# Cannot set the value for a user originator
# because Spree.user_class is not defined at that time.
# because Spree.admin_user_class is not defined at that time.
# Spree::UserClassHandle does not work here either as
# the assignment is evaluated before user_class is set
def add_user_originator_link
originator_links[Spree.user_class.to_s] = {
originator_links[Spree.admin_user_class.to_s] = {
new_tab: true,
href_type: :user,
translation_key: 'admin.store_credits.user_originator'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% admin_breadcrumb(link_to plural_resource_name(Spree::LegacyUser), spree.admin_users_path) %>
<% admin_breadcrumb(link_to plural_resource_name(Spree.admin_user_class), spree.admin_users_path) %>
<% admin_breadcrumb(link_to @user.email, edit_admin_user_url(@user)) %>
<% admin_breadcrumb(link_to plural_resource_name(Spree::StoreCredit), spree.admin_user_store_credits_path(@user)) %>
<% admin_breadcrumb(link_to Spree::StoreCredit.model_name.human, admin_user_store_credit_path(@user, @store_credit)) %>
Expand Down
2 changes: 1 addition & 1 deletion backend/app/views/spree/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= render partial: 'spree/admin/users/user_page_actions' %>

<fieldset data-hook="admin_user_edit_general_settings">
<legend><%= Spree.user_class.model_name.human %></legend>
<legend><%= Spree.admin_user_class.model_name.human %></legend>

<div data-hook="admin_user_edit_form_header">
<%= render partial: 'spree/shared/error_messages', locals: { target: @user } %>
Expand Down
12 changes: 6 additions & 6 deletions backend/app/views/spree/admin/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% admin_breadcrumb(plural_resource_name(Spree::LegacyUser)) %>
<% admin_breadcrumb(plural_resource_name(Spree.admin_user_class)) %>

<% content_for :page_actions do %>
<% if can?(:admin, Spree.user_class) && can?(:create, Spree.user_class) %>
<% if can?(:admin, Spree.admin_user_class) && can?(:create, Spree.admin_user_class) %>
<li>
<%= link_to t('spree.new_user'), new_admin_user_url, id: 'admin_new_user_link', class: 'btn btn-primary' %>
</li>
Expand All @@ -24,7 +24,7 @@
</div>
<div class="col-xs-12 col-md-3">
<div class="form-group">
<%= f.label :spree_roles_id_in, Spree.user_class.human_attribute_name(:spree_roles) %>
<%= f.label :spree_roles_id_in, Spree.admin_user_class.human_attribute_name(:spree_roles) %>
<%= f.collection_select :spree_roles_id_in, @roles, :id, :name, {},
multiple: true, class: 'select2 fullwidth' %>
</div>
Expand Down Expand Up @@ -69,10 +69,10 @@
</colgroup>
<thead>
<tr data-hook="admin_users_index_headers">
<th><%= sort_link @search, :email, Spree.user_class.human_attribute_name(:email), {title: 'users_email_title'} %></th>
<th><%= Spree.user_class.human_attribute_name(:spree_roles) %></th>
<th><%= sort_link @search, :email, Spree.admin_user_class.human_attribute_name(:email), {title: 'users_email_title'} %></th>
<th><%= Spree.admin_user_class.human_attribute_name(:spree_roles) %></th>
<th class="align-center"><%= t('spree.num_orders') %></th>
<th class="align-center"><%= Spree.user_class.human_attribute_name(:lifetime_value) %></th>
<th class="align-center"><%= Spree.admin_user_class.human_attribute_name(:lifetime_value) %></th>
<th class="align-center"><%= sort_link @search, :created_at, t('spree.member_since') %></th>
<th data-hook="admin_users_index_header_actions" class="actions"></th>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/spree/backend_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def menu_items
label: :users,
icon: admin_updated_navbar ? 'ri-user-line' : 'user',
match_path: %r{/(users|store_credits)},
condition: -> { Spree.user_class && can?(:admin, Spree.user_class) },
condition: -> { Spree.admin_user_class && can?(:admin, Spree.admin_user_class) },
url: :admin_users_path,
),
MenuItem.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ end
Spree.user_class = <%= options[:user_class].inspect %>
<% end -%>

# By default, admin users share the same class as storefront users.
# Override this if you use a separate model for admin users.
<% if options[:user_class].present? -%>
Spree.admin_user_class = <%= options[:user_class].inspect %>
<% end -%>

# Rules for avoiding to store the current path into session for redirects
# When at least one rule is matched, the request path will not be stored
# in session.
Expand Down
17 changes: 17 additions & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Spree
autoload :Deprecation, 'spree/deprecation'

mattr_accessor :user_class, default: 'Spree::LegacyUser'
mattr_accessor :admin_user_class, default: nil

def self.user_class
if @@user_class.is_a?(Class)
Expand All @@ -43,6 +44,22 @@ def self.user_class_name
@@user_class
end

# The class used for admin/backoffice users.
# Falls back to `Spree.user_class` for backwards compatibility.
def self.admin_user_class
return user_class if @@admin_user_class.nil?

if @@admin_user_class.is_a?(Class)
raise "Spree.admin_user_class MUST be a String or Symbol object, not a Class object."
elsif @@admin_user_class.is_a?(String) || @@admin_user_class.is_a?(Symbol)
@@admin_user_class.to_s.constantize
end
end

def self.admin_user_class_name
@@admin_user_class || @@user_class
end

# Load the same version defaults for all available Solidus components
#
# @see Spree::Preferences::Configuration#load_defaults
Expand Down
8 changes: 7 additions & 1 deletion core/lib/spree/testing_support/capybara_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ module Spree
module TestingSupport
module CapybaraExt
def click_icon(type)
find(".fa-#{type}").click
el = find(".fa-#{type}", visible: :all)
el.click
rescue Selenium::WebDriver::Error::ElementClickInterceptedError
# When a floating element (eg. tooltips/overlays) intercepts the click,
# scroll the target into view and dispatch a click via JS to keep tests stable.
page.execute_script('arguments[0].scrollIntoView({block: "center"});', el.native)
page.execute_script('arguments[0].click();', el.native)
end

def eventually_fill_in(field, options = {})
Expand Down
13 changes: 12 additions & 1 deletion core/lib/spree/testing_support/dummy_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
ENV['RAILS_ENV'] = 'test'
ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] = '1'

# Ensure sqlite3 v2 is supported when running with the default sqlite adapter.
# The `fast_sqlite` gem relaxes ActiveRecord's sqlite3 version constraints.
begin
require 'fast_sqlite'
rescue LoadError
# Gem not available, skip.
end

require 'rails'
require 'active_record/railtie'
require 'action_controller/railtie'
Expand Down Expand Up @@ -106,7 +114,10 @@ class Application < ::Rails::Application

# Set the preview path within the dummy app:
if ActionMailer::Base.respond_to? :preview_paths # Rails 7.1+
config.action_mailer.preview_paths << File.expand_path('dummy_app/mailer_previews', __dir__)
# Some Rails versions return a frozen array here; assign a new array
# to avoid FrozenError when augmenting the paths.
existing = Array(config.action_mailer.preview_paths)
config.action_mailer.preview_paths = existing + [File.expand_path('dummy_app/mailer_previews', __dir__)]
else
config.action_mailer.preview_path = File.expand_path('dummy_app/mailer_previews', __dir__)
end
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.7'
version: "3.7"

services:
mysql:
Expand All @@ -18,12 +18,12 @@ services:
- postgres:/var/lib/postgresql/data:cached

app:
shm_size: '512mb'
shm_size: "512mb"
build:
context: .dockerdev
dockerfile: Dockerfile
args:
RUBY_VERSION: "3.1"
RUBY_VERSION: "3.4.6"
PG_VERSION: 13
NODE_VERSION: 20
MYSQL_VERSION: "8.0"
Expand Down