Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 1 addition & 138 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,138 +1 @@
# This file contains NDR's agreed deviations from the standard Ruby
# Style Guide, the configuration for which can be found here:
# https://github.com/bbatsov/rubocop/blob/master/config/default.yml
#
# See the README for instructions on using in a project.

require:
- rubocop-rails

AllCops:
# All cops should ignore files in the following locations:
Exclude:
- 'bin/*'
- 'db/schema.rb'
- 'lib/generators/**/templates/*'
- 'tmp/**/*'
- 'vendor/**/*'

# Once supported by RuboCop, this will prevent cop-specific Excludes from
# overwriting the AllCops defaults above:
#
# inherit_mode:
# merge:
# - Exclude

# Run the Rails cops by default (-R/--rails not required):
Rails:
Enabled: true

##################### Layout #################################

Layout/DotPosition:
# Multi-line method chaining should be done with trailing dots.
EnforcedStyle: trailing

Layout/HashAlignment:
EnforcedColonStyle:
- key
- table
EnforcedHashRocketStyle:
- key
- table

##################### Style ##################################

# We make use of block comments, e.g. for validation documentation.
Style/BlockComments:
Enabled: false

Style/Documentation:
Exclude:
- 'test/**/*.rb'

Style/FrozenStringLiteralComment:
# We're not confident enough to make this recommendation everywhere
Enabled: false

Style/ModuleFunction:
# `extend self` has fewer side effects than `module_function`.
EnforcedStyle: extend_self

Style/NumericLiterals:
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Style/YodaCondition:
# Disagree; literals as first argument can guard against accidental assignment.
Enabled: false

Style/SingleLineBlockParams:
# Prefer readability of contextually-named variables.
Enabled: false

##################### Metrics ##################################
# NOTE: we bump some metrics by 50%, in recognition of Rubocop's
# desire to split that which we would consider atomic.

Metrics/AbcSize:
Max: 23
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Metrics/BlockLength:
# We're already limiting method size, blocks outside of methods
# can legitimately be long (RuboCop defaults to max: 25 lines).
Enabled: false

Metrics/ClassLength:
Max: 150
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Metrics/ModuleLength:
Max: 150
Exclude:
- 'test/**/*.rb'

Metrics/CyclomaticComplexity:
Max: 9

Layout/LineLength:
Max: 100
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Metrics/MethodLength:
Max: 15
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Metrics/PerceivedComplexity:
Max: 12

##################### Rails ##################################

Rails/ActionFilter:
# 'action' is the default already; we explicitly list here as Rails 3.x
# projects will want to override this configuration to use 'filter' instead.
EnforcedStyle: action

Rails/DynamicFindBy:
Exclude:
- 'test/integration/**/*.rb'

Rails/RefuteMethods:
Enabled: false

Rails/SkipsModelValidations:
# Methods like 'update_column' exist for a reason, and it is the developer's
# responsibilty to understand the behaviour of the code they write; blanket
# avoiding them is not helpful/practical.
Enabled: false

require: ndr_dev_support
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## [Unreleased]
*no unreleased changes*

### Added
* bundle master RuboCop config, and allow it to be `required`

## 5.4.8 / 2020-01-24
### Fixed
* deploy: insert temporary DB config to allow asset precompilation
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,22 @@ For more details of the audit tasks available, execute:

### RuboCop configuration

ndr_dev_support includes tweaks to the default Ruby Style Guide, to better suit NDR.
To use this updated style guide from within a project, add the following to top of the project's `.rubocop.yml` file:
`ndr_dev_support` includes tweaks to the default Ruby and Ruby on Rails Style Guides, to better suit NDR.
To use these updated style guides from within a project, add the following to top of the project's `.rubocop.yml` file:

```yaml
inherit_from: 'https://raw.githubusercontent.com/PublicHealthEngland/ndr_dev_support/master/.rubocop.yml'
require: ndr_dev_support
```

RuboCop also allows `inherit_gem`, but this currently doesn't work with relative paths (paths are deemed relative to the config file, rather than the project including `ndr_dev_support`).
Or, if using other extensions too:

In order for these configuration to apply, you will need to invoke RuboCop using Bundler:
```yaml
require:
- ndr_dev_support
- some_other_extension
```

In order for these configurations to apply, you will need to invoke RuboCop using Bundler:

