-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from skryukov/inertia-share-before-action-lik…
…e-filters `before_action` like filters for `inertia_share`
- Loading branch information
Showing
6 changed files
with
217 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
# | ||
# Based on AbstractController::Callbacks::ActionFilter | ||
# https://github.com/rails/rails/blob/v7.2.0/actionpack/lib/abstract_controller/callbacks.rb#L39 | ||
module InertiaRails | ||
class ActionFilter | ||
def initialize(conditional_key, actions) | ||
@conditional_key = conditional_key | ||
@actions = Array(actions).map(&:to_s).to_set | ||
end | ||
|
||
def match?(controller) | ||
missing_action = @actions.find { |action| !controller.available_action?(action) } | ||
if missing_action | ||
message = <<~MSG | ||
The #{missing_action} action could not be found for the :inertia_share | ||
callback on #{controller.class.name}, but it is listed in the controller's | ||
#{@conditional_key.inspect} option. | ||
MSG | ||
|
||
raise AbstractController::ActionNotFound.new(message, controller, missing_action) | ||
end | ||
|
||
@actions.include?(controller.action_name) | ||
end | ||
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
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,55 @@ | ||
# spec/lib/inertia_rails/action_filter_spec.rb | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe InertiaRails::ActionFilter do | ||
let(:controller) do | ||
instance_double( | ||
'ActionController::Base', | ||
action_name: 'current_action', | ||
class: instance_double('Class', name: 'TestController') | ||
).tap do |stub| | ||
allow(stub).to receive(:available_action?).and_return(true) | ||
allow(stub).to receive(:available_action?).with('nonexistent').and_return(false) | ||
end | ||
end | ||
|
||
describe '#match?' do | ||
context 'when action exists' do | ||
it 'returns true if action matches' do | ||
filter = described_class.new(:only, 'current_action') | ||
expect(filter.match?(controller)).to be true | ||
end | ||
|
||
it 'returns false if action does not match' do | ||
filter = described_class.new(:only, 'other_action') | ||
expect(filter.match?(controller)).to be false | ||
end | ||
|
||
it 'handles multiple actions' do | ||
filter = described_class.new(:only, %w[current_action other actions]) | ||
expect(filter.match?(controller)).to be true | ||
end | ||
|
||
it 'handles symbol actions' do | ||
filter = described_class.new(:only, :current_action) | ||
expect(filter.match?(controller)).to be true | ||
end | ||
end | ||
|
||
context 'when action does not exist' do | ||
it 'raises ActionNotFound with appropriate message' do | ||
filter = described_class.new(:only, :nonexistent) | ||
expected_message = <<~MSG | ||
The nonexistent action could not be found for the :inertia_share | ||
callback on TestController, but it is listed in the controller's | ||
:only option. | ||
MSG | ||
|
||
expect { | ||
filter.match?(controller) | ||
}.to raise_error(AbstractController::ActionNotFound, expected_message) | ||
end | ||
end | ||
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