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

Call authorize_resource on the index action #1234

Merged
merged 1 commit into from
Nov 13, 2020
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
1 change: 1 addition & 0 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def index
authorize_resource(resource_class)
search_term = params[:search].to_s.strip
resources = Administrate::Search.new(scoped_resource,
dashboard_class,
Expand Down
17 changes: 6 additions & 11 deletions spec/controllers/admin/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@ def pundit_user
end
end

let!(:user) { create :customer }
let(:user) { create(:customer) }

before(:each) do
# Create a few orders for the user and a few for other customers
create_list :order, 4, customer: create(:customer)
create_list :order, 7, customer: user
create_list :order, 2, customer: create(:customer)
create_list :order, 2, customer: user
end

# Policies are defined in order_policy.rb
describe "GET index" do
it "shows only the records in the admin scope" do
order = create(:order, customer: user)
_missing_order = create(:order)

locals = capture_view_locals { get :index }
expect(locals[:resources].count).to eq(9) # only my orders

expect(locals[:resources]).to contain_exactly(order)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(user, record)
end

def index?
false
true
end

def show?
Expand Down