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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user