First off, I would also strongly suggest you avoid the and
keyword and prefer &&
unless you have a very specific reason for favoring and
over &&
. Look here if you're interested in seeing why a lot of ruby programmers think this way.
That being said, the easiest way to clean up big conditionals like this without much refactoring is by assigning intermediary variables (optimize for readability):
persisted = @tool_cvt_video_info_test.save
firmware = check_and_update_firmware_model_and_version(params["camera_ip"])
saved_upload = save_uploaded(params[:videos])
if persisted && firmware && saved_upload
format.html { redirect_to @tool_cvt_video_info_test, notice: 'Video info test was successfully created.' }
format.json { render action: 'show', status: :created, location: @tool_cvt_video_info_test }
else
format.html { render action: 'new' }
format.json { render json: @tool_cvt_video_info_test.errors, status: :unprocessable_entity }
end
I think that makes for a bit more readable code, but I'd agree with other comments in this thread that this block of code seems to be doing too much. I'd suggest seeing if you can separate the logic used to validate and persist this data from the places you determine the controller response.
Cleaner separation of purpose will make it easier to avoid things like temporary variables and make it easier to spot places where complex logic can be sequestered into a private method (for instance, a method called save_or_update_cvt_video_info(params)
)