Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use RuboCop::Cop::Rails::ApplicationRecord on Rails 4 #295

Closed
ryosk7 opened this issue Jul 20, 2020 · 2 comments
Closed

How to use RuboCop::Cop::Rails::ApplicationRecord on Rails 4 #295

ryosk7 opened this issue Jul 20, 2020 · 2 comments

Comments

@ryosk7
Copy link

ryosk7 commented Jul 20, 2020

Hi there.
I want to use ApplicationRecord Cop on Rails 4.
I already added 'self.abstract_class = true' .

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

And this file is written 'minimum_target_rails_version 5.0'.

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

So I created custom_cop like this.

# 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

But I got an error running rspec, like this.

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

Do you know how to solve this error, or other method( like override 'minimum_target_rails_version 5.0' to 'minimum_target_rails_version 4.0' ) ?

versions

Rails 4.2.8
ruby 2.6.6
rubocop 0.80.1
RSpec 3.9
@koic
Copy link
Member

koic commented Aug 2, 2020

I'm sorry about I will close this issue because RuboCop Rails is currently negative support for Rails 4 series. #236
I think it works by reusing Rails/ApplicationRecord cop's implementation with your custom cop and removing minimum_target_rails_version 5.0 from the reused implementation.

@koic koic closed this as completed Aug 2, 2020
@ryosk7
Copy link
Author

ryosk7 commented Aug 2, 2020

Thank you :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants