Skip to content

Commit 65e3557

Browse files
committed
add invalid context error type
1 parent 6946970 commit 65e3557

11 files changed

Lines changed: 33 additions & 21 deletions

File tree

lib/rabarber.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
module Rabarber
2222
class Error < StandardError; end
23-
class ConfigurationError < Rabarber::Error; end
2423
class InvalidArgumentError < Rabarber::Error; end
24+
class ConfigurationError < Rabarber::InvalidArgumentError; end
25+
class InvalidContextError < Rabarber::InvalidArgumentError; end
2526
class NotFoundError < Rabarber::Error; end
2627

2728
delegate :configure, to: Rabarber::Configuration

lib/rabarber/controllers/concerns/authorization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def grant_access(action: nil, roles: nil, context: nil, if: nil, unless: nil)
3535
).process,
3636
Rabarber::Inputs::Contexts::Authorizational.new(
3737
context,
38+
error: Rabarber::InvalidContextError,
3839
message: "Expected a Class, an instance of ActiveRecord model, a symbol, a string, or a proc, got #{context.inspect}"
3940
).resolve,
4041
Rabarber::Inputs::DynamicRule.new(

lib/rabarber/core/rule.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ def execute_rule(controller_instance, rule)
3333
end
3434

3535
def resolve_context(controller_instance)
36-
resolved_context = case @context
37-
when Proc then controller_instance.instance_exec(&@context)
38-
when Symbol then controller_instance.send(@context)
39-
else @context
40-
end
41-
Rabarber::Inputs::Context.new(resolved_context, message: "Expected an instance of ActiveRecord model, a Class, or nil, got #{resolved_context.inspect}").resolve
36+
context = case @context
37+
when Proc then controller_instance.instance_exec(&@context)
38+
when Symbol then controller_instance.send(@context)
39+
else @context
40+
end
41+
42+
Rabarber::Inputs::Context.new(
43+
context,
44+
error: Rabarber::InvalidContextError,
45+
message: "Expected an instance of ActiveRecord model, a Class, or nil, got #{context.inspect}"
46+
).resolve
4247
end
4348
end
4449
end

lib/rabarber/inputs/base.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ def process
1818
type_checker = @optional ? type.optional : type
1919
type_checker[@value]
2020
rescue Dry::Types::CoercionError
21-
raise @error, @message
21+
raise_error
2222
end
2323

2424
private
2525

2626
def type
2727
raise NotImplementedError
2828
end
29+
30+
def raise_error
31+
raise @error, @message
32+
end
2933
end
3034
end
3135
end

lib/rabarber/inputs/context.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ def resolve
1010
when Class
1111
{ context_type: context.to_s, context_id: nil }
1212
when ActiveRecord::Base
13-
raise @error, @message unless context.persisted?
14-
13+
raise_error unless context.persisted?
1514
{ context_type: context.class.to_s, context_id: context.public_send(context.class.primary_key) }
1615
else
1716
context

lib/rabarber/models/concerns/has_roles.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def process_role_names(role_names)
9090
def process_context(context)
9191
Rabarber::Inputs::Context.new(
9292
context,
93+
error: Rabarber::InvalidContextError,
9394
message: "Expected an instance of ActiveRecord model, a Class, or nil, got #{context.inspect}"
9495
).resolve
9596
end

lib/rabarber/models/role.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def process_role_name(name)
8282
def process_context(context)
8383
Rabarber::Inputs::Context.new(
8484
context,
85+
error: Rabarber::InvalidContextError,
8586
message: "Expected an instance of ActiveRecord model, a Class, or nil, got #{context.inspect}"
8687
).resolve
8788
end

spec/rabarber/controllers/concerns/authorization_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
context "when context is invalid" do
6464
let(:args) { { context: 1 } }
6565

66-
it_behaves_like "raises an error", Rabarber::InvalidArgumentError, "Expected a Class, an instance of ActiveRecord model, a symbol, a string, or a proc, got 1"
66+
it_behaves_like "raises an error", Rabarber::InvalidContextError, "Expected a Class, an instance of ActiveRecord model, a symbol, a string, or a proc, got 1"
6767
end
6868

6969
context "when dynamic rule is invalid" do

spec/rabarber/core/rule_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
let(:rule) { described_class.new(roles, 123, nil, nil) }
8585

8686
it "raises with correct message" do
87-
expect { subject }.to raise_error(Rabarber::InvalidArgumentError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
87+
expect { subject }.to raise_error(Rabarber::InvalidContextError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
8888
end
8989
end
9090
end

spec/rabarber/models/concerns/has_roles_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
let(:context) { 123 }
8282

8383
it "raises with correct message" do
84-
expect { subject }.to raise_error(Rabarber::InvalidArgumentError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
84+
expect { subject }.to raise_error(Rabarber::InvalidContextError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
8585
end
8686
end
8787
end
@@ -198,7 +198,7 @@
198198
let(:roles) { [:admin] }
199199

200200
it "raises with correct message" do
201-
expect { subject }.to raise_error(Rabarber::InvalidArgumentError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
201+
expect { subject }.to raise_error(Rabarber::InvalidContextError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
202202
end
203203
end
204204
end
@@ -401,7 +401,7 @@
401401
let(:create_new) { true }
402402

403403
it "raises with correct message" do
404-
expect { subject }.to raise_error(Rabarber::InvalidArgumentError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
404+
expect { subject }.to raise_error(Rabarber::InvalidContextError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
405405
end
406406
end
407407
end
@@ -467,7 +467,7 @@
467467
let(:context) { 123 }
468468

469469
it "raises with correct message" do
470-
expect { subject }.to raise_error(Rabarber::InvalidArgumentError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
470+
expect { subject }.to raise_error(Rabarber::InvalidContextError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
471471
end
472472
end
473473
end
@@ -525,7 +525,7 @@
525525
end
526526

527527
it "raises with correct message" do
528-
expect { subject }.to raise_error(Rabarber::InvalidArgumentError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
528+
expect { subject }.to raise_error(Rabarber::InvalidContextError, "Expected an instance of ActiveRecord model, a Class, or nil, got 123")
529529
end
530530
end
531531
end

0 commit comments

Comments
 (0)