Skip to content

Instantly share code, notes, and snippets.

@alexdunae
Created April 17, 2015 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdunae/de56344e36e19ca9729d to your computer and use it in GitHub Desktop.
Save alexdunae/de56344e36e19ca9729d to your computer and use it in GitHub Desktop.
matt
cw = ChatWindow.new(user, other_user)
cw.avatar
cw.lasst_update
cw.messages.each do |m|
render m
end
cw.last_updated
ChatMessages.new(from: current_user, to: other_user)
# app/queries/chat_messages.rb
class ChatWindow
def initialize(user, other_user)
@user, @other_user = user, other_user
end
def last_updated
end
def avatar
other_user.avatar
end
def last_message
messages.last
end
def messages
@messages ||= ChatMessages.new(user, lther_user)
end
end
class ChatMessages
def initialize(user, other_user)
@user, @other_user = user, other_user
end
def messages
load_messages.map do |m|
MessagePresenter.new(m)
end
end
private
def load_messages
@messages ||= Message.where(user: user, other_user: other_user).includes(:user).limit(25).to_a
end
end
class MessagePresenter < SimpleDelegator
def style
from_current_user? ? 'current' : 'other'
end
def from_current_user?
# do some logic
end
def from_best_frend?
end
def from_blocked?
end
def how_long_ago
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment