Use respond_to? instead of try

Usually when we use `try` we actually mean `try!`, which is the same as
the safe navigation operator. However, there are a few cases where we
actually mean to execute a method if the object responds to that method.

In those cases using `try` would actually be OK, but in order to avoid
confusion as to whether we mean to check for `respond_to?` or we mean to
use safe navigation, I'm removing all usages of `try`.
This commit is contained in:
Javi Martín
2019-10-10 03:30:31 +02:00
parent 1004ac01f8
commit 38b7307450
3 changed files with 4 additions and 4 deletions

View File

@@ -25,7 +25,7 @@ module WelcomeHelper
end
def calculate_image_path(recommended, image_default)
if recommended.try(:image) && recommended.image.present? && recommended.image.attachment.exists?
if recommended.respond_to?(:image) && recommended.image.present? && recommended.image.attachment.exists?
recommended.image.attachment.send("url", :medium)
elsif image_default.present?
image_default

View File

@@ -120,7 +120,7 @@ class Comment < ApplicationRecord
end
def call_after_commented
commentable.try(:after_commented)
commentable.after_commented if commentable.respond_to?(:after_commented)
end
def self.body_max_length

View File

@@ -29,8 +29,8 @@ module Notifiable
def check_availability(resource)
resource.present? &&
resource.try(:hidden_at).nil? &&
resource.try(:retired_at).nil?
!(resource.respond_to?(:hidden?) && resource.hidden?) &&
!(resource.respond_to?(:retired?) && resource.retired?)
end
def linkable_resource