Skip to content

Commit

Permalink
Merge pull request solidusio#5219 from mamhoff/resource-controller-fo…
Browse files Browse the repository at this point in the history
…r-foreign-engines

ResourceController: Allow overriding routes proxy
  • Loading branch information
waiting-for-dev authored Jul 11, 2023
2 parents 05439dd + 3957b5e commit 568f4ed
Show file tree
Hide file tree
Showing 2 changed files with 28 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
19 changes: 15 additions & 4 deletions backend/spec/controllers/spree/admin/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ def model_class
# Database
class CreateWidgets < ActiveRecord::Migration[5.1]
def change
create_table(:widgets) do |t|
t.string :name
t.integer :position
t.timestamps null: false
unless table_exists?(:widgets)
create_table(:widgets) do |t|
t.string :name
t.integer :position
t.timestamps null: false
end
end
end
end
Expand Down Expand Up @@ -290,4 +292,13 @@ def check_destroy_constraints
expect(flash[:error]).to eql('Product is not found')
end
end

describe "#routes_proxy" do
subject { controller.send(:routes_proxy) }

it "forwards to the #spree routing proxy" do
expect(controller).to receive(:spree).and_call_original
expect(subject).to be_a(ActionDispatch::Routing::RoutesProxy)
end
end
end

0 comments on commit 568f4ed

Please sign in to comment.