2
\$\begingroup\$

I've got below implementation for my controller and show action which implements MiniMagick gem. I've just wondering is there any better way to write it down? If block in a separate, private method maybe?

 def show
 identification_document = IdentificationDocument.find(params[:id])
 authorize identification_document
 return unless identification_document
 if params.has_key?(:thumbnail)
 document = identification_document.id_document
 image = MiniMagick::Image.read(document.file.read)
 image.resize("50x60")
 scaled_image_bytes = image.to_blob
 send_data(scaled_image_bytes, filename: identification_document.file_name)
 else 
 send_data(identification_document.id_document.file.read, filename: identification_document.file_name)
 end
 end
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 25, 2019 at 9:10
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Rubocop Report

Conventions

Prefer key? over has_key? (PreferredHashMethods).

if params.has_key?(:thumbnail)
if params.key?(:thumbnail)

Prefer single quoted over double quoted string literals if you don't need interpolation or special symbols (StringLiterals).

image.resize("50x60")
image.resize('50x60')

Complexity

If block in a separate, private method maybe?

The AbcSize complexity of your method is too high. [19.65/15] So your suggestion to put the following code in a private method, is justified.

 document = identification_document.id_document
 image = MiniMagick::Image.read(document.file.read)
 image.resize("50x60")
 scaled_image_bytes = image.to_blob

Readability

Keep line sizes below 80 characters:

send_data(identification_document.id_document.file.read, filename: identification_document.file_name)
answered Aug 2, 2019 at 16:39
\$\endgroup\$
4
  • \$\begingroup\$ The under 80 chars rule is outdated. Screens have gotten significantly bigger and more pixels are available. There should definitely be a max char length enforced, but it can easily be 120 or 150 depending on the developers setups in an organization. \$\endgroup\$ Commented Aug 4, 2019 at 6:54
  • \$\begingroup\$ Single quote vs double quote is against the rails convention. Rails convention is double quotes all around, so I would suggest keeping the double quotes. \$\endgroup\$ Commented Aug 4, 2019 at 6:56
  • \$\begingroup\$ @ekampp Thanks for the feedback, you have a link to more recent guidelines, or are you talking out of personal experience? \$\endgroup\$ Commented Aug 4, 2019 at 8:11
  • \$\begingroup\$ regarding the 80 char rule, that's from experience. The Rails convention if from reading the Rails documentation and source code. \$\endgroup\$ Commented Aug 5, 2019 at 8:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.