Skip to content
Permalink
Browse files

Add conversations

  • Loading branch information...
excid3 committed Jul 5, 2019
1 parent b2f0c73 commit 0ac6dbb9c4b9fbb9408611f329652c34f767534e
@@ -0,0 +1,3 @@
// Place all the styles related to the Conversations controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,84 @@
body {
background-color: #fff;
color: #333;
margin: 33px;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;

&:visited {
color: #666;
}

&:hover {
color: #fff;
background-color: #000;
}
}

th {
padding-bottom: 5px;
}

td {
padding: 0 5px 7px;
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0;

h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}

label {
display: block;
}
@@ -0,0 +1,75 @@
class ConversationsController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation, only: [:show, :edit, :update, :destroy]

# GET /conversations
# GET /conversations.json
def index
@conversations = Conversation.all
end

# GET /conversations/1
# GET /conversations/1.json
def show
end

# GET /conversations/new
def new
@conversation = Conversation.new
end

# GET /conversations/1/edit
def edit
end

# POST /conversations
# POST /conversations.json
def create
@conversation = Conversation.new(conversation_params)

respond_to do |format|
if @conversation.save
format.html { redirect_to @conversation, notice: 'Conversation was successfully created.' }
format.json { render :show, status: :created, location: @conversation }
else
format.html { render :new }
format.json { render json: @conversation.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /conversations/1
# PATCH/PUT /conversations/1.json
def update
respond_to do |format|
if @conversation.update(conversation_params)
format.html { redirect_to @conversation, notice: 'Conversation was successfully updated.' }
format.json { render :show, status: :ok, location: @conversation }
else
format.html { render :edit }
format.json { render json: @conversation.errors, status: :unprocessable_entity }
end
end
end

# DELETE /conversations/1
# DELETE /conversations/1.json
def destroy
@conversation.destroy
respond_to do |format|
format.html { redirect_to conversations_url, notice: 'Conversation was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_conversation
@conversation = Conversation.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def conversation_params
params.require(:conversation).permit(:user_id)
end
end
@@ -0,0 +1,26 @@
class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation

def create
@message = @conversation.messages.new message_params
@message.user = current_user

if @message.save
redirect_to @conversation, notice: "Message saved successfully"
else
redirect_to @conversation, alert: "Unable to save message"
end

end

private

def set_conversation
@conversation = Conversation.find(params[:conversation_id])
end

def message_params
params.require(:message).permit(:body)
end
end
@@ -0,0 +1,13 @@
class Webhooks::TwilioController < ApplicationController
skip_before_action :verify_authenticity_token

def create
user = User.find_by(phone: params[:From])
body = params[:Body]

conversation = Conversation.find_or_create_by(user: user)
conversation.messages.create(user: user, body: body)

head :ok
end
end
@@ -0,0 +1,2 @@
module ConversationsHelper
end
@@ -0,0 +1,4 @@
class Conversation < ApplicationRecord
belongs_to :user
has_many :messages, dependent: :destroy
end
@@ -0,0 +1,11 @@
class Message < ApplicationRecord
belongs_to :conversation
belongs_to :user

after_create :send_sms
def send_sms
return if user == conversation.user

TwilioClient.new.send_text(conversation.user, body)
end
end
@@ -0,0 +1,2 @@
json.extract! conversation, :id, :user_id, :created_at, :updated_at
json.url conversation_url(conversation, format: :json)
@@ -0,0 +1,34 @@
<%= form_with(model: conversation, local: true) do |form| %>
<% if conversation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(conversation.errors.count, "error") %> prohibited this conversation from being saved:</h2>

<ul>
<% conversation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="form-group">
<%= form.label :user_id %>
<%= form.collection_select :user_id, User.all, :id, :name, {}, { class: 'form-control' } %>
</div>

<div class="form-group">
<% if conversation.persisted? %>
<div class="float-right">
<%= link_to 'Destroy', conversation, method: :delete, class: "text-danger", data: { confirm: 'Are you sure?' } %>
</div>
<% end %>

<%= form.submit class: 'btn btn-primary' %>

<% if conversation.persisted? %>
<%= link_to "Cancel", conversation, class: "btn btn-link" %>
<% else %>
<%= link_to "Cancel", conversations_path, class: "btn btn-link" %>
<% end %>
</div>
<% end %>
@@ -0,0 +1,3 @@
<h1>Edit Conversation</h1>

<%= render 'form', conversation: @conversation %>
@@ -0,0 +1,43 @@



<div class="row">
<div class="col-sm-6">
<h1>Conversations</h1>
</div>

<div class="col-sm-6 text-right">
<%= link_to new_conversation_path, class: 'btn btn-primary' do %>
Add New Conversation
<% end %>
</div>
</div>

<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>


<th>User</th>

<th></th>

</tr>
</thead>

<tbody>
<% @conversations.each do |conversation| %>
<%= content_tag :tr, id: dom_id(conversation), class: dom_class(conversation) do %>


<td><%= conversation.user %></td>


<td><%= link_to 'Show', conversation %></td>

<% end %>
<% end %>
</tbody>
</table>
</div>
@@ -0,0 +1 @@
json.array! @conversations, partial: "conversations/conversation", as: :conversation
@@ -0,0 +1,3 @@
<h1>New conversation</h1>

<%= render 'form', conversation: @conversation %>
@@ -0,0 +1,13 @@
<div class="page-header">
<%= link_to conversations_path, class: 'btn btn-default' do %>
All Conversations
<% end %>
<%= link_to edit_conversation_path(@conversation), class: 'btn btn-primary' do %>
Edit
<% end %>
<h1><%= @conversation.user.name %></h1>
</div>

<%= render @conversation.messages %>

<%= render "messages/form", conversation: @conversation, message: Message.new %>
@@ -0,0 +1 @@
json.partial! "conversations/conversation", conversation: @conversation
@@ -0,0 +1,9 @@
<%= form_with model: [conversation, message] do |form| %>
<div class="form-group">
<%= form.text_area :body, class: "form-control" %>
</div>

<div class="form-group">
<%= form.submit %>
</div>
<% end %>
@@ -0,0 +1,4 @@
<div class="mb-4">
<div><%= message.user.name %></div>
<%= message.body %>
</div>
@@ -15,6 +15,7 @@

<div class="collapse navbar-collapse" id="navbarMain">
<ul class="navbar-nav mr-auto mt-2 mt-md-0">
<li class="nav-item"><%= link_to "Conversations", conversations_path, class: "nav-link" %></li>
</ul>

<ul class="navbar-nav">
@@ -0,0 +1 @@
eurYHoSGX+D9Rt2LzhjrsxC7XPuM44T6l7g5RiA7YohohOPAC1RUAbQgOrylh4BFgldjw+59K3zEGuoBpBgIzdQUXmoJFR4e66Bw4+tqesGFgtSahNeFFgwmVTKopsyLYGchxpmsZugOuvn4tT1ZmXZRbdMHWTdORxVS1YVv2H0uvwZoEHKfv5elLX8DnMYPqRHCLOiOrTqMTMakRbLBDZGR1s1cj5r5OvSsx5wKa4i3032g3wJmF4y6ogwVCp6i0/cvS4g=--IAEW0r7iJsuEaAOR--Ny6/9khEoofmL6prY7HqBA==
@@ -60,4 +60,6 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

config.hosts << "wicked-emu-66.localtunnel.me"
end
@@ -1,6 +1,14 @@
require 'sidekiq/web'

Rails.application.routes.draw do
namespace :webhooks do
resource :twilio, controller: :twilio, only: [:create]
end

resources :conversations do
resources :messages
end

namespace :admin do
resources :users

@@ -0,0 +1,9 @@
class CreateConversations < ActiveRecord::Migration[6.0]
def change
create_table :conversations do |t|
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end

0 comments on commit 0ac6dbb

Please sign in to comment.
You can’t perform that action at this time.