Created
April 17, 2015 01:06
-
-
Save alexdunae/de56344e36e19ca9729d to your computer and use it in GitHub Desktop.
matt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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