In PART 1 we built a basic app with reseller and customer models, devise authentication and index templates to list our models. In part 2 we’ll make the rest of our app.
In Part 2:
We’ll create conversations
Send messages to conversations
Send conversations to Trash
Add users to BlackList
Go to Reseller and Customer Models.
Add denshobato_for :your_class to these models.
This method does a lot of things - sets up associations and adds useful methods for you.
Add it to your models.
Go to Resellers and Customers contoller.
Add to index action conversation builder for our form.
Add this form to your index page, which shows all your resellers and customers.
fill_conversation_form is a Denshobato view helper, it helps to create a conversation.
Don’t forget to add route for conversations resource :conversations
On a reseller’s page (if you’re signed as a reseller) you can see an extra button for creating conversation with yourself. There is also a useful helper which can hide this button.
Add this to both forms (for reseller and customer).
Okay, now if we click the button, we got an error uninitialized constant ConversationsController
Let`s create this contoller.
Done, we’re ready to create a form for messages.
First, add resources :messages to routes.rb
In ConversationController, show action add form for message.
We set url: :messages for correct resource path (by default it searches for :denshobato_messages)
fill_message_form is a Denshobato helper, it helps you to create a message form.
To show all messages for this conversation add this to the same view below. show.html.slim
Okay, when we try to create a message, we got an error, we don’t have MessagesController, so create it.
Notice: Don`t forget to send_notification to conversation after you save message, it sends notification both for you and recipient, so both of you get access to messages.
Now we’ll make our conversation list.
Open /layouts/_links.html.slim
Add to index action in ConversationContoller
And in index view
Now we can list all your conversations.
It is not safe now, because now everyone have an access to your conversations. So you need to go to conversation show action, and add this line
Notice: We use user_in_conversation?(current_account, @conversation) method in this example. This method checks if current_user presents in conversations.
If we go back to reseller’s or customer’s index page, we can still see start conversation button, even if the conversation is already started. Let`s hide it.
In our index.html.slim templates for reseller and customer, use conversation_exists? method.
Good, now go to conversation index page, we’ll use some view helpers to show our recipient’s name and avatar. You will also see the last message of a shown conversation.
It should look like this.
Of course, your models should have an image url, and at least one message in conversation
Next, go into a conversation and use this helpers to make it look better.
Open conversation/show.html.slim and replace messages with formatted outp
Great! We are almost in the end, but we have to do two more features - send conversation to trash and ignore users.
We start with trash feature.
Trash
First, create buttons for these actions.
Again, open your conversation index view and add the button
Add route
And action
Add to index action our trashed conversations
And back to our index view, add this under conversations.
As you can see, we add ‘Move from Trash button’
Define route for this action
Add an action for it. As longs as we have two similar actions we can DRYing it by ruby define_method, like this.
Great, it works!
BlackList
We have one last thing to do, it`s a blacklist.
You can add model to your blacklist, blocked model can’t start conversation with you or send messages, and vice versa, if you want to send a message to this model, remove it from blacklist.
Looks terrible. Here is an advice for you: move some logic to helpers or decorators.
Okay, we added these two buttons - add to black list and remove, - and helpers to show or hide it.
Go to routes.rb and define routes for these actions.
Create BlacklistsController
Now you can create page with your blacklist.
For example, page in your blacklists_controller
We use define_method again for very similar methods.
Now, it looks as it should look. Feel free to customize it, e.g, add Mailer, when you send message.
When reseller sends message to customer, we can also send a notification to customer’s email. Like a “You have a new message from John Doe…” etc. If something is unclear, read documentation on the Denshobato repo