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/RootPathnameMethods cop #587

Merged
merged 1 commit into from Jul 19, 2022

Commits on Jul 17, 2022

  1. Add new Rails/RootPathnameMethods cop

    `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 committed Jul 17, 2022
    Copy the full SHA
    467da4e View commit details
    Browse the repository at this point in the history