-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Annotate controllers in Rails engines and packs (#48)
~~This is a draft. I still have to get the tests updated and write more tests, but I wanted to get some feedback before doing so.~~ I approached this in a different way from the current implementation. 1. It starts similarly by gathering all the routes, to which I added following routes of any mounted engines. 2. Then instead of iterating through the controllers provided by the controller pattern, I iterated through the routes and then loaded the corresponding controller class. 3. From there I got the source location of controller, and added the annotations but only if the source location matched the controller pattern. It's possible with Zeitwerk (and probably other mechanisms) for the controller name and path to not line up exactly, so with this approach you don't need to make any assumptions. Please let me know what you think and if it looks good, I can add and update the tests. Fixes #47 --------- Co-authored-by: Nishiki (錦華) <[email protected]>
- Loading branch information
Showing
16 changed files
with
267 additions
and
135 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 |
---|---|---|
|
@@ -7,5 +7,7 @@ | |
/spec/reports/ | ||
/tmp/ | ||
|
||
.DS_Store | ||
|
||
.ruby-version | ||
Gemfile.lock |
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,5 +1,7 @@ | ||
class BurritosController < ApplicationController | ||
# @route POST /api/burritos (burritos) | ||
def create | ||
module Api | ||
class BurritosController < ApplicationController | ||
# @route POST /api/burritos (burritos) | ||
def create | ||
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
class CakesController < ApplicationController | ||
# This route's GET action should be named the same as its PUT action, | ||
# even though only the PUT action is named. | ||
def inherit | ||
module Api | ||
class CakesController < ApplicationController | ||
# This route's GET action should be named the same as its PUT action, | ||
# even though only the PUT action is named. | ||
def inherit | ||
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 |
---|---|---|
@@ -1,31 +1,33 @@ | ||
class TacosController < ApplicationController | ||
def show | ||
end | ||
module Api | ||
class TacosController < ApplicationController | ||
def show | ||
end | ||
|
||
# This is an example of generated annotations that come with Rails 6 | ||
# scaffolds. These should be replaced by Chusaku annotations. | ||
# POST /api/tacos | ||
# PATCH/PUT /api/tacos/1 | ||
def create | ||
end | ||
# This is an example of generated annotations that come with Rails 6 | ||
# scaffolds. These should be replaced by Chusaku annotations. | ||
# POST /api/tacos | ||
# PATCH/PUT /api/tacos/1 | ||
def create | ||
end | ||
|
||
# Update all the tacos! | ||
# @route this should be deleted, it's not a valid route. | ||
# We should not see a duplicate @route in this comment block. | ||
# @route PUT /api/tacos/:id (taco) | ||
# But this should remain (@route), because it's just words. | ||
def update | ||
end | ||
# Update all the tacos! | ||
# @route this should be deleted, it's not a valid route. | ||
# We should not see a duplicate @route in this comment block. | ||
# @route PUT /api/tacos/:id (taco) | ||
# But this should remain (@route), because it's just words. | ||
def update | ||
end | ||
|
||
# This route doesn't exist, so it should be deleted. | ||
# @route DELETE /api/tacos/:id | ||
def destroy | ||
puts("Tacos are indestructible") | ||
end | ||
# This route doesn't exist, so it should be deleted. | ||
# @route DELETE /api/tacos/:id | ||
def destroy | ||
puts("Tacos are indestructible") | ||
end | ||
|
||
private | ||
private | ||
|
||
def tacos_params | ||
params.require(:tacos).permit(:carnitas) | ||
def tacos_params | ||
params.require(:tacos).permit(:carnitas) | ||
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,2 @@ | ||
class ApplicationController | ||
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,46 @@ | ||
# This file overrides Rails methods such that we can test without installing | ||
# multiple versions of Rails in the test suite. If different versions of Rails | ||
# begin treating route generation differently, new overrides should be written | ||
# for each version. | ||
# | ||
# The mocks used should reflect the files located in `test/mock/app/`. | ||
|
||
require "pathname" | ||
require_relative "route_helper" | ||
require_relative "engine/app/controllers/engine_controller" | ||
require_relative "engine/app/controllers/cars_controller" | ||
|
||
module Engine | ||
extend RouteHelper | ||
|
||
class << self | ||
# Lets us call `Engine.app.routes.routes` without a skeleton Rails | ||
# app. | ||
# | ||
# @return [Minitest::Mock] Mocked `Engine.app` | ||
def app | ||
routes = [] | ||
|
||
routes.push \ | ||
mock_route \ | ||
controller: "cars", | ||
action: "create", | ||
verb: "POST", | ||
path: "/engine/cars(.:format)", | ||
name: "car" | ||
|
||
engine_app = Minitest::Mock.new | ||
app_routes = Minitest::Mock.new | ||
app_routes.expect(:routes, routes.compact) | ||
engine_app.expect(:routes, app_routes) | ||
engine_app | ||
end | ||
|
||
# Lets us call `Engine.engine?` without a skeleton Rails app. | ||
# | ||
# @return [Boolean] | ||
def engine? | ||
true | ||
end | ||
end | ||
end |
Oops, something went wrong.