Skip to content

Commit e19e1a7

Browse files
authored
Merge pull request #6282 from chaimann/admin-extract-moveable
[Admin] Extract `#move` action into concern
2 parents 388b3e4 + c057d37 commit e19e1a7

File tree

9 files changed

+113
-51
lines changed

9 files changed

+113
-51
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusAdmin::Moveable
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
before_action :load_moveable, only: [:move]
8+
end
9+
10+
def move
11+
@moveable.insert_at(params.require(:position).to_i)
12+
13+
respond_to do |format|
14+
format.js { head :no_content }
15+
end
16+
end
17+
18+
private
19+
20+
def load_moveable
21+
@moveable = moveable_class.find(params.require(:id))
22+
authorize! action_name, @moveable
23+
end
24+
25+
def moveable_class
26+
"Spree::#{self.class.name.demodulize.remove('Controller').singularize}".constantize
27+
rescue NameError
28+
raise NameError,
29+
"could not infer model class from #{self.class.name}. Please override `moveable_class` to specify it explicitly."
30+
end
31+
end

admin/app/controllers/solidus_admin/option_types_controller.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
module SolidusAdmin
44
class OptionTypesController < SolidusAdmin::BaseController
55
include SolidusAdmin::ControllerHelpers::Search
6-
7-
before_action :load_option_type, only: [:move]
6+
include SolidusAdmin::Moveable
87

