Add comments to topics

This commit is contained in:
taitus
2017-08-07 15:05:36 +02:00
parent 7cf932490d
commit bea393bcde
8 changed files with 59 additions and 9 deletions

View File

@@ -1,8 +1,10 @@
class TopicsController < ApplicationController class TopicsController < ApplicationController
# include CommentableActions include CommentableActions
before_action :set_community before_action :set_community
has_orders %w{most_voted newest oldest}, only: :show
skip_authorization_check skip_authorization_check
def new def new
@@ -21,6 +23,9 @@ class TopicsController < ApplicationController
def show def show
@topic = Topic.find(params[:id]) @topic = Topic.find(params[:id])
@commentable = @topic
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
set_comment_flags(@comment_tree.comments)
end end
def edit def edit

View File

@@ -3,7 +3,7 @@ class Comment < ActiveRecord::Base
include HasPublicAuthor include HasPublicAuthor
include Graphqlable include Graphqlable
COMMENTABLE_TYPES = %w(Debate Proposal Budget::Investment Poll::Question Legislation::Question Legislation::Annotation).freeze COMMENTABLE_TYPES = %w(Debate Proposal Budget::Investment Poll::Question Legislation::Question Legislation::Annotation Topic).freeze
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases include ActsAsParanoidAliases

View File

@@ -1,4 +1,9 @@
class Topic < ActiveRecord::Base class Topic < ActiveRecord::Base
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
belongs_to :community belongs_to :community
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
has_many :comments, as: :commentable
end end

View File

@@ -0,0 +1,24 @@
<% cache [locale_and_user_status, @current_order, commentable_cache_key(@topic), @comment_tree.comments, @comment_tree.comment_authors, @topic.comments_count, @comment_flags] do %>
<div class="row comments">
<div id="comments" class="small-12 column">
<%= render 'shared/wide_order_selector', i18n_namespace: "comments" %>
<% if user_signed_in? %>
<%= render 'comments/form', {commentable: @topic, parent_id: nil, toggeable: false} %>
<% else %>
<br>
<div data-alert class="callout primary">
<%= t("topics.show.login_to_comment",
signin: link_to(t("votes.signin"), new_user_session_path),
signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %>
</div>
<% end %>
<% @comment_tree.root_comments.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<% end %>
<%= paginate @comment_tree.root_comments %>
</div>
</div>
<% end %>

View File

@@ -6,10 +6,12 @@
<% topics.each do |topic| %> <% topics.each do |topic| %>
<div id="<%= dom_id(topic) %>"> <div id="<%= dom_id(topic) %>">
<%= link_to topic.title, community_topic_path(@community, topic) %> <%= link_to topic.title, community_topic_path(@community, topic) %>
<br> <p class="topic-info">
<%= link_to t("proposals.proposal.comments", count: topic.comments_count), community_topic_path(@community, topic, anchor: "comments") %>
<%= topic.author.name %> <%= topic.author.name %>
<br>
<%= I18n.l topic.created_at %> <%= I18n.l topic.created_at %>
</p>
<% if topic.author == current_user %> <% if topic.author == current_user %>
<%= link_to t("topic.edit"), edit_community_topic_path(@community.id, topic), class: 'button expanded' %> <%= link_to t("topic.edit"), edit_community_topic_path(@community.id, topic), class: 'button expanded' %>
<% end %> <% end %>

View File

@@ -3,3 +3,12 @@
Comunidad: <%= @community.proposal.title %> Comunidad: <%= @community.proposal.title %>
<br> <br>
<%= @topic.title %> <%= @topic.title %>
<div class="tabs-content" data-tabs-content="proposals-tabs" role="tablist">
<%#= render "proposals/filter_subnav" %>
<%#= render "proposals/notifications" %>
<div class="tabs-panel is-active" id="tab-comments">
<%= render "topics/comments" %>
</div>
</div>

View File

@@ -3,7 +3,9 @@ class CreateTopics < ActiveRecord::Migration
create_table :topics do |t| create_table :topics do |t|
t.string :title, null: false t.string :title, null: false
t.integer :author_id t.integer :author_id
t.integer "comments_count", default: 0
t.references :community, index: true t.references :community, index: true
t.datetime :hidden_at, index: true
t.timestamps null: false t.timestamps null: false
end end
end end

View File

@@ -893,12 +893,15 @@ ActiveRecord::Schema.define(version: 20170807082243) do
create_table "topics", force: :cascade do |t| create_table "topics", force: :cascade do |t|
t.string "title", null: false t.string "title", null: false
t.integer "author_id" t.integer "author_id"
t.integer "comments_count", default: 0
t.integer "community_id" t.integer "community_id"
t.datetime "hidden_at"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
add_index "topics", ["community_id"], name: "index_topics_on_community_id", using: :btree add_index "topics", ["community_id"], name: "index_topics_on_community_id", using: :btree
add_index "topics", ["hidden_at"], name: "index_topics_on_hidden_at", using: :btree
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "email", default: "" t.string "email", default: ""