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

Add new Rails/RootJoinChain cop #586

Merged
merged 1 commit into from
Nov 28, 2021

Conversation

leoarnold
Copy link
Contributor

Instead of chaining #join on Rails.root or Rails.public_path

Rails.root.join('db').join('schema.rb')
Rails.root.join('db').join(migrate).join('migration.rb')
Rails.public_path.join('path').join('file.pdf')
Rails.public_path.join('path').join(to).join('file.pdf')

we can combine all arguments into a single join

Rails.root.join('db', 'schema.rb')
Rails.root.join('db', migrate, 'migration.rb')
Rails.public_path.join('path', 'file.pdf')
Rails.public_path.join('path', to, 'file.pdf')

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@leoarnold leoarnold force-pushed the leoarnold/rails-root-join-chain branch from cd98870 to 8e77876 Compare November 21, 2021 05:18
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Nov 21, 2021
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
@leoarnold leoarnold force-pushed the leoarnold/rails-root-join-chain branch from 8e77876 to 5a867ab Compare November 21, 2021 06:01
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Nov 21, 2021
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
@leoarnold leoarnold force-pushed the leoarnold/rails-root-join-chain branch from 5a867ab to af7d2a1 Compare November 21, 2021 07:04
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

lib/rubocop/cop/rails/root_join_chain.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/root_join_chain.rb Outdated Show resolved Hide resolved
@leoarnold leoarnold force-pushed the leoarnold/rails-root-join-chain branch from af7d2a1 to 1f7a089 Compare November 21, 2021 14:38
lib/rubocop/cop/rails_cops.rb Outdated Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@leoarnold leoarnold force-pushed the leoarnold/rails-root-join-chain branch 2 times, most recently from 95c2fe4 to d6dc624 Compare November 23, 2021 02:38
Instead of chaining `#join` on `Rails.root` or `Rails.public_path`

```ruby
Rails.root.join('db').join('schema.rb')
Rails.root.join('db').join(migrate).join('migration.rb')
Rails.public_path.join('path').join('file.pdf')
Rails.public_path.join('path').join(to).join('file.pdf')
```

we can combine all arguments into a single join

```ruby
Rails.root.join('db', 'schema.rb')
Rails.root.join('db', migrate, 'migration.rb')
Rails.public_path.join('path', 'file.pdf')
Rails.public_path.join('path', to, 'file.pdf')
```
@leoarnold leoarnold force-pushed the leoarnold/rails-root-join-chain branch from d6dc624 to 6eae52a Compare November 23, 2021 03:05
@koic koic merged commit baac68e into rubocop:master Nov 28, 2021
@koic
Copy link
Member

koic commented Nov 28, 2021

Thanks!

leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 16, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 16, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 16, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 16, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 16, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 16, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 16, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 16, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 17, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 17, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 17, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 17, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
leoarnold added a commit to leoarnold/rubocop-rails that referenced this pull request Jul 17, 2022
`Rails.root` is an instance of `Pathname`. So instead of

```ruby
File.open(Rails.root.join('db', 'schema.rb'))
File.open(Rails.root.join('db', 'schema.rb'), 'w')
File.read(Rails.root.join('db', 'schema.rb'))
File.binread(Rails.root.join('db', 'schema.rb'))
File.write(Rails.root.join('db', 'schema.rb'), content)
File.binwrite(Rails.root.join('db', 'schema.rb'), content)
```

we can simply write

```ruby
Rails.root.join('db', 'schema.rb').open
Rails.root.join('db', 'schema.rb').open('w')
Rails.root.join('db', 'schema.rb').read
Rails.root.join('db', 'schema.rb').binread
Rails.root.join('db', 'schema.rb').write(content)
Rails.root.join('db', 'schema.rb').binwrite(content)
```

This cop works best when used together with
[`Style/FileRead`](rubocop/rubocop#10261),
[`Style/FileWrite`](rubocop/rubocop#10260),
and [`Rails/RootJoinChain`](rubocop#586).
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

Successfully merging this pull request may close these issues.

3 participants