Class: Shirobai::Cop::Rails::ApplicationRecord

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector, RuboCop::Cop::TargetRailsVersion
Includes:
BundleEligible
Defined in:
lib/shirobai/cop/rails/application_record.rb

Overview

Drop-in Rust reimplementation of Rails/ApplicationRecord (rubocop-rails 2.35.5).

Rust replicates stock's EnforceSuperclass mixin: class X < ActiveRecord::Base (unless X's terminal name is ApplicationRecord) and Class.new(ActiveRecord::Base) (unless it is the direct value of an ApplicationRecord = constant write, covering the do..end / {} block forms). The offense range is the superclass / argument const node (leading :: included); the wrapper replaces it with the bare ApplicationRecord name — byte for byte with stock's autocorrect.

TargetRailsVersion: like stock this cop is gated on requires_gem('railties', '>= 5.0'), so it stays silent on Rails < 5.0 or without railties in the target bundle. The Exclude: db/**/*.rb from the merged default.yml is resolved by RuboCop through this wrapper's badge, exactly as for the stock cop.

Constant Summary collapse

MSG =
"Models should subclass `ApplicationRecord`."
SUPERCLASS =
"ApplicationRecord"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



33
# File 'lib/shirobai/cop/rails/application_record.rb', line 33

def self.badge = RuboCop::Cop::Badge.parse(cop_name)

.bundle_args(_config) ⇒ Object

No behavioral config: the segment is a wake-up flag only, so bundle_args contributes nothing.



37
38
39
# File 'lib/shirobai/cop/rails/application_record.rb', line 37

def self.bundle_args(_config)
  []
end

.cop_nameObject



32
# File 'lib/shirobai/cop/rails/application_record.rb', line 32

def self.cop_name = "Rails/ApplicationRecord"

Instance Method Details

#on_new_investigationObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shirobai/cop/rails/application_record.rb', line 41

def on_new_investigation
  buffer = processed_source.buffer
  source = bundle_eligible? ? processed_source.raw_source : buffer.source
  off = SourceOffsets.for(source)
  resolved_offenses.each do |start_offset, end_offset|
    range = Parser::Source::Range.new(buffer, off[start_offset], off[end_offset])
    add_offense(range, message: MSG) do |corrector|
      corrector.replace(range, self.class::SUPERCLASS)
    end
  end
end