如何在Rails 4上使用RuboCop::Cop::Rails:ApplicationRecord



我想在Rails 4上使用ApplicationRecord Cop。我已经添加了"self.abstract_class=true"。

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

这个文件写为"minimum_target_rails_version 5.0"。

https://github.com/rubocop-hq/rubocop-rails/blob/master/lib/rubocop/cop/rails/application_record.rb

所以我创建了这样的custom_cp。

# frozen_string_literal: true
module CustomCops
# @example
#
#  # good
#  class Rails5Model < ApplicationRecord
#    # ...
#  end
#
#  # bad
#  class Rails4Model < ActiveRecord::Base
#    # ...
#  end
class MustApplicationRecord < RuboCop::Cop::Cop
MSG = 'Models should subclass `ApplicationRecord`.'
SUPERCLASS = 'ApplicationRecord'
BASE_PATTERN = '(const (const nil? :ActiveRecord) :Base)'
include RuboCop::Cop::EnforceSuperclass

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.source_range, self.class::SUPERCLASS)
end
end
end
end

但我在运行rspec时遇到了一个错误,就像这样。

Failure/Error:
class MustApplicationRecord < RuboCop::Cop::Cop
MSG = 'Models should subclass `ApplicationRecord`.'
SUPERCLASS = 'ApplicationRecord'
BASE_PATTERN = '(const (const nil? :ActiveRecord) :Base)'
include RuboCop::Cop::EnforceSuperclass

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.source_range, self.class::SUPERCLASS)
NameError:
uninitialized constant CustomCops::RuboCop

你知道如何解决这个错误,或者其他方法(比如将"minimum_target_rails_version 5.0"重写为"minimut_target_rails_version 4.0"(吗?

版本

Rails 4.2.8
ruby 2.6.6
rubocop 0.80.1
RSpec 3.9

错误告诉您未定义RuboCop

在定义你的警察之前,你需要先在某个地方require 'rubocop'

相关内容

  • 没有找到相关文章

最新更新