avdi / naught

A toolkit for building Null Object classes in Ruby
MIT License
1.05k stars 53 forks source link

Pundit does not see NullObject so can't find policy #73

Closed helphop closed 6 years ago

helphop commented 6 years ago

lib/null_object.rb

require 'naught'

NulllObject = Naught.build do |config|
  config.define_explicit_conversions
  config.define_implicit_conversions
  config.black_hole
end

In my model:

  def self.find_by_id(id)
    ProofreadingJob.where(id: id).first || NullObject.new
  end

I have the following in my controller:

  def show
    @proofreading_job = ProofreadingJob.find_by_id(params[:id])
    authorize [:proofreader, @proofreading_job], :show?
  end

Pundit gives the following error:

unable to find policyProofreader::Policyfor[:proofreader, null]`

However, if I create a simple NullObject class as follows:

class NullObject end

And don't use the one built using the Naught gem, Pundit can read the class of this NullObject and can find the NullObjectPolicy.

Even more bizarre is if I do the following:

  def show
    @proofreading_job = ProofreadingJob.find_by_id(params[:id])
    authorize [:proofreader, @proofreading_job.class], :show?
  end

It works. Any ideas why Pundit can't 'see' the class of the NullObject derived from the Naught gem?