Skip to content

Commit

Permalink
Move store_credits actions to their own controller
Browse files Browse the repository at this point in the history
The admin store credit flow is complicated enough and has enough of its
own actions that it should really be in its own controller rather than
squished into the users controller.
  • Loading branch information
MadelineCollier committed Dec 4, 2024
1 parent 81c605c commit 07a2d3a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
21 changes: 21 additions & 0 deletions admin/app/controllers/solidus_admin/store_credits_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module SolidusAdmin
class StoreCreditsController < SolidusAdmin::BaseController
before_action :set_user

def index
@store_credits = Spree::StoreCredit.where(user_id: @user.id).order(id: :desc)

respond_to do |format|
format.html { render component("users/store_credits/index").new(user: @user, store_credits: @store_credits) }
end
end

private

def set_user
@user = Spree.user_class.find(params[:id])
end
end
end
10 changes: 1 addition & 9 deletions admin/app/controllers/solidus_admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UsersController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search
include Spree::Core::ControllerHelpers::StrongParameters

before_action :set_user, only: [:edit, :addresses, :update_addresses, :orders, :items, :store_credits]
before_action :set_user, only: [:edit, :addresses, :update_addresses, :orders, :items]

search_scope(:all, default: true)
search_scope(:customers) { _1.left_outer_joins(:role_users).where(role_users: { id: nil }) }
Expand Down Expand Up @@ -81,14 +81,6 @@ def destroy
redirect_back_or_to users_path, status: :see_other
end

def store_credits
@store_credits = Spree::StoreCredit.where(user_id: @user.id).order(id: :desc)

respond_to do |format|
format.html { render component("users/store_credits/index").new(user: @user, store_credits: @store_credits) }
end
end

private

def set_user
Expand Down
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
put :update_addresses
get :orders
get :items
get :store_credits
get :store_credits, controller: "store_credits", action: "index"
end
end

Expand Down
21 changes: 21 additions & 0 deletions admin/spec/requests/solidus_admin/store_credits_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe SolidusAdmin::StoreCreditsController, type: :request do
let(:admin_user) { create(:admin_user) }
let(:user) { create(:user) }
let!(:store_credit) { create(:store_credit, user: user) }

before do
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
end

describe "GET /store_credits" do
it "renders the store credits template with a 200 OK status" do
get solidus_admin.store_credits_user_path(user)
expect(response).to have_http_status(:ok)
expect(response.body).to include(store_credit.amount.to_s)
end
end
end
7 changes: 0 additions & 7 deletions admin/spec/requests/solidus_admin/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@
end
end

describe "GET /store_credits" do
it "renders the store credits template with a 200 OK status" do
get solidus_admin.store_credits_user_path(user)
expect(response).to have_http_status(:ok)
end
end

describe "GET /addresses" do
it "renders the addresses template with a 200 OK status" do
get solidus_admin.addresses_user_path(user)
Expand Down

0 comments on commit 07a2d3a

Please sign in to comment.