Skip to content

Commit

Permalink
ResourceController: Allow overriding routes proxy
Browse files Browse the repository at this point in the history
Within Solidus, we always want to use the `spree` routes proxy as that
is where all routes are defined. However, many people use the Resource
controller as a parent controller in their main app, and because of this
they need to override all the URL generation methods in the Resource
Controller.

This allows one to specify the routes proxy in a custom controller as
follows:

```rb
module MyApp
  class WidgetsController < Spree::Admin::ResourceController
  private

  def routes_proxy
    main_app
  end
end
```

Engine Developers can use this for engines, as well:

```rb
module MyEngine
  class WidgetsController < Spree::Admin::ResourceController
  private

  def routes_proxy
    my_engine
  end
end
```
  • Loading branch information
mamhoff committed Jul 7, 2023
1 parent 2f59fa0 commit 1f62a61
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
22 changes: 13 additions & 9 deletions backend/app/controllers/spree/admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def parent
.find_by!(self.class.parent_data[:find_by] => params["#{parent_model_name}_id"])
instance_variable_set("@#{parent_model_name}", @parent)
rescue ActiveRecord::RecordNotFound => e
resource_not_found(flash_class: e.model.constantize, redirect_url: spree.polymorphic_url([:admin, parent_model_name.pluralize.to_sym]))
resource_not_found(flash_class: e.model.constantize, redirect_url: routes_proxy.polymorphic_url([:admin, parent_model_name.pluralize.to_sym]))
end

def parent?
Expand Down Expand Up @@ -238,38 +238,42 @@ def location_after_save

def new_object_url(options = {})
if parent?
spree.new_polymorphic_url([:admin, parent, model_class], options)
routes_proxy.new_polymorphic_url([:admin, parent, model_class], options)
else
spree.new_polymorphic_url([:admin, model_class], options)
routes_proxy.new_polymorphic_url([:admin, model_class], options)
end
end

def edit_object_url(object, options = {})
if parent?
spree.polymorphic_url([:edit, :admin, parent, object], options)
routes_proxy.polymorphic_url([:edit, :admin, parent, object], options)
else
spree.polymorphic_url([:edit, :admin, object], options)
routes_proxy.polymorphic_url([:edit, :admin, object], options)
end
end

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

if parent?
spree.polymorphic_url([:admin, parent, target], options)
routes_proxy.polymorphic_url([:admin, parent, target], options)
else
spree.polymorphic_url([:admin, target], options)
routes_proxy.polymorphic_url([:admin, target], options)
end
end

def collection_url(options = {})
if parent?
spree.polymorphic_url([:admin, parent, model_class], options)
routes_proxy.polymorphic_url([:admin, parent, model_class], options)
else
spree.polymorphic_url([:admin, model_class], options)
routes_proxy.polymorphic_url([:admin, model_class], options)
end
end

def routes_proxy
spree
end

# Allow all attributes to be updatable.
#
# Other controllers can, should, override it to set custom logic
Expand Down
29 changes: 25 additions & 4 deletions backend/spec/controllers/spree/admin/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

require 'spec_helper'

module Spree
module MyEngine
module Admin
class WidgetsController < Spree::Admin::ResourceController
prepend_view_path('spec/test_views')

def model_class
Widget
end

def routes_proxy
my_engine
end
end
end
end

describe Spree::Admin::WidgetsController, type: :controller do
describe MyEngine::Admin::WidgetsController, type: :controller do
stub_authorization!

# RESOURCE FIXTURE
before(:all) do
# Engine
module MyEngine
class Engine < Rails::Engine
isolate_namespace MyEngine
end
end

# Database
class CreateWidgets < ActiveRecord::Migration[5.1]
def change
Expand Down Expand Up @@ -46,13 +57,17 @@ def check_destroy_constraints
end

# Routes
Spree::Core::Engine.routes.draw do
MyEngine::Engine.routes.draw do
namespace :admin do
resources :widgets do
post :update_positions, on: :member
end
end
end

DummyApp::Application.routes.draw do
mount MyEngine::Engine, at: "/"
end
end

after(:all) do
Expand All @@ -64,12 +79,18 @@ def check_destroy_constraints
Object.send(:remove_const, :Widget)

# Controller
Spree::Admin.send(:remove_const, :WidgetsController)
MyEngine::Admin.send(:remove_const, :WidgetsController)

#Engine
MyEngine.send(:remove_const, :Engine)
Object.send(:remove_const, :MyEngine)

# Routes
Rails.application.reload_routes!
end

routes { MyEngine::Engine.routes }

describe '#new' do
subject do
get :new
Expand Down

0 comments on commit 1f62a61

Please sign in to comment.