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

Namespaced policy not found #2

Closed
pcriv opened this issue Apr 20, 2018 · 5 comments
Closed

Namespaced policy not found #2

pcriv opened this issue Apr 20, 2018 · 5 comments
Labels
bug Something isn't working

Comments

@pcriv
Copy link

pcriv commented Apr 20, 2018

Current scenario:

# models/appointment.rb
class Appointment
end

# policies/care/appointment_policy.rb
module Care
  class AppointmentPolicy < ApplicationPolicy
  end
end

# controllers/care/appointments_controller.rb
module Care
  class AppointmentsController < BaseController
    def index
      authorize!
    end
  end
end

# => ActionPolicy::NotFound in Care::AppointmentsController#index 
# => Couldn't find policy class for Appointment

Ruby version: 2.5.0
Rails: 5.2.0

Errors seems to be that when this check is made:

mod.const_defined?(policy_name, false)

The Care namespace only contains this constants:

mod.constants
=> [:AppointmentsController, :BaseController]

The weirdest thing is that my test are working:

# frozen_string_literal: true

require "rails_helper"

RSpec.describe "GET /care/appointments", type: :request do
  before { sign_in(user) }

  subject { get care_appointments_path }

  context "as a caretaker" do
    let(:user) { create(:caretaker, :confirmed) }

    it "is authorized" do
      expect { subject }.to be_authorized_to(:index?, Appointment)
        .with(Care::AppointmentPolicy)
    end
  end
end

Any idea what could be the issue here?

@palkan
Copy link
Owner

palkan commented Apr 20, 2018

Hi, @pablocrivella!

The weirdest thing is that my test are working:

That could be a clue. Do you have config.eager_load = true in your environments/test.rb? And config.eager_load = false in your development.rb respectively?

@palkan palkan added the bug Something isn't working label Apr 20, 2018
@pcriv
Copy link
Author

pcriv commented Apr 20, 2018

@palkan It's false on both. Should those settings be true for this to work?

@palkan
Copy link
Owner

palkan commented Apr 20, 2018

Should those settings be true for this to work?

Yep, that should help.

I've pushed a fixed (hopefully); please, try master branch.

@pcriv
Copy link
Author

pcriv commented Apr 21, 2018

@palkan I can confirm master is working without changing setting config.eager_load to true

@palkan
Copy link
Owner

palkan commented Apr 22, 2018

@pablocrivella Thanks!

Released as 0.1.1

@palkan palkan closed this as completed Apr 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants