Add description as comment on topic.

This commit is contained in:
taitus
2017-08-07 15:48:43 +02:00
parent d8b4323555
commit bc95b6f28d
5 changed files with 17 additions and 5 deletions

View File

@@ -44,7 +44,7 @@ class TopicsController < ApplicationController
private private
def topic_params def topic_params
params.require(:topic).permit(:title, :community_id) params.require(:topic).permit(:title, :community_id, :description_as_comment)
end end
def set_community def set_community

View File

@@ -6,4 +6,12 @@ class Topic < ActiveRecord::Base
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 has_many :comments, as: :commentable
after_create :associate_comment
private
def associate_comment
Comment.create(commentable: self, user: self.author, body: self.description_as_comment)
end
end end

View File

@@ -6,6 +6,8 @@
<div class="small-12 column"> <div class="small-12 column">
<%= f.label :title, t("topic.form.topic_title") %> <%= f.label :title, t("topic.form.topic_title") %>
<%= f.text_field :title %> <%= f.text_field :title %>
<%= f.label :description_as_comment, t("topic.form.topic_description_as_comment") %>
<%= f.text_area :description_as_comment, maxlength: Comment.body_max_length %>
</div> </div>
<div class="actions small-12 column"> <div class="actions small-12 column">

View File

@@ -2,6 +2,7 @@ class CreateTopics < ActiveRecord::Migration
def change def change
create_table :topics do |t| create_table :topics do |t|
t.string :title, null: false t.string :title, null: false
t.text :description_as_comment
t.integer :author_id t.integer :author_id
t.integer "comments_count", default: 0 t.integer "comments_count", default: 0
t.references :community, index: true t.references :community, index: true

View File

@@ -892,6 +892,7 @@ 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.text "description_as_comment"
t.integer "author_id" t.integer "author_id"
t.integer "comments_count", default: 0 t.integer "comments_count", default: 0
t.integer "community_id" t.integer "community_id"