Rails 最佳实践误报未使用的方法

问题描述 投票:0回答:2

我正在使用 Rails 最佳实践来验证我自己的代码。

./app/controllers/news_items_controller.rb:2 - remove unused methods (NewsItemsController#new)
./app/controllers/news_items_controller.rb:2 - remove unused methods (NewsItemsController#create)
./app/controllers/news_items_controller.rb:2 - remove unused methods (NewsItemsController#edit)
./app/controllers/news_items_controller.rb:2 - remove unused methods (NewsItemsController#update)
./app/controllers/news_items_controller.rb:2 - remove unused methods (NewsItemsController#destroy)
./app/controllers/pages_controller.rb:1 - remove unused methods (PagesController#new)
./app/controllers/pages_controller.rb:1 - remove unused methods (PagesController#create)
./app/controllers/pages_controller.rb:1 - remove unused methods (PagesController#edit)
./app/controllers/pages_controller.rb:1 - remove unused methods (PagesController#update)
./app/controllers/pages_controller.rb:1 - remove unused methods (PagesController#destroy)
./app/controllers/registrations_controller.rb:2 - remove unused methods (RegistrationsController#update)
./app/controllers/registrations_controller.rb:8 - remove unused methods (RegistrationsController#after_sign_up_path_for)
./app/controllers/registrations_controller.rb:1 - remove unused methods (RegistrationsController#new)
./app/controllers/registrations_controller.rb:1 - remove unused methods (RegistrationsController#create)
./app/controllers/registrations_controller.rb:1 - remove unused methods (RegistrationsController#edit)
./app/controllers/registrations_controller.rb:1 - remove unused methods (RegistrationsController#destroy)
./app/controllers/static_controller.rb:2 - remove unused methods (StaticController#new)
./app/controllers/static_controller.rb:2 - remove unused methods (StaticController#create)
./app/controllers/static_controller.rb:2 - remove unused methods (StaticController#edit)
./app/controllers/static_controller.rb:2 - remove unused methods (StaticController#update)
./app/controllers/static_controller.rb:2 - remove unused methods (StaticController#destroy)

这里的动作大多是未使用的方法。唯一的问题是这些操作甚至没有定义...我尝试使用

remove_method
但没有成功。

例如,news_items_controller 仅具有

index
show
,而路由仅定义
index
show

我的班级定义如下:

NewsItemsController << StaticController << ApplicationController << ActionController::Base

我的猜测是 ActionController::Base 正在定义它们,有趣的是它没有显示“ApplicationController”...

这里是StaticController的内容

# encoding: utf-8                                                                                                                                                                                     
class StaticController < ApplicationController
end

并且没有路线指向

StaticController

还有routes.rb

  ActiveAdmin.routes(self)

  scope "(:locale)", :locale => /ru|en/ do
    devise_for :users, :controllers => { :registrations => "registrations" }

    resources :news_items, only: [:show, :index]
    resources :pages, :only => [:show]

    root to: "home#index"
  end

  root to: "home#index"
ruby-on-rails ruby false-positive rbp
2个回答
4
投票

Rails 最佳实践经常沉迷于显示误报。 在这种情况下,我不会太信任 RBP,而是继续将所有操作排除在rails_best_practises.yml 文件中。

例如:

RemoveUnusedMethodsInControllersCheck: { except_methods: ['YourController#search'] }

0
投票

您还可以使用 内联注释(于 2021 年添加)来禁用特定的

rails_best_practices
检查:

class Foo
  def mymethod # rails_best_practices:disable RemovedUnusedMethodsInControllersCheck
    ...
  end

end
© www.soinside.com 2019 - 2024. All rights reserved.