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`.
40 lines
813 B
Ruby
40 lines
813 B
Ruby
module Notifiable
|
|
extend ActiveSupport::Concern
|
|
|
|
def notifiable_title
|
|
case self.class.name
|
|
when "ProposalNotification"
|
|
proposal.title
|
|
when "Comment"
|
|
commentable.title
|
|
else
|
|
title
|
|
end
|
|
end
|
|
|
|
def notifiable_body
|
|
body if attribute_names.include?("body")
|
|
end
|
|
|
|
def notifiable_available?
|
|
case self.class.name
|
|
when "ProposalNotification"
|
|
check_availability(proposal)
|
|
when "Comment"
|
|
check_availability(commentable)
|
|
else
|
|
check_availability(self)
|
|
end
|
|
end
|
|
|
|
def check_availability(resource)
|
|
resource.present? &&
|
|
!(resource.respond_to?(:hidden?) && resource.hidden?) &&
|
|
!(resource.respond_to?(:retired?) && resource.retired?)
|
|
end
|
|
|
|
def linkable_resource
|
|
is_a?(ProposalNotification) ? proposal : self
|
|
end
|
|
end
|