Skip to content

Instantly share code, notes, and snippets.

@sunkibaek
Created March 13, 2016 08:07
Show Gist options
  • Save sunkibaek/84c830c4329569d74679 to your computer and use it in GitHub Desktop.
Save sunkibaek/84c830c4329569d74679 to your computer and use it in GitHub Desktop.
NullDateTime
class NullDateTime
def in_time_zone(_city = 'DefaultTimeZoneCity')
self
end
def to_s(_format = :default_date_time)
''
end
end
# app/models/order.rb
def completed?
completed_at?
end
# app/models/order.rb
def completed_at
super || NullDateTime.new
end
# app/views/orders/show.html.erb
order.completed_at.to_s(:short)
# app/views/orders/show.html.erb
<% if order.completed? %>
<%= order.completed_at.to_s(:short) %><br>
<%= order.completed_at.in_time_zone('Seoul') %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment