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
# ....
  private

  def current_account
    current_reseller || current_customer
  end
end

And to ApplicationHelper

module ApplicationHelper
  def current_account
    current_reseller || current_customer
  end

  def current_account_signed_in?
    reseller_signed_in? || customer_signed_in?
  end
end

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
  hr
  .row.text-center
    .col-md-4.col-md-offset-2
      = link_to 'Register as reseller', :new_reseller_registration,
        class: 'btn btn-primary'
      hr
      = link_to 'Sign In as reseller', :new_reseller_session,
        class: 'btn btn-primary'
    .col-md-4
      = link_to 'Register as Customer', :new_customer_registration,
        class: 'btn btn-success'
      hr
      = 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 %>
</div>

app/assets/stylesheets/welcome.scss

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

nav.navbar.navbar-default
  .container-fluid
    .navbar-header
      button.navbar-toggle.collapsed aria-controls="navbar" aria-expanded="false" data-target="#navbar" data-toggle="collapse" type="button"
        span.sr-only Toggle navigation
        span.icon-bar
        span.icon-bar
        span.icon-bar
      = link_to 'Shop', :root, class: 'navbar-brand'
    #navbar.navbar-collapse.collapse
      - 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'
ul.nav.navbar-nav.navbar-right
  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

ul.nav.navbar-nav
  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
  end
end
// => resellers/index.html.slim

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

Make the same with Customer (controller and index view)

Let’s create a few resellers and customers

go to rails c

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

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

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

Customer.create(email: 'customer_fred@gmail.com', 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.

PART 2