Can a null object be used in `link_to`

Is there a practical and not too-convoluted way to implement null objects that can be used in the link_to helper on par with their “real” counterparts? I’m specifically thinking about associations, so that if there is

class Sponsor
  has_many :sponsorships

  def most_recent_active_sponsorship ||

then in the view it would be possible to get rid of the if clause:

<p>Most recent active sponsorship:</p>
<% sponsorship = @sponsor.most_recent_active_sponsorship %>
<%# if sponsorship %>
  <%= link_to, sponsorship %>
<%# end %>

Using the null object in the link_to should render the return value of its #name method (e.g. “none”) as something clearly unclickable.

You can use the presenter pattern for this sort of thing. Check out this blog post for an example:

Probably overkill if this is a one-off scenario, if that’s the case I would suggest moving the logic into a helper method if you wish to clean up the view some.

I had this exact need a few days ago, and even with @seangriffin’s help it turned out to be enough of a pain in the ass that I abandoned the refactoring.

Yeah, the short answer is yes a null object can be used, but link_to is still going to want to call a URL helper method, so you’d need to define a null_path and null_url helper method that could be used.

Thanks for the answers, everyone!