Skip to content

Commit

Permalink
add support for API controller
Browse files Browse the repository at this point in the history
  • Loading branch information
emfy0 committed Jun 22, 2024
1 parent f1b38c7 commit de85849
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
- name: Install appraisal dependencies
run: bundle exec appraisal install
- name: Run tests
run: bundle exec appraisal rake test
run: bundle exec appraisal rake test
2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ end

appraise 'haml-6.3' do
gem 'haml', '>= 6.3', '< 6.4'
end
end
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Compatibility with `ActionController::API` - [@emfy0](https://github.com/emfy0)

### Removed

> _Add your own contributions to the next release on a new line above this; please include your name too._
Expand Down
15 changes: 13 additions & 2 deletions lib/rails-route-checker/loaded_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,24 @@ def all_route_names
end

def controller_information
@controller_information ||= ActionController::Base.descendants.map do |controller|
return @controller_information if @controller_information

base_controllers_descendants = [ActionController::Base, ActionController::API].flat_map(&:descendants)

@controller_information = base_controllers_descendants.map do |controller|
next if controller.controller_path.nil? || controller.controller_path.start_with?('rails/')

controller_helper_methods =
if controller.respond_to?(:helpers)
controller.helpers.methods.map(&:to_s)
else
[]
end

[
controller.controller_path,
{
helpers: controller.helpers.methods.map(&:to_s),
helpers: controller_helper_methods,
actions: controller.action_methods.to_a,
instance_methods: instance_methods(controller),
lookup_context: lookup_context(controller)
Expand Down
4 changes: 3 additions & 1 deletion rails-route-checker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rubocop', '~> 0.86'
spec.add_development_dependency 'appraisal', '~> 2.5.0'
spec.add_development_dependency 'minitest'
end
spec.add_development_dependency 'rails'
spec.add_development_dependency 'propshaft'
end
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/articles_api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ArticlesApiController < BaseApiController
def index
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/controllers/base_api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class BaseApiController < ActionController::API
end
1 change: 1 addition & 0 deletions test/dummy/app/views/articles_api/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.response 'This is json response'
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Rails.application.routes.draw do
resources :articles, only: [:index_erb, :index_haml]
resources :articles_api, only: :index
end
17 changes: 17 additions & 0 deletions test/rails-route-checker/loaded_app_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'minitest/autorun'

require 'rails-route-checker/loaded_app'

describe RailsRouteChecker::LoadedApp do
let(:loaded_app) do
Dir.stub(:pwd, File.join(__dir__, '../dummy')) do
RailsRouteChecker::LoadedApp.new
end
end

it 'parses ActionController::Base and ActionController::API descendants' do
assert_equal(
loaded_app.controller_information.keys.sort, %w[application articles base_api articles_api].sort
)
end
end

0 comments on commit de85849

Please sign in to comment.