```
$ bundle exec rubocop .
Expand Down
138 changes: 138 additions & 0 deletions config/rubocop/ndr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# This file contains NDR's agreed deviations from the standard Ruby
# Style Guide, the configuration for which can be found here:
# https://github.com/bbatsov/rubocop/blob/master/config/default.yml
#
# See the README for instructions on using in a project.

require:
- rubocop-rails

AllCops:
# All cops should ignore files in the following locations:
Exclude:
- 'bin/*'
- 'db/schema.rb'
- 'lib/generators/**/templates/*'
- 'tmp/**/*'
- 'vendor/**/*'

# Once supported by RuboCop, this will prevent cop-specific Excludes from
# overwriting the AllCops defaults above:
#
# inherit_mode:
# merge:
# - Exclude

# Run the Rails cops by default (-R/--rails not required):
Rails:
Enabled: true

##################### Layout #################################

Layout/DotPosition:
# Multi-line method chaining should be done with trailing dots.
EnforcedStyle: trailing

Layout/HashAlignment:
EnforcedColonStyle:
- key
- table
EnforcedHashRocketStyle:
- key
- table

##################### Style ##################################

# We make use of block comments, e.g. for validation documentation.
Style/BlockComments:
Enabled: false

Style/Documentation:
Exclude:
- 'test/**/*.rb'

Style/FrozenStringLiteralComment:
# We're not confident enough to make this recommendation everywhere
Enabled: false

Style/ModuleFunction:
# `extend self` has fewer side effects than `module_function`.
EnforcedStyle: extend_self

Style/NumericLiterals:
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Style/YodaCondition:
# Disagree; literals as first argument can guard against accidental assignment.
Enabled: false

Style/SingleLineBlockParams:
# Prefer readability of contextually-named variables.
Enabled: false

##################### Metrics ##################################
# NOTE: we bump some metrics by 50%, in recognition of Rubocop's
# desire to split that which we would consider atomic.

Metrics/AbcSize:
Max: 23
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Metrics/BlockLength:
# We're already limiting method size, blocks outside of methods
# can legitimately be long (RuboCop defaults to max: 25 lines).
Enabled: false

Metrics/ClassLength:
Max: 150
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Metrics/ModuleLength:
Max: 150
Exclude:
- 'test/**/*.rb'

Metrics/CyclomaticComplexity:
Max: 9

Layout/LineLength:
Max: 100
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Metrics/MethodLength:
Max: 15
Exclude:
- 'db/migrate/*.rb'
- 'test/**/*.rb'

Metrics/PerceivedComplexity:
Max: 12

##################### Rails ##################################

Rails/ActionFilter:
# 'action' is the default already; we explicitly list here as Rails 3.x
# projects will want to override this configuration to use 'filter' instead.
EnforcedStyle: action

Rails/DynamicFindBy:
Exclude:
- 'test/integration/**/*.rb'

Rails/RefuteMethods:
Enabled: false

Rails/SkipsModelValidations:
# Methods like 'update_column' exist for a reason, and it is the developer's
# responsibilty to understand the behaviour of the code they write; blanket
# avoiding them is not helpful/practical.
Enabled: false

5 changes: 4 additions & 1 deletion lib/ndr_dev_support.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'ndr_dev_support/rubocop/inject'
require 'ndr_dev_support/version'

module NdrDevSupport
# Your code goes here...
# Bootstrap our RuboCop config in to any project
# when ndr_dev_support is required in .rubocop.yml.
Rubocop::Inject.defaults!
end
26 changes: 26 additions & 0 deletions lib/ndr_dev_support/rubocop/inject.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rubocop'

module NdrDevSupport
module Rubocop
# Following approach of rubocop-hq/rubocop-extension-generator,
# monkey-patch in default configuration.
module Inject
def self.defaults!
root = Pathname.new(__dir__).parent.parent.parent.expand_path
path = root.join('config', 'rubocop', 'ndr.yml').to_s

# Whereas by default, the raw YAML would be processed, we pass
# through the ConfigLoader fully - this ensures `require` and
# `inherit_from` statements are properly evaluated.
#
# PR at rubocop-hq/rubocop-extension-generator/pull/9
#
config = ::RuboCop::ConfigLoader.load_file(path)
puts "configuration from \#{path}" if ::RuboCop::ConfigLoader.debug?

config = ::RuboCop::ConfigLoader.merge_with_default(config, path)
::RuboCop::ConfigLoader.instance_variable_set(:@default_configuration, config)
end
end
end
end