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/RootPublicPath cop #588

Closed

Conversation

leoarnold
Copy link
Contributor

Instead of

Rails.root.join('public')
Rails.root.join('public/file.pdf')
Rails.root.join('public', 'file.pdf')

we can use

Rails.public_path
Rails.public_path.join('file.pdf')
Rails.public_path.join('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-public-path branch from 6f726fc to d3b0bc9 Compare November 21, 2021 05:59
Instead of

```ruby
Rails.root.join('public')
Rails.root.join('public/file.pdf')
Rails.root.join('public', 'file.pdf')
```

we can use

```ruby
Rails.public_path
Rails.public_path.join('file.pdf')
Rails.public_path.join('file.pdf')
```
@leoarnold leoarnold force-pushed the leoarnold/rails-root-public-path branch from d3b0bc9 to ce65d64 Compare November 21, 2021 06:55

it "registers an offense when using `Rails.root.join('public/file.pdf')`" do
expect_offense(<<~RUBY)
Rails.root.join('public/file.pdf')
Copy link
Member

Choose a reason for hiding this comment

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

Will it detect "public/#{file}.pdf"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not yet. Will work on that.

Rails/RootPublicPath:
Description: "Favor `Rails.public_path` over `Rails.root` with `'public'`."
Enabled: pending
VersionAdded: '2.13'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
VersionAdded: '2.13'
VersionAdded: '<<next>>'


RESTRICT_ON_SEND = %i[join].to_set.freeze

PATTERN = %r{^public(/|$)}.freeze
Copy link
Member

Choose a reason for hiding this comment

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

\A and \z would be preferable if it is unexpecting a match to a multiline.

Suggested change
PATTERN = %r{^public(/|$)}.freeze
PATTERN = %r{\Apublic(/|\z)}.freeze

@@ -484,3 +484,4 @@
[@theunraveler]: https://github.com/theunraveler
[@pirj]: https://github.com/pirj
[@vitormd]: https://github.com/vitormd
[@leoarnold]: https://github.com/leoarnold
Copy link
Member

Choose a reason for hiding this comment

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

Account name will be auto-inserted, so it's better to remove it.

@koic
Copy link
Member

koic commented Dec 1, 2021

@leoarnold Can you rebase with the latest mater branch?

@pirj
Copy link
Member

pirj commented Dec 25, 2021

@leoarnold Ping

@koic
Copy link
Member

koic commented Jun 6, 2022

I've cherry-picked this PR into the master branch.
5fed4f6

The following can be extended later.
#588 (comment)

Thank you.

@koic koic closed this Jun 6, 2022
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.

None yet

3 participants