From 1723a1b038d384c1d9a042c9c7c20958ebd349fd Mon Sep 17 00:00:00 2001 From: Martin Sereinig Date: Tue, 5 May 2015 08:07:50 +0200 Subject: [PATCH] NotAuthorizedError can be initialized with a string --- lib/pundit.rb | 12 ++++++++---- spec/pundit_spec.rb | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/pundit.rb b/lib/pundit.rb index 4c1a161..79f0380 100644 --- a/lib/pundit.rb +++ b/lib/pundit.rb @@ -14,11 +14,15 @@ module Pundit attr_reader :query, :record, :policy def initialize(options = {}) - @query = options[:query] - @record = options[:record] - @policy = options[:policy] + if options.is_a? String + message = options + else + @query = options[:query] + @record = options[:record] + @policy = options[:policy] - message = options.fetch(:message) { "not allowed to #{query} this #{record.inspect}" } + message = options.fetch(:message) { "not allowed to #{query} this #{record.inspect}" } + end super(message) end diff --git a/spec/pundit_spec.rb b/spec/pundit_spec.rb index f9b5428..e559fc1 100644 --- a/spec/pundit_spec.rb +++ b/spec/pundit_spec.rb @@ -344,4 +344,11 @@ describe Pundit do expect(Controller.new(double, params).permitted_attributes(post)).to eq({ 'votes' => 5 }) end end + + describe "Pundit::NotAuthorizedError" do + it "can be initialized with a string as message" do + error = Pundit::NotAuthorizedError.new("must be logged in") + expect(error.message).to eq "must be logged in" + end + end end