Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding the routes proxy in the ResourceController #5219

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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