Commit d999a05b by tady

comment機能試作

parent 80eba16a
......@@ -51,13 +51,13 @@
/* side tree view
-------------------------------------------------- */
ul {
/* ul {
padding-left: 1em;
}
*/
/* home#show
-------------------------------------------------- */
.text-box {
/* .text-box {
padding: 9px 14px;
border: 1px solid #e1e1e8;
border-radius: 4px;
......@@ -85,7 +85,7 @@
border-top-left-radius: 0px;
border-top-right-radius: 0px;
}
*/
/* posts#new
-------------------------------------------------- */
......
......@@ -102,6 +102,21 @@ class PostsController < ApplicationController
end
end
# POST /posts/1/comment
def comment
@post = set_post
@comment = @post.comments.build(comment_params.merge(author: current_user))
respond_to do |format|
if @comment.save
format.html { redirect_to posts_path(id: @post.id) }
format.json { render json: { status: 'ok', comment: @comment }, status: :created }
else
format.html { redirect_to posts_path(id: @post.id), flash: { alert: 'Comment is not saved.' } }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
......@@ -129,4 +144,8 @@ class PostsController < ApplicationController
def mail_params
params.require(:mail).permit(:to).to_hash.symbolize_keys
end
def comment_params
params.require(:comment).permit(:body).to_hash
end
end
module StyleHelper
## Store style
# Usage:
# <% style do %>
......
class Comment < ActiveRecord::Base
belongs_to :author, class_name: 'User'
belongs_to :post
validates :author_id, presence: true
validates :post_id, presence: true
validates :body, presence: true
end
......@@ -2,6 +2,7 @@ class Post < ActiveRecord::Base
has_many :post_tags
has_many :tags, through: :post_tags
belongs_to :author, class_name: 'User'
has_many :comments
# Named scope
scope :search, (lambda do |query|
......
......@@ -8,6 +8,7 @@ class User < ActiveRecord::Base
devise :omniauthable, omniauth_providers: [:google_oauth2]
has_many :posts
has_many :comments
# Device
def self.find_for_google_oauth2(access_token, signed_in_resource = nil)
......
<div class="text-box title" style!="position:relative;">
<a href="<%= post_path(@post) %>"><%= @post.title %></a>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<a href="<%= post_path(@post) %>"><%= @post.title %></a>
</h3>
</div>
<ul class="list-group">
<li class="list-group-item">
<%### Post meta ###%>
<% @post.tags.each do |tag| %>
<span class="label label-info">
<a href="<%= posts_path(q: "##{tag.name}") %>">#<%= tag.name %></a>
</span>
<% end %>
<span class="label label-success">
<a href="<%= posts_path(q: "@#{@post.author.name}") %>">@<%= @post.author.name %></a>
</span>
<span class="label label-danger">
<a href="<%= posts_path(q: "date:#{@post.updated_at.strftime('%Y-%m-%d')}") %>"><%= @post.updated_at.strftime('%Y-%m-%d') %></a>
</span>
<!-- Split button -->
<div class="btn-group pull-right" style="margin: -7px -12px 0 0;">
<button type="button" class="btn btn-primary">
<span class="glyphicon glyphicon-pencil"></span>
</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="<%= fork_post_path(@post) %>">Fork</a></li>
<li><a href="#" data-toggle="modal" data-target="#myModal">Mail to...</a></li>
<li class="divider"></li>
<li><%= link_to 'Delete', post_path(@post), method: :delete, data: { confirm: 'Are you sure?' } %></li>
</ul>
</div>
<%### /Post meta ###%>
</li>
</ul>
<div class="panel-body viewer github">
<%= h_application_format_markdown(@post.body) %>
</div>
</div>
<div class="text-box meta">
<% @post.tags.each do |tag| %>
<span class="label label-info">
<a href="<%= posts_path(q: "##{tag.name}") %>">#<%= tag.name %></a>
</span>
<% end %>
<span class="label label-success">
<a href="<%= posts_path(q: "@#{@post.author.name}") %>">@<%= @post.author.name %></a>
</span>
<span class="label label-danger">
<a href="<%= posts_path(q: "date:#{@post.updated_at.strftime('%Y-%m-%d')}") %>"><%= @post.updated_at.strftime('%Y-%m-%d') %></a>
</span>
<!-- Split button -->
<div class="btn-group pull-right" style="margin: -7px -12px 0 0;">
<button type="button" class="btn btn-primary">
<span class="glyphicon glyphicon-pencil"></span>
</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="<%= fork_post_path(@post) %>">Fork</a></li>
<li><a href="#" data-toggle="modal" data-target="#myModal">Mail to...</a></li>
<li class="divider"></li>
<li><%= link_to 'Delete', post_path(@post), method: :delete, data: { confirm: 'Are you sure?' } %></li>
</ul>
<%### Comments ###%>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Comments</h3>
</div>
<!-- /Split button -->
<div class="panel-body">
</div>
<div class="text-box body viewer github">
<%= h_application_format_markdown(@post.body) %>
</div>
<ul class="media-list">
<% @post.comments.each do |comment| %>
<li class="media">
<a class="pull-left" href="#">
<img class="media-object" src="<%= comment.author.image_url %>">
</a>
<div class="media-body">
<h4 class="media-heading"><%= comment.author.name %></h4>
<%= comment.body %>
</div>
</li>
<% end %>
<%= form_tag(comment_post_path, method: :post, data: { 'form-id' => "comment_#{@post.id}" }) do %>
<li class="media">
<a class="pull-left" href="#">
<img class="media-object" src="<%= current_user.image_url %>">
</a>
<div class="media-body">
<h4 class="media-heading"><%= current_user.name %></h4>
<%= text_area :comment, :body, class: 'form-control', placeholder: 'コメントする...' %>
<%= submit_tag 'Comment', class: 'btn btn-primary', data: { 'disable-with' => '...' } %>
</div>
</li>
<% end %>
<script type="text/javascript">
$("form[data-form-id='comment_<%= @post.id %>']").on('ajax:success', function(event, data, xhr) {
});
</script>
</ul>
</div>
</div>
<%### /Comments ###%>
<!-- Button trigger modal -->
<!-- <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
......
......@@ -6,6 +6,7 @@ Rendezvous::Application.routes.draw do
# get 'posts/show_fragment' => 'posts#show_fragment'
get 'posts/:id/fork' => 'posts#fork', as: 'fork_post'
post 'posts/:id/mail' => 'posts#mail', as: 'mail_post'
post 'posts/:id/comment' => 'posts#comment', as: 'comment_post'
resources :posts
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
......
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.integer :author_id
t.integer :post_id
t.text :body
t.timestamps
end
add_index :comments, [:author_id, :updated_at]
add_index :comments, [:post_id, :updated_at]
end
end
......@@ -11,7 +11,18 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20131228110818) do
ActiveRecord::Schema.define(version: 20140106160129) do
create_table 'comments', force: true do |t|
t.integer 'author_id'
t.integer 'post_id'
t.text 'body'
t.datetime 'created_at'
t.datetime 'updated_at'
end
add_index 'comments', ['author_id', 'updated_at'], name: 'index_comments_on_author_id_and_updated_at', using: :btree
add_index 'comments', ['post_id', 'updated_at'], name: 'index_comments_on_post_id_and_updated_at', using: :btree
create_table 'post_tags', force: true do |t|
t.integer 'post_id', null: false
......@@ -20,8 +31,8 @@ ActiveRecord::Schema.define(version: 20131228110818) do
t.datetime 'updated_at'
end
add_index 'post_tags', ['post_id'], name: 'index_post_tags_on_post_id'
add_index 'post_tags', ['tag_id'], name: 'index_post_tags_on_tag_id'
add_index 'post_tags', ['post_id'], name: 'index_post_tags_on_post_id', using: :btree
add_index 'post_tags', ['tag_id'], name: 'index_post_tags_on_tag_id', using: :btree
create_table 'posts', force: true do |t|
t.string 'title'
......@@ -38,7 +49,7 @@ ActiveRecord::Schema.define(version: 20131228110818) do
t.string 'ancestry'
end
add_index 'tags', ['ancestry'], name: 'index_tags_on_ancestry'
add_index 'tags', ['ancestry'], name: 'index_tags_on_ancestry', using: :btree
create_table 'users', force: true do |t|
t.string 'name'
......@@ -60,7 +71,7 @@ ActiveRecord::Schema.define(version: 20131228110818) do
t.datetime 'google_token_expires_at'
end
add_index 'users', ['email'], name: 'index_users_on_email', unique: true
add_index 'users', ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true
add_index 'users', ['email'], name: 'index_users_on_email', unique: true, using: :btree
add_index 'users', ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true, using: :btree
end
......@@ -39,12 +39,12 @@ _tag_tree.each do |_parent, _child|
end
# User
User.find_or_create_by(name: '山田太郎') do |_u|
user_1 = User.find_or_create_by(name: '山田太郎') do |_u|
_u.email = "#{Devise.friendly_token[0, 20]}@example.com"
_u.password = Devise.friendly_token[0, 20]
end
User.find_or_create_by(name: '鈴木二郎') do |_u|
user_2 = User.find_or_create_by(name: '鈴木二郎') do |_u|
_u.email = "#{Devise.friendly_token[0, 20]}@example.com"
_u.password = Devise.friendly_token[0, 20]
end
......@@ -57,13 +57,18 @@ end
# Post
users = User.all
Post.delete_all
Dir.glob(Rails.root.join('db', 'seeds').to_s + '/*').each do |file_name|
Rails.logger.debug "[Post Tag] #{file_name}"
title = file_name.split('/').last
post = Post.find_or_initialize_by(title: title)
post = Post.new(title: title)
post.body = File.read(file_name)
post.author = users.sample
post.tags = [tags.sample, tags.sample]
post.save!
end
Post.first.comments.create(author: user_1, body: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.')
Post.first.comments.create(author: user_2, body: 'Hooooooo....')
Post.first.comments.create(author: user_1, body: 'It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.')
# FactoryGirl.define do
# factory :comment_1, class: Comment do
# body 'ruby'
# end
# end
require 'spec_helper'
describe Comment do
describe 'validations' do
it 'blank author_id' do
expect(Comment.new(post_id: 1, body: 'test')).not_to be_valid
end
it 'blank post_id' do
expect(Comment.new(author_id: 1, body: 'test')).not_to be_valid
end
it 'blank body' do
expect(Comment.new(post_id: 1, author_id: 2)).not_to be_valid
end
end
end
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