-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
182 additions
and
16 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
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
class Taro::Rails::Railtie < ::Rails::Railtie | ||
initializer("taro") do |_app| | ||
initializer("taro") do |app| | ||
ActiveSupport.on_load(:action_controller_base) do | ||
ActionController::Base.prepend(Taro::Rails::ControllerExtension) | ||
end | ||
|
||
app.reloader.to_prepare do | ||
Taro::Rails.reset | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Taro::Rails::RouteFinder | ||
class << self | ||
def call(controller_class:, action_name:) | ||
cache["#{controller_class.controller_path}##{action_name}"] || [] | ||
end | ||
|
||
def clear_cache | ||
@cache = nil | ||
end | ||
|
||
private | ||
|
||
def cache | ||
@cache ||= build_cache | ||
end | ||
|
||
def build_cache | ||
# Build a Hash like | ||
# { { controller: 'users', action: 'show', verb: 'GET' } => #<Route> } | ||
routes_by_attributes = map_routes_by_attributes | ||
|
||
# Rails has both PATCH and PUT routes for updates. We only need one copy. | ||
routes_by_attributes.reject! do |attrs, _route| | ||
attrs[:verb] == 'PATCH' && routes_by_attributes[attrs.merge(verb: 'PUT')] | ||
end | ||
|
||
# Build a Hash like | ||
# { 'users#show' } => [#<Route>, #<Route>] } | ||
routes_by_attributes.each_with_object({}) do |(attrs, route), map| | ||
(map["#{attrs[:controller]}##{attrs[:action]}"] ||= []) << route | ||
end | ||
end | ||
|
||
def map_routes_by_attributes | ||
routes.each_with_object({}) do |route, map| | ||
# Route#verb is a String. Its usually something like 'POST', but manual | ||
# matched routes may have e.g. 'GET|POST' (🤢). We only need one copy. | ||
verb = route.verb.to_s.scan(/\w+/).sort.last | ||
next unless verb | ||
|
||
# The #requirements Hash contains :controller (an underscored | ||
# controller name) and :action (the action name as String, e.g. 'show'). | ||
attrs = route.requirements.slice(:controller, :action).merge(verb:) | ||
map[attrs] = route | ||
end | ||
end | ||
|
||
def routes | ||
# make sure routes are loaded | ||
Rails.application.reload_routes! unless Rails.application.routes.routes.any? | ||
Rails.application.routes.routes | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,4 +88,3 @@ def raise_coercion_error(object) | |
MSG | ||
end | ||
end | ||
Taro::Types::Field::NOT_GIVEN = Object.new |
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,24 @@ | ||
def stub_rails(with_routes: []) | ||
rails = Module.new { def self.name = 'Rails' } | ||
application = instance_double( | ||
Rails::Application, | ||
env_config: {}, | ||
reloader: ActiveSupport::Reloader, | ||
reload_routes!: true, | ||
routes: instance_double(ActionDispatch::Routing::RouteSet, routes: with_routes), | ||
) | ||
rails.define_singleton_method(:application) { application } | ||
stub_const('Rails', rails) | ||
end | ||
|
||
def mock_user_route(verb: 'GET') | ||
instance_double( | ||
ActionDispatch::Journey::Route, | ||
path: instance_double( | ||
ActionDispatch::Journey::Path::Pattern, | ||
spec: instance_double(ActionDispatch::Journey::Nodes::Cat, to_s: '/users/:id'), | ||
), | ||
requirements: { controller: 'users', action: 'show' }, | ||
verb:, | ||
) | ||
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,12 @@ | ||
describe Taro::Rails::DefinitionBuffer do | ||
it 'raises if an endpoint has a definition but no route pointing to it' do | ||
buffer = Object.extend(described_class) | ||
controller_class = :dummy | ||
buffer.buffered_definition(controller_class) | ||
allow(Taro::Rails::RouteFinder).to receive(:call).and_return([]) | ||
|
||
expect do | ||
buffer.apply_buffered_definition(controller_class, :create) | ||
end.to raise_error(Taro::Error, /route.*dummy#create/i) | ||
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,35 @@ | ||
require 'action_controller' | ||
|
||
describe Taro::Rails::RouteFinder do | ||
let(:controller_class) { instance_double(ActionController::Base, controller_path: 'users') } | ||
|
||
it 'returns matching routes' do | ||
route = mock_user_route | ||
allow(described_class).to receive(:routes).and_return([route]) | ||
expect(described_class.call(controller_class:, action_name: 'show')).to eq([route]) | ||
end | ||
|
||
it 'returns an empty Array when no routes are found' do | ||
allow(described_class).to receive(:routes).and_return([]) | ||
expect(described_class.call(controller_class:, action_name: 'show')).to eq([]) | ||
end | ||
|
||
it 'ignores routes without verb' do | ||
allow(described_class).to receive(:routes).and_return([mock_user_route(verb: nil)]) | ||
expect(described_class.send(:build_cache)).to be_empty | ||
end | ||
|
||
describe '::routes' do | ||
it 'loads the routes if needed' do | ||
stub_rails | ||
expect(Rails.application).to receive(:reload_routes!) | ||
described_class.send(:routes) | ||
end | ||
|
||
it 'does not load the routes if they are already loaded' do | ||
stub_rails(with_routes: [:some_route]) | ||
expect(Rails.application).not_to receive(:reload_routes!) | ||
described_class.send(:routes) | ||
end | ||
end | ||
end |