Patch #44201
openUse && instead of & in Attachment#move_to_target_directory! and add test coverage
Description
The guard clause in Attachment#move_to_target_directory! (app/models/attachment.rb)
uses the bitwise, non-short-circuiting operator & instead of the logical &&:
def move_to_target_directory!
return unless !new_record? & readable?
...
For boolean operands the result is the same, but & does not short-circuit, so
readable? — which performs a filesystem check via File.readable? — is always
evaluated, even for new records. Using && is the idiomatic choice and restores
short-circuit evaluation.
This patch:
- Replaces & with && in the guard clause.
- Adds unit tests for move_to_target_directory!, which previously had no direct
test coverage: one verifying that a readable, persisted attachment is moved to
its target directory, and one verifying that the method does nothing for a new
record.
Tested against current trunk:
- bundle exec rails test test/unit/attachment_test.rb passes
- bundle exec rubocop reports no offenses
A patch is attached.
Files