Commit 38a2c3d6 authored by Simon Sölder's avatar Simon Sölder
Browse files

Revert "Merge branch 'feature/messages' into 'develop'"

This reverts merge request !9
parent 3ce99360
// Place all the styles related to the conversation controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
# frozen_string_literal: true
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
# frozen_string_literal: true
class ConversationsController < 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')
end
def show
@conversation_message = ConversationMessage.new
end
def create; end
def new; end
private
def set_conversation
@conversation = Conversation.find(params[:id])
# @conversation = Conversation.find_by(id: params[:id])
end
def check_engaged
redirect_to root_path unless @conversation.exists? && @conversation.engages?(current_user)
end
end
......@@ -4,6 +4,7 @@
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
end
# 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 = ?)', user.id, user.id)
}
# get the conversation partner
def conversation_partner(user)
if author.equals user
receiver
else
author
end
end
# check if user engages in a conversation
def engages?(user)
author == user or receiver == user
end
end
# frozen_string_literal: true
class ConversationMessage < ApplicationRecord
validates :content, presence: true
belongs_to :conversation, inverse_of: :conversation_message
belongs_to :user
end
......@@ -8,9 +8,6 @@ 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>
<ul>
<% comment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
<h1>Editing Comment</h1>
<%= render 'form', comment: @comment %>
<%= link_to 'Show', @comment %> |
<%= link_to 'Back', comments_path %>
<p id="notice"><%= notice %></p>
<h1>Comments</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @comments.each do |comment| %>
<tr>
<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>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= 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
Chatting with <%= conversation.with(current_user).name %>
<br>
<em><%= conversation.personal_messages.last.body.truncate(50) %></em>
<br>
<%= link_to 'View conversation', conversation_path(conversation) %>
<hr>
</div>
<h1>Conversation#create</h1>
<p>Find me in app/views/conversation/create.html.erb</p>
<h1>Your messages:</h1>
<div class="conversations">
<%= render @conversations %>
</div>
<h1>Conversation#new</h1>
<p>Find me in app/views/conversation/new.html.erb</p>
Markdown is supported
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