alt text

Create messaging system between Reseller and Customer.

Denshobato Github Repository

Part 1

Denshobato is a Rails gem that helps models communicate with each other. It gives simple api for creating a complete conversation system. You can create conversation with any model. Denshobato provides api methods for making conversation, messages, blacklists and trash. It also provides Helper methods for controller and view.

In this tutorial, we’ll create messaging system from scratch. A most part of work Denshobato gem will take over.

$ rails new shop

Add bootstrap-sass gem for styles, Devise for quick users authentication, slim-rails for templates, and Denshobato for messaging.

gem 'bootstrap-sass'
gem 'devise'
gem 'slim-rails'
gem 'denshobato'

run bundle install

Install Devise

$ rails g devise:install

$ rails g devise Reseller first_name:string last_name:string

$ rails g devise Customer first_name:string last_name:string

And install Denshobato

$ rails g denshobato:install

Now run rake db:migrate

Next, we need to set up a basic authentication for reseller and customer model.

Add this helpful method to ApplicationController

class ApplicationController < ActionController::Base
# ....

  def current_account
    current_reseller || current_customer

And to ApplicationHelper

module ApplicationHelper
  def current_account
    current_reseller || current_customer

  def current_account_signed_in?
    reseller_signed_in? || customer_signed_in?

Generate WelcomeController, with home action for root path.

rails g controller Welcome home

Add route to root path. routes.rb

root 'welcome#home'

Add this to welcome/home.html.slim

h1.text-danger.text-center Welcome to the Shop!
- if current_account_signed_in?
  h2.text-success.text-center = "Hello, #{current_account.try(:email)}"
- else
      = link_to 'Register as reseller', :new_reseller_registration,
        class: 'btn btn-primary'
      = link_to 'Sign In as reseller', :new_reseller_session,
        class: 'btn btn-primary'
      = link_to 'Register as Customer', :new_customer_registration,
        class: 'btn btn-success'
      = link_to 'Sign In as Customer', :new_customer_session,
        class: 'btn btn-success'

Next, add header to our application layout and import bootstrap styles, if you haven`t done it yet.

<div class="container">
  <%= render 'layouts/header' %>
  <%= yield %>


  @import 'bootstrap';
// => _header.html.slim

      button.navbar-toggle.collapsed aria-controls="navbar" aria-expanded="false" data-target="#navbar" data-toggle="collapse" type="button" Toggle navigation
      = link_to 'Shop', :root, class: 'navbar-brand'
      - if current_account_signed_in?
        = render 'layouts/account_header'

As long as we have two different models, for sake of brevity, we’ll use devise_url_helper, which generates correct route, obviously based on your model. name.

// => 'layouts/_account_header.html.slim'

= render 'layouts/links'
  li = link_to 'Edit Profile',
    devise_url_helper(:edit, current_account, :registration)
  li = link_to 'Log out',
    devise_url_helper(:destroy, current_account, :session), method: :delete

In Links partial

// => layouts/_links.html.slim

  li = link_to 'Home',  :root
  li = link_to 'Resellers', :resellers
  li = link_to 'Customers', :customers

It should look like this alt text

Next, we create the reseller’s controller and customer’s controller.

Don`t forget about routes

resources :resellers, :customers
class ResellersController < ApplicationController
  def index
    @resellers = Reseller.all
// => resellers/index.html.slim

- @resellers.each do |reseller|
  = link_to, reseller if current_account != reseller

Make the same with Customer (controller and index view)

Let’s create a few resellers and customers

go to rails c

Reseller.create(email: '', password: 'password123',
password_confirmation: 'password123', first_name: 'John', last_name: 'Doe')

Reseller.create(email: '', password: 'password123',
password_confirmation: 'password123', first_name: 'Steve', last_name: 'Smith')

Customer.create(email: '', password: 'password123',
password_confirmation: 'password123', first_name: 'Mark', last_name: 'Paul')

Customer.create(email: '', password: 'password123',
password_confirmation: 'password123', first_name: 'Fred', last_name: 'Munch')

Okay, looks like we bootstraped everything we need at this point.

We ready to set up messaging system for our Reseller and Customer.