progapandist / evil_chat

Code for "Evil Front: Modern Front-end in Rails" 3-part tutorial: https://evilmartians.com/chronicles/evil-front-part-1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Show if current user is message sender or receiver

cseelus opened this issue · comments

Nice blogpost, I really like the component-based approach without relying on any specific frontend framework.

How would you go about showing if a rendered message is received or sent?

Doing this for messages rendered through the ChatController show action is easy: Just add a variable for @current_user and pass it along _chat.html.erb, down to _message.html.erb. Then for example a class sent can be added to <div class="message"> which can be adequately styled:

screen shot 2018-01-18 at 7 47 24

The problem is the ChatChannel though, where the current_user is always the sending user.

You can probably come up with the approach to differentiate sent and received messages in the real time, but that is not the real problem in my view — the real limitation of this example (because it's not about building chats, but about organizing application front-end in general) is that we only have one Message model in the database and no persistence for users, so after a page refresh there will be no way to distinguish messages on sender.

I don't think this example is worth expanding to cover that specific feature request — if you are developing a real chat in Rails, you're better off creating a User model and associating Users with messages. Does it answer your question?

The mockup for @current_user I used in the Controller was to just use session[:username], which (only) works after page refresh, the problem was associating the messages sent by the ChatChannel.

But you are probably right in that this is beyond the scope of this tutorial. I probably got carried away as building a (very basic) chat was unexpectedly simple with this whole approach, imho simpler than with the official Rails guides ;-)

When dealing with ActionCable I find that it's good to always ask yourself what will be the state of your page after the full page refresh, then you do whatever you need to build it, and only after think how to "dynamize" it with ActionCable magic :)

Yep, I find that makes sense with all parts of frontend applications.