98
def index
109
option_types = apply_search_to(
@@ -19,14 +18,6 @@ def index
1918
end
2019
end
2120

22-
def move
23-
@option_type.insert_at(params[:position].to_i)
24-
25-
respond_to do |format|
26-
format.js { head :no_content }
27-
end
28-
end
29-
3021
def destroy
3122
@option_types = Spree::OptionType.where(id: params[:id])
3223

@@ -35,12 +26,5 @@ def destroy
3526
flash[:notice] = t('.success')
3627
redirect_back_or_to option_types_path, status: :see_other
3728
end
38-
39-
private
40-
41-
def load_option_type
42-
@option_type = Spree::OptionType.find(params[:id])
43-
authorize! action_name, @option_type
44-
end
4529
end
4630
end

admin/app/controllers/solidus_admin/payment_methods_controller.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
module SolidusAdmin
44
class PaymentMethodsController < SolidusAdmin::BaseController
55
include SolidusAdmin::ControllerHelpers::Search
6-
7-
before_action :load_payment_method, only: [:move]
6+
include SolidusAdmin::Moveable
87

98
search_scope(:all)
109
search_scope(:active, default: true, &:active)
@@ -25,14 +24,6 @@ def index
2524
end
2625
end
2726

28-
def move
29-
@payment_method.insert_at(params[:position].to_i)
30-
31-
respond_to do |format|
32-
format.js { head :no_content }
33-
end
34-
end
35-
3627
def destroy
3728
@payment_methods = Spree::PaymentMethod.where(id: params[:id])
3829

@@ -41,12 +32,5 @@ def destroy
4132
flash[:notice] = t('.success')
4233
redirect_back_or_to payment_methods_path, status: :see_other
4334
end
44-
45-
private
46-
47-
def load_payment_method
48-
@payment_method = Spree::PaymentMethod.find_by!(id: params[:id])
49-
authorize! action_name, @payment_method
50-
end
5135
end
5236
end

admin/app/controllers/solidus_admin/taxonomies_controller.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
module SolidusAdmin
44
class TaxonomiesController < SolidusAdmin::BaseController
55
include SolidusAdmin::ControllerHelpers::Search
6-
7-
before_action :load_taxonomy, only: [:move]
6+
include SolidusAdmin::Moveable
87

98
def index
109
taxonomies = apply_search_to(
@@ -19,14 +18,6 @@ def index
1918
end
2019
end
2120

22-
def move
23-
@taxonomy.insert_at(params[:position].to_i)
24-
25-
respond_to do |format|
26-
format.js { head :no_content }
27-
end
28-
end
29-
3021
def destroy
3122
@taxonomies = Spree::Taxonomy.where(id: params[:id])
3223

@@ -35,12 +26,5 @@ def destroy
3526
flash[:notice] = t('.success')
3627
redirect_back_or_to taxonomies_path, status: :see_other
3728
end
38-
39-
private
40-
41-
def load_taxonomy
42-
@taxonomy = Spree::Taxonomy.find(params[:id])
43-
authorize! action_name, @taxonomy
44-
end
4529
end
4630
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_examples_for "requests: moveable" do
4+
let(:admin_user) { create(:admin_user) }
5+
let(:record) { create(factory, position: 1) }
6+
let(:request_path) do
7+
solidus_admin.send("move_#{record.model_name.singular_route_key}_path", record, format: :js)
8+
end
9+
10+
before do
11+
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
12+
end
13+
14+
describe "PATCH /move" do
15+
it "updates record's position" do
16+
expect { patch request_path, params: { position: 2 } }.to change { record.reload.position }.from(1).to(2)
17+
expect(response).to have_http_status(:no_content)
18+
end
19+
end
20+
end
21+
22+
RSpec.shared_examples_for "features: sortable" do
23+
let(:factory_attrs) { {} }
24+
let(:scope) { "body" }
25+
26+
before do
27+
create(factory, displayed_attribute => "First", position: 1, **factory_attrs)
28+
create(factory, displayed_attribute => "Second", position: 2, **factory_attrs)
29+
visit path
30+
end
31+
32+
it "allows sorting via drag and drop" do
33+
within(scope) do
34+
expect(find("[data-controller='sortable']").all(:xpath, "./*").first).to have_text("First")
35+
expect(find("[data-controller='sortable']").all(:xpath, "./*").last).to have_text("Second")
36+
37+
rows = find("[data-controller='sortable']").all(:xpath, "./*")
38+
rows[1].drag_to rows[0]
39+
40+
expect(find("[data-controller='sortable']").all(:xpath, "./*").first).to have_text("Second")
41+
expect(find("[data-controller='sortable']").all(:xpath, "./*").last).to have_text("First")
42+
end
43+
end
44+
end

admin/spec/features/payment_methods_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4+
require "solidus_admin/testing_support/shared_examples/moveable"
45

56
describe "Payment Methods", :js, type: :feature do
67
before { sign_in create(:admin_user, email: 'admin@example.com') }
@@ -45,4 +46,10 @@
4546
expect(page).not_to have_content("Check")
4647
expect(Spree::PaymentMethod.count).to eq(3)
4748
end
49+
50+
it_behaves_like "features: sortable" do
51+
let(:factory) { :payment_method }
52+
let(:displayed_attribute) { :name }
53+
let(:path) { solidus_admin.payment_methods_path }
54+
end
4855
end

admin/spec/features/taxonomies_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4+
require "solidus_admin/testing_support/shared_examples/moveable"
45

56
describe "Taxonomies", :js, type: :feature do
67
before { sign_in create(:admin_user, email: 'admin@example.com') }
@@ -21,4 +22,10 @@
2122
expect(page).not_to have_content("Categories")
2223
expect(Spree::Taxonomy.count).to eq(1)
2324
end
25+
26+
it_behaves_like "features: sortable" do
27+
let(:factory) { :taxonomy }
28+
let(:displayed_attribute) { :name }
29+
let(:path) { solidus_admin.taxonomies_path }
30+
end
2431
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
require "solidus_admin/testing_support/shared_examples/moveable"
5+
6+
RSpec.describe "SolidusAdmin::PaymentMethodsController", type: :request do
7+
it_behaves_like "requests: moveable" do
8+
let(:factory) { :payment_method }
9+
let(:request_path) { solidus_admin.move_payment_method_path(record, format: :js) }
10+
end
11+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
require "solidus_admin/testing_support/shared_examples/moveable"
5+
6+
RSpec.describe "SolidusAdmin::TaxonomiesController", type: :request do
7+
it_behaves_like "requests: moveable" do
8+
let(:factory) { :taxonomy }
9+
end
10+
end

0 commit comments

Comments
 (0)