Rubocop

Set up rubcop with a predefined configuration file
Icons/chart bar
Used 16 times
Created by
A Arandi Lopez

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/XE5sP0"
Template Source

Review the code before running this template on your machine.

def create_bin(name, command = nil)
  create_file "bin/#{name}" do <<~EOF
    #!/usr/bin/env ruby
    APP_ROOT = File.expand_path('..', __dir__)
    Dir.chdir(APP_ROOT) do
      begin
        exec '#{command || name}'
      rescue Errno::ENOENT
        $stderr.puts "#{name} executable was not detected in the system."
        exit 1
      end
    end
  EOF
  end
  `chmod +x bin/#{name}`
end

gem_group :development, :test do
  gem 'rubocop'
  gem 'rubocop-minitest'
  gem 'rubocop-performance'
  gem 'rubocop-rails'
end

Bundler.with_unbundled_env { run 'bundle install' }

create_file '.rubocop.yml' do <<~YAML
    require:
      - rubocop-rails
      - rubocop-performance
      - rubocop-minitest

    inherit_mode:
      merge:
        - Exclude

    # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
    # to ignore them, so only the ones explicitly set in this file are enabled.
    AllCops:
      NewCops: disable
      DisabledByDefault: true
      Exclude:
        - "**/tmp/**/*"
        - "**/vendor/**/*"
        - "**/node_modules/**/*"
    Bundler/OrderedGems:
      Enabled: true
      AutoCorrect: true
    Layout/EmptyLineAfterMagicComment:
      Enabled: true
    Layout/EmptyLinesAroundAttributeAccessor:
      Enabled: true
    Layout/LineLength:
      Enabled: true
    Layout/SpaceAroundMethodCallOperator:
      Enabled: true
    Lint/DeprecatedOpenSSLConstant:
      Enabled: true
    Lint/MixedRegexpCaptureTypes:
      Enabled: true
    Lint/RaiseException:
      Enabled: true
    Lint/StructNewOverride:
      Enabled: true
    Minitest:
      Enabled: true
    Performance:
      Enabled: true
    Security:
      Enabled: true
    Style/AccessorGrouping:
      Enabled: true
    Style/BisectedAttrAccessor:
      Enabled: true
    Style/ClassAndModuleChildren:
      Enabled: true
      AutoCorrect: true
    Style/Documentation:
      Enabled: false
    Style/FrozenStringLiteralComment:
      Enabled: true
    Style/ExponentialNotation:
      Enabled: true
    Style/HashEachMethods:
      Enabled: true
    Style/HashTransformKeys:
      Enabled: true
    Style/HashTransformValues:
      Enabled: true
    Style/HashSyntax:
      Enabled: true
    Style/RedundantAssignment:
      Enabled: true
    Style/RedundantFetchBlock:
      Enabled: true
    Style/RedundantRegexpCharacterClass:
      Enabled: true
    Style/RedundantRegexpEscape:
      Enabled: true
    Style/SlicingWithRange:
      Enabled: true
    Style/StringLiterals:
      Enabled: true
    Rails:
      Enabled: true
  YAML
end

create_bin 'rubocop', 'rubocop'
create_bin 'rubocop_fix', 'rubocop --auto-correct-all'
Comments

Sign up or Login to leave a comment.