-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BDO-348] Add custom pundit macros (#329)
- Loading branch information
Showing
5 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Macro | ||
def self.Policy(policy_class, rule, policy_params: nil, name: :default, model: :model, **) | ||
task = ->((ctx, flow_options), **) { | ||
policy_namespace = :"macro.policy.#{name}" | ||
ctx[policy_namespace] = policy_class.new(ctx[:current_user], ctx[model]) | ||
result = if policy_params | ||
ctx[policy_namespace].public_send(rule, ctx[:policy_params]) | ||
else | ||
ctx[policy_namespace].public_send(rule) | ||
end | ||
unless result | ||
message = "#{policy_class.name.demodulize.underscore}.#{rule}" || 'default' | ||
current_errors = ctx[:errors] || {} | ||
ctx[:errors] = current_errors.deep_merge( | ||
{ | ||
policy: [I18n.t("policy.errors.#{message}")] | ||
} | ||
) | ||
|
||
ctx[:semantic_failure] = :forbidden | ||
end | ||
|
||
signal = result ? Trailblazer::Activity::Right : Trailblazer::Activity::Left | ||
[signal, [ctx, flow_options]] | ||
} | ||
|
||
{ task: task, id: "#{name}/#{__method__}_id_#{task.object_id}".underscore } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Macro do | ||
describe '.Policy' do | ||
subject(:result) { described_class::Policy(policy_class, rule)[:task].call(ctx, **flow_options) } | ||
|
||
let(:flow_options) { {} } | ||
let(:rule) { :update_role? } | ||
let(:ctx) { {} } | ||
let(:policy_class) { TestPolicy } | ||
|
||
before { stub_const('TestPolicy', Struct.new(:TestPolicy)) } | ||
|
||
context 'when set policy_params to true' do | ||
subject(:result) do | ||
described_class::Policy(policy_class, | ||
rule, | ||
policy_params: true)[:task].call(ctx, **flow_options) | ||
end | ||
|
||
let(:ctx) { { policy_params: true } } | ||
|
||
it 'is allowed' do | ||
expect(policy_class).to receive_message_chain(:new, rule).with(ctx[:policy_params]).and_return(true) | ||
expect(result[0]).to eq(Trailblazer::Activity::Right) | ||
end | ||
end | ||
|
||
context 'when current_user in have access to model' do | ||
before do | ||
allow(policy_class).to receive_message_chain(:new, rule).and_return(true) | ||
end | ||
|
||
it 'is allowed' do | ||
expect(result[0]).to eq(Trailblazer::Activity::Right) | ||
expect(ctx[:semantic_failure]).to be_nil | ||
end | ||
|
||
it 'is put policy in right namespace' do | ||
expect(result.second.first[:'macro.policy.default']).to be_instance_of(RSpec::Mocks::Double) | ||
expect(result[1][0][:'macro.policy.default']).to be_respond_to(rule) | ||
end | ||
end | ||
|
||
context 'when current_user in have not access to model' do | ||
before do | ||
allow(policy_class).to receive_message_chain(:new, rule).and_return(false) | ||
end | ||
|
||
it 'is not allowed' do | ||
expect(result[0]).to eq(Trailblazer::Activity::Left) | ||
expect(ctx[:semantic_failure]).to eq(:forbidden) | ||
end | ||
end | ||
|
||
context 'when other than default namespace' do | ||
subject(:result) { described_class::Policy(policy_class, rule, name: 'custom')[:task].call(ctx, **flow_options) } | ||
|
||
before do | ||
allow(policy_class).to receive_message_chain(:new, rule).and_return(true) | ||
end | ||
|
||
it 'is put policy in right namespace' do | ||
expect(result[0]).to eq(Trailblazer::Activity::Right) | ||
expect(result[1][0][:'macro.policy.custom']).to be_instance_of(RSpec::Mocks::Double) | ||
expect(result[1][0][:'macro.policy.custom']).to be_respond_to(rule) | ||
expect(result[1][0][:'macro.policy.default']).not_to be_present | ||
end | ||
end | ||
end | ||
end |