Skip to content

Instantly share code, notes, and snippets.

@secretpray
Created February 4, 2022 12:12
Show Gist options
  • Save secretpray/cce9b693bab60366339cd49df30c20c6 to your computer and use it in GitHub Desktop.
Save secretpray/cce9b693bab60366339cd49df30c20c6 to your computer and use it in GitHub Desktop.
# app/views/messages/_message.html.erb
<%= message.decorate.formatted_for_room %>
#app/decorators/message_decorator.rb

class MessageDecorator < Draper::Decorator
  delegate_all

  def formatted_for_room
    case created_at
    when DateTime.now.beginning_of_day..DateTime.now
      "#{message.created_at.strftime('%H:%M')} #{message.user.username}: #{message.content.camelize}"
    else
      "#{message.created_at.strftime('%b %d %Y %H:%M')} #{message.user.username}: #{message.content.camelize}"
    end

  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment