Commit 097b6285 authored by Simon Sölder's avatar Simon Sölder
Browse files

Add initial messaging code - WIP

parent 36367c95
# frozen_string_literal: true
class ApplicationController < ActionController::Base
before_action :authenticate_user!
# frozen_string_literal: true
class ConversationController < ApplicationController
before_action :set_conversation, only: %w[show create new]
before_action :check_engaged, only: %w[show create new]
def index
@conversations = Conversation.engaged(current_user).order('updated_at DESC')
def show; end
def create; end
def new; end
def set_conversation
@conversation = Conversation.find(params[:id])
# @conversation = Conversation.find_by(id: params[:id])
def check_engaged
redirect_to root_path unless @conversation.exists? && @conversation.engages?(current_user)
......@@ -4,7 +4,6 @@
class PostsController < ApplicationController
before_action :set_post, only: %w[show edit update destroy]
before_action :initialize_all_tags, only: %w[index create update]
# before_action :authenticate_user!
# GET /posts
# GET /posts.json
# frozen_string_literal: true
module ConversationHelper
# frozen_string_literal: true
class Conversation < ApplicationRecord
validates :author, uniqueness: { scope: :receiver }
belongs_to :author, class_name: 'User'
belongs_to :receiver, class_name: 'User'
has_many :conversation_messages, -> { order('created_at: :asc') }, dependent: :destroy, inverse_of: :conversation
# get all conversations that the user is engaged in
scope :engaged, lambda { |user|
where('(conversations.author_id = ? OR conversations.receiver_id = ?)',,
# get the conversation partner
def conversation_partner(user)
if author.equals user
# check if user engages in a conversation
def engages?(user)
author == user or receiver == user
# frozen_string_literal: true
class ConversationMessage < ApplicationRecord
validates :content, presence: true
belongs_to :conversation, inverse_of: :conversation_message
belongs_to :user
......@@ -8,6 +8,9 @@ class User < ApplicationRecord
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :authored_conversations, class_name: 'Conversation', foreign_key: 'author_id'
has_many :received_conversations, class_name: 'Conversation', foreign_key: 'received_id'
has_many :conversation_messages, dependent: :destroy
has_one_attached :avatar
# frozen_string_literal: true
json.extract! comment, :id, :created_at, :updated_at
json.url comment_url(comment, format: :json)
<%= form_with(model: comment, local: true) do |form| %>
<% if comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<% comment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
<% end %>
<div class="actions">
<%= form.submit %>
<% end %>
<h1>Editing Comment</h1>
<%= render 'form', comment: @comment %>
<%= link_to 'Show', @comment %> |
<%= link_to 'Back', comments_path %>
<p id="notice"><%= notice %></p>
<th colspan="3"></th>
<% @comments.each do |comment| %>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
<%= link_to 'New Comment', new_comment_path %>
# frozen_string_literal: true
json.array! @comments, partial: 'comments/comment', as: :comment
<h1>New Comment</h1>
<%= render 'form', comment: @comment %>
<%= link_to 'Back', comments_path %>
<p id="notice"><%= notice %></p>
<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>
# frozen_string_literal: true
json.partial! 'comments/comment', comment: @comment
<p>Find me in app/views/conversation/create.html.erb</p>
<p>Find me in app/views/conversation/index.html.erb</p>
<p>Find me in app/views/conversation/new.html.erb</p>
<p>Find me in app/views/conversation/show.html.erb</p>
......@@ -15,4 +15,4 @@ Rails.application.config.assets.paths << Rails.root.join('node_modules')
# folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css )
Rails.application.config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
\ No newline at end of file
Rails.application.config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment