Skip to content

Commit 568f4ed

Browse files
Merge pull request #5219 from mamhoff/resource-controller-for-foreign-engines
ResourceController: Allow overriding routes proxy
2 parents 05439dd + 3957b5e commit 568f4ed

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

backend/app/controllers/spree/admin/resource_controller.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def parent
193193
.find_by!(self.class.parent_data[:find_by] => params["#{parent_model_name}_id"])
194194
instance_variable_set("@#{parent_model_name}", @parent)
195195
rescue ActiveRecord::RecordNotFound => e
196-
resource_not_found(flash_class: e.model.constantize, redirect_url: spree.polymorphic_url([:admin, parent_model_name.pluralize.to_sym]))
196+
resource_not_found(flash_class: e.model.constantize, redirect_url: routes_proxy.polymorphic_url([:admin, parent_model_name.pluralize.to_sym]))
197197
end
198198

199199
def parent?
@@ -238,38 +238,42 @@ def location_after_save
238238

239239
def new_object_url(options = {})
240240
if parent?
241-
spree.new_polymorphic_url([:admin, parent, model_class], options)
241+
routes_proxy.new_polymorphic_url([:admin, parent, model_class], options)
242242
else
243-
spree.new_polymorphic_url([:admin, model_class], options)
243+
routes_proxy.new_polymorphic_url([:admin, model_class], options)
244244
end
245245
end
246246

247247
def edit_object_url(object, options = {})
248248
if parent?
249-
spree.polymorphic_url([:edit, :admin, parent, object], options)
249+
routes_proxy.polymorphic_url([:edit, :admin, parent, object], options)
250250
else
251-
spree.polymorphic_url([:edit, :admin, object], options)
251+
routes_proxy.polymorphic_url([:edit, :admin, object], options)
252252
end
253253
end
254254

255255
def object_url(object = nil, options = {})
256256
target = object ? object : @object
257257

258258
if parent?
259-
spree.polymorphic_url([:admin, parent, target], options)
259+
routes_proxy.polymorphic_url([:admin, parent, target], options)
260260
else
261-
spree.polymorphic_url([:admin, target], options)
261+
routes_proxy.polymorphic_url([:admin, target], options)
262262
end
263263
end
264264

265265
def collection_url(options = {})
266266
if parent?
267-
spree.polymorphic_url([:admin, parent, model_class], options)
267+
routes_proxy.polymorphic_url([:admin, parent, model_class], options)
268268
else
269-
spree.polymorphic_url([:admin, model_class], options)
269+
routes_proxy.polymorphic_url([:admin, model_class], options)
270270
end
271271
end
272272

273+
def routes_proxy
274+
spree
275+
end
276+
273277
# Allow all attributes to be updatable.
274278
#
275279
# Other controllers can, should, override it to set custom logic

backend/spec/controllers/spree/admin/resource_controller_spec.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ def model_class
2222
# Database
2323
class CreateWidgets < ActiveRecord::Migration[5.1]
2424
def change
25-
create_table(:widgets) do |t|
26-
t.string :name
27-
t.integer :position
28-
t.timestamps null: false
25+
unless table_exists?(:widgets)
26+
create_table(:widgets) do |t|
27+
t.string :name
28+
t.integer :position
29+
t.timestamps null: false
30+
end
2931
end
3032
end
3133
end
@@ -290,4 +292,13 @@ def check_destroy_constraints
290292
expect(flash[:error]).to eql('Product is not found')
291293
end
292294
end
295+
296+
describe "#routes_proxy" do
297+
subject { controller.send(:routes_proxy) }
298+
299+
it "forwards to the #spree routing proxy" do
300+
expect(controller).to receive(:spree).and_call_original
301+
expect(subject).to be_a(ActionDispatch::Routing::RoutesProxy)
302+
end
303+
end
293304
end

0 commit comments

Comments
 (0)