Skip to content

Commit

Permalink
Add specs and doc for override redirect feature
Browse files Browse the repository at this point in the history
  • Loading branch information
edimossilva committed Jun 3, 2021
1 parent 9ce3fc8 commit 554e6af
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
5 changes: 2 additions & 3 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def after_resource_destroyed_path(_requested_resource)
{ action: :index }
end

def after_resource_created_path(resource)
[namespace, resource]
def after_resource_created_path(requested_resource)
[namespace, requested_resource]
end

def after_resource_updated_path(requested_resource)
Expand Down Expand Up @@ -158,7 +158,6 @@ def scoped_resource
def apply_collection_includes(relation)
resource_includes = dashboard.collection_includes
return relation if resource_includes.empty?

relation.includes(*resource_includes)
end

Expand Down
20 changes: 19 additions & 1 deletion docs/customizing_controller_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@ end
def default_sorting_direction
:desc
end
```
```

## Customizing Redirects after actions

To change redirect after the actions `create`, `update` and `destroy` you could override `after_resource_created_path` or `after_resource_updated_path` or `after_resource_destroyed_path` like this

```ruby
def after_resource_destroyed_path(_requested_resource)
{ action: :index, controller: :some_other_resource }
end

def after_resource_created_path(requested_resource)
[namespace, requested_resource.some_other_resource]
end

def after_resource_updated_path(requested_resource)
[namespace, requested_resource.some_other_resource]
end
```
43 changes: 43 additions & 0 deletions spec/controllers/admin/application_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "rails_helper"

module Admin
class OrdersController < Admin::ApplicationController
def after_resource_destroyed_path(_requested_resource)
{ action: :index, controller: :customers }
end

def after_resource_created_path(requested_resource)
[namespace, requested_resource.customer]
end

def after_resource_updated_path(requested_resource)
[namespace, requested_resource.customer]
end
end
end

RSpec.describe Admin::OrdersController, type: :controller do
it "redirect to custom route after destroy" do
order = create(:order)

delete :destroy, id: order.to_param
expect(response).to redirect_to(admin_customers_path)
end

it "redirect to custom route after create" do
customer = create(:customer)
order_attributes = build(:order, customer: customer).attributes
params = order_attributes.except("id", "created_at", "updated_at", "shipped_at")

post :create, order: params
expect(response).to redirect_to(admin_customer_path(customer))
end

it "redirect to custom route after update" do
order = create(:order)
order_params = { address_line_one: order.address_line_one }

put :update, id: order.to_param, order: order_params
expect(response).to redirect_to(admin_customer_path(order.customer))
end
end

0 comments on commit 554e6af

Please sign in to comment.