comment 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/comment/comment_methods.rb +37 -0
- data/lib/comment/commentable_methods.rb +37 -0
- data/lib/comment/version.rb +1 -1
- data/lib/generators/comment/USAGE +8 -0
- data/lib/generators/comment/comment_generator.rb +41 -0
- data/lib/generators/comment/templates/app/controllers/comments_controller.rb +71 -0
- data/lib/generators/comment/templates/app/models/comment.rb +32 -0
- data/lib/generators/comment/templates/app/views/comments/_comment.html.erb +23 -0
- data/lib/generators/comment/templates/app/views/comments/_comment_headline.html.erb +22 -0
- data/lib/generators/comment/templates/app/views/comments/_form.html.erb +40 -0
- data/lib/generators/comment/templates/app/views/comments/create.js.erb +10 -0
- data/lib/generators/comment/templates/app/views/comments/destroy.js.erb +1 -0
- data/lib/generators/comment/templates/app/views/comments/edit.js.erb +6 -0
- data/lib/generators/comment/templates/app/views/comments/index.html.erb +10 -0
- data/lib/generators/comment/templates/app/views/comments/new.js.erb +6 -0
- data/lib/generators/comment/templates/app/views/comments/update.js.erb +5 -0
- data/lib/generators/comment/templates/create_comments.rb +39 -0
- metadata +17 -1
@@ -0,0 +1,37 @@
|
|
1
|
+
module Bravo
|
2
|
+
# including this module into your Comment model will give you finders and named scopes
|
3
|
+
# useful for working with Comments.
|
4
|
+
# The named scopes are:
|
5
|
+
# in_order: Returns comments in the order they were created (created_at ASC).
|
6
|
+
# recent: Returns comments by how recently they were created (created_at DESC).
|
7
|
+
# limit(N): Return no more than N comments.
|
8
|
+
module Comment
|
9
|
+
|
10
|
+
def self.included(comment_model)
|
11
|
+
comment_model.extend Finders
|
12
|
+
comment_model.scope :in_order, comment_model.order('created_at ASC')
|
13
|
+
comment_model.scope :recent, comment_model.order('created_at DESC')
|
14
|
+
end
|
15
|
+
|
16
|
+
module Finders
|
17
|
+
# Helper class method to lookup all comments assigned
|
18
|
+
# to all commentable types for a given user.
|
19
|
+
def find_comments_by_user(user)
|
20
|
+
where(["user_id = ?", user.id]).order("created_at DESC")
|
21
|
+
end
|
22
|
+
|
23
|
+
# Helper class method to look up all comments for
|
24
|
+
# commentable class name and commentable id.
|
25
|
+
def find_comments_for_commentable(commentable_type, commentable_id)
|
26
|
+
where(["commentable_type = ? and commentable_id = ?", commentable_type, commentable_id]).order("created_at DESC")
|
27
|
+
end
|
28
|
+
|
29
|
+
# Helper class method to look up a commentable object
|
30
|
+
# given the commentable class name and id
|
31
|
+
def find_commentable(commentable_str, commentable_id)
|
32
|
+
model = commentable_str.constantize
|
33
|
+
model.respond_to?(:find_comments_for) ? model.find(commentable_id) : nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module Bravo
|
4
|
+
module Commentable
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def has_comments
|
12
|
+
has_many :comments, {:as => :commentable, :dependent => :destroy}
|
13
|
+
|
14
|
+
class_eval %{
|
15
|
+
def self.find_comments_for(obj)
|
16
|
+
Comment.find_comments_for_commentable(self.base_class.name, obj.id)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.find_comments_by_user(user)
|
20
|
+
commentable = self.base_class.name
|
21
|
+
Comment.where(["user_id = ? and commentable_type = ?", user.id, commentable]).order("created_at DESC")
|
22
|
+
end
|
23
|
+
|
24
|
+
def comments_ordered_by_submitted
|
25
|
+
Comment.find_comments_for_commentable(self.class.name, id)
|
26
|
+
end
|
27
|
+
|
28
|
+
def add_comments(comment)
|
29
|
+
comments << comment
|
30
|
+
end
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
ActiveRecord::Base.send(:include, Bravo::Commentable)
|
data/lib/comment/version.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
class CommentGenerator < Rails::Generators::Base
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
def self.next_migration_number(path)
|
9
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_model_file
|
13
|
+
template "app/models/comment.rb"
|
14
|
+
migration_template "create_comments.rb", "db/migrate/create_comments.rb"
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_route
|
18
|
+
route("resources :comments")
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_controller_file
|
22
|
+
template 'app/controllers/comments_controller.rb'
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_views
|
26
|
+
directory "app/views/comments/"
|
27
|
+
|
28
|
+
inside "app/views/comments" do
|
29
|
+
template "_comment.html.erb"
|
30
|
+
template "_comment_headline.html.erb"
|
31
|
+
template "_form.html.erb"
|
32
|
+
template "create.js.erb"
|
33
|
+
template "destroy.js.erb"
|
34
|
+
template "edit.js.erb"
|
35
|
+
template "index.html.erb"
|
36
|
+
template "new.js.erb"
|
37
|
+
template "update.js.erb"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
class CommentsController < ApplicationController
|
3
|
+
|
4
|
+
def index
|
5
|
+
@comments = Comment.search(params[:comment_search]).includes(:user).arrange(:order => "created_at asc")
|
6
|
+
@comment = Comment.new(:user => current_user)
|
7
|
+
end
|
8
|
+
|
9
|
+
def new
|
10
|
+
@comment = Comment.new(:parent_id => params[:parent_id], :user => current_user)
|
11
|
+
end
|
12
|
+
|
13
|
+
def create
|
14
|
+
@comment = current_user.comments.build(params[:comment])
|
15
|
+
#@comment.request = request
|
16
|
+
if @comment.save
|
17
|
+
flash.now[:notice] = "回复成功"
|
18
|
+
end
|
19
|
+
respond_to do |format|
|
20
|
+
format.html do
|
21
|
+
if @comment.errors.present?
|
22
|
+
render :new
|
23
|
+
else
|
24
|
+
#@comment.notify_other_commenters
|
25
|
+
redirect_to comments_path
|
26
|
+
end
|
27
|
+
end
|
28
|
+
format.js
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def edit
|
33
|
+
@comment = current_user.comments.find(params[:id])
|
34
|
+
end
|
35
|
+
|
36
|
+
def update
|
37
|
+
@comment = current_user.comments.find(params[:id])
|
38
|
+
if @comment.update_attribute(:content, params[:comment][:content])
|
39
|
+
flash.now[:notice] = "修改成功"
|
40
|
+
end
|
41
|
+
respond_to do |format|
|
42
|
+
format.html do
|
43
|
+
if @comment.errors.present?
|
44
|
+
render :edit
|
45
|
+
else
|
46
|
+
redirect_to(episode_path(@comment.episode, :view => "comments"))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
format.js
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def destroy
|
54
|
+
@comment = current_user.comments.find(params[:id])
|
55
|
+
@comment.destroy
|
56
|
+
flash.now[:notice] = "删除成功"
|
57
|
+
respond_to do |format|
|
58
|
+
#format.html { redirect_to episode_path(@comment.episode, :view => "comments") }
|
59
|
+
format.js
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def undo_link
|
66
|
+
if can? :revert, :versions
|
67
|
+
version = @comment.versions.scoped.last
|
68
|
+
view_context.link_to("undo", revert_version_path(version), :method => :post) if can? :revert, version
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Comment < ActiveRecord::Base
|
2
|
+
include Bravo::Comment
|
3
|
+
|
4
|
+
attr_accessible :content, :parent_id, :user, :commentable_id, :commentable_type
|
5
|
+
|
6
|
+
belongs_to :user
|
7
|
+
belongs_to :commentable, :polymorphic => true
|
8
|
+
validates_presence_of :content
|
9
|
+
|
10
|
+
has_ancestry
|
11
|
+
|
12
|
+
before_save :set_commentable
|
13
|
+
|
14
|
+
def self.search(query)
|
15
|
+
if query.blank?
|
16
|
+
scoped
|
17
|
+
else
|
18
|
+
conditions = %w[content].map { |c| "comments.#{c} like :query" }
|
19
|
+
where(conditions.join(" or "),:query => "%#{query}%")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def set_commentable
|
25
|
+
if parent_id.present?
|
26
|
+
parent = Comment.find(parent_id)
|
27
|
+
self.commentable_id = parent.commentable_id
|
28
|
+
self.commentable_type = parent.commentable_type
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
<%= div_for comment do %>
|
2
|
+
<%= render "common/flashes" %>
|
3
|
+
<div class="face">
|
4
|
+
<%= profile_avatar_tag(comment.user.profile, :size36) %>
|
5
|
+
</div>
|
6
|
+
<div class="main">
|
7
|
+
<%= render "comments/comment_headline", :comment => comment %>
|
8
|
+
<div class="comment_content">
|
9
|
+
<%= simple_format comment.content %>
|
10
|
+
</div>
|
11
|
+
<div class="actions">
|
12
|
+
<%= link_to "Reply", new_comment_path(:parent_id => comment), :remote => true %>
|
13
|
+
<% if current_user.id == comment.user.id %>
|
14
|
+
<span class="bullet">•</span>
|
15
|
+
<%= link_to "Edit", edit_comment_path(comment), :remote => true %>
|
16
|
+
<span class="bullet">•</span>
|
17
|
+
<%= link_to "Delete", comment, :method => :delete, :remote => true, :confirm => "此评论和所有子评论都将被删除,你确定吗?" %>
|
18
|
+
<% end %>
|
19
|
+
</div>
|
20
|
+
<div style="clear:both;"></div>
|
21
|
+
</div>
|
22
|
+
<div style="clear:both;"></div>
|
23
|
+
<% end %>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<div class="headline">
|
2
|
+
<span class="name">
|
3
|
+
<%= link_to comment.user.profile.name, comment.user.profile %>
|
4
|
+
</span>
|
5
|
+
<% if comment.new_record? %>
|
6
|
+
<span class="bullet">•</span>
|
7
|
+
<span class="position">请输入评论内容</span>
|
8
|
+
<% end %>
|
9
|
+
<% unless comment.created_at.nil? %>
|
10
|
+
<span class="bullet">•</span>
|
11
|
+
<%= time_ago_in_words comment.created_at %>前
|
12
|
+
<%#<%= comment.created_at.strftime("%Y-%m-%d %H:%M:%I") %>
|
13
|
+
<% end %>
|
14
|
+
<ul class="menu">
|
15
|
+
<li class="collapse">
|
16
|
+
<a href="#" data-action="collapse" title="Collapse">−</a>
|
17
|
+
</li>
|
18
|
+
<li class="expand" style="display:none">
|
19
|
+
<a href="#" data-action="collapse" title="Expand">+</a>
|
20
|
+
</li>
|
21
|
+
</ul>
|
22
|
+
</div>
|
@@ -0,0 +1,40 @@
|
|
1
|
+
<%= div_for @comment do %>
|
2
|
+
<div class="face">
|
3
|
+
<%= profile_avatar_tag(current_user.profile, :size36) %>
|
4
|
+
</div>
|
5
|
+
<div class="main">
|
6
|
+
<%= render "comments/comment_headline", :comment => @comment %>
|
7
|
+
<div class="comment_content">
|
8
|
+
<%= form_for @comment, :remote => true do |f| %>
|
9
|
+
<fieldset>
|
10
|
+
<%= render :partial => "common/form_error", :locals => { model: @comment } %>
|
11
|
+
<%= f.hidden_field :parent_id %>
|
12
|
+
<%= f.hidden_field :commentable_id %>
|
13
|
+
<%= f.hidden_field :commentable_type %>
|
14
|
+
<div class="control-group">
|
15
|
+
<div class="controls">
|
16
|
+
<%= f.text_area :content, :class => 'text_area span6', :rows => 3, :onKeyDown => "javascript:return ctrl_enter_submit(event);" %>
|
17
|
+
<div>
|
18
|
+
<%= f.submit(@comment.new_record? ? "Post Comment" : "Update Comment", :class => "btn btn-primary") %>
|
19
|
+
(Ctrl+Enter)
|
20
|
+
</div>
|
21
|
+
</div>
|
22
|
+
</div>
|
23
|
+
</fieldset>
|
24
|
+
<% end %>
|
25
|
+
</div>
|
26
|
+
<% unless @comment.new_record? %>
|
27
|
+
<div class="actions">
|
28
|
+
<%= link_to "Reply", new_comment_path(:parent_id => @comment), :remote => true %>
|
29
|
+
<% if current_user.id == @comment.user.id %>
|
30
|
+
<span class="bullet">•</span>
|
31
|
+
<%= link_to "Cancel Edit", edit_comment_path(@comment), :remote => true %>
|
32
|
+
<span class="bullet">•</span>
|
33
|
+
<%= link_to "Delete", @comment, :method => :delete, :remote => true %>
|
34
|
+
<% end %>
|
35
|
+
</div>
|
36
|
+
<% end %>
|
37
|
+
<div style="clear:both;"></div>
|
38
|
+
</div>
|
39
|
+
<div style="clear:both;"></div>
|
40
|
+
<% end %>
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<% if @comment.parent_id.present? %>
|
2
|
+
var container = $("#<%= dom_id(@comment.parent) %>").next(".nested_comments");
|
3
|
+
<% else %>
|
4
|
+
var container = $("#comments");
|
5
|
+
<% end %>
|
6
|
+
<% if @comment.errors.present? %>
|
7
|
+
container.find("form").prepend("<%= escape_javascript error_messages_for(@comment) %>");
|
8
|
+
<% else %>
|
9
|
+
container.children("#new_comment").replaceWith("<%= escape_javascript render(@comment) + content_tag(:div, "", :class => "nested_comments") %>");
|
10
|
+
<% end %>
|
@@ -0,0 +1 @@
|
|
1
|
+
$("#<%= dom_id(@comment) %>").replaceWith("<%= escape_javascript render "common/flashes" %>");
|
@@ -0,0 +1,6 @@
|
|
1
|
+
if ($("#<%= dom_id(@comment) %> .comment_content form").length == 0) {
|
2
|
+
$("#<%= dom_id(@comment) %>").replaceWith("<%= escape_javascript render("form") %>");
|
3
|
+
$("#<%= dom_id(@comment) %>").find("#comment_content")[0].focus();
|
4
|
+
} else {
|
5
|
+
$("#<%= dom_id(@comment) %>").replaceWith("<%= escape_javascript render(@comment) %>");
|
6
|
+
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<% provide :title, "Recent Comments" %>
|
2
|
+
<%= form_tag comments_path, :method => :get do %>
|
3
|
+
<%= text_field_tag :comment_search, params[:comment_search] %>
|
4
|
+
<%= submit_tag "Search Comments", :class => 'btn btn-primary' %>
|
5
|
+
<% end %>
|
6
|
+
<div class="box" id="comments">
|
7
|
+
<h2>评论列表</h2>
|
8
|
+
<%= nested_comments @comments %>
|
9
|
+
<%= render 'form' %>
|
10
|
+
</div>
|
@@ -0,0 +1,6 @@
|
|
1
|
+
if ($("#comment_<%= params[:parent_id] %>").next(".nested_comments").children("#new_comment").length == 0) {
|
2
|
+
$("#comment_<%= params[:parent_id] %>").next(".nested_comments").prepend("<%= escape_javascript render("form") %>");
|
3
|
+
$("#comment_<%= params[:parent_id] %>").next(".nested_comments").find("#comment_content")[0].focus();
|
4
|
+
} else {
|
5
|
+
$("#comment_<%= params[:parent_id] %>").next(".nested_comments").children("#new_comment").toggleClass('hidden');
|
6
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class CreateComments < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :comments do |t|
|
4
|
+
t.string :title, :limit => 50, :default => ""
|
5
|
+
t.text :comment
|
6
|
+
t.references :commentable, :polymorphic => true
|
7
|
+
t.references :user
|
8
|
+
t.string :role, :default => "comments"
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
add_index :comments, :commentable_type
|
13
|
+
add_index :comments, :commentable_id
|
14
|
+
add_index :comments, :user_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :comments
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class CreateComments < ActiveRecord::Migration
|
23
|
+
def change
|
24
|
+
create_table :comments do |t|
|
25
|
+
t.text :content
|
26
|
+
add_column :comments, :ancestry, :string
|
27
|
+
t.references :user
|
28
|
+
t.references :commentable, :polymorphic => true
|
29
|
+
#add_column :comments, :commentable_id, :integer
|
30
|
+
#add_column :comments, :commentable_type, :string
|
31
|
+
#t.string :role, :default => "comments"
|
32
|
+
t.timestamps
|
33
|
+
|
34
|
+
add_index :comments, :commentable_type
|
35
|
+
add_index :comments, :commentable_id
|
36
|
+
add_index :comments, :user_id
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: comment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -25,7 +25,23 @@ files:
|
|
25
25
|
- Rakefile
|
26
26
|
- comment.gemspec
|
27
27
|
- lib/comment.rb
|
28
|
+
- lib/comment/comment_methods.rb
|
29
|
+
- lib/comment/commentable_methods.rb
|
28
30
|
- lib/comment/version.rb
|
31
|
+
- lib/generators/comment/USAGE
|
32
|
+
- lib/generators/comment/comment_generator.rb
|
33
|
+
- lib/generators/comment/templates/app/controllers/comments_controller.rb
|
34
|
+
- lib/generators/comment/templates/app/models/comment.rb
|
35
|
+
- lib/generators/comment/templates/app/views/comments/_comment.html.erb
|
36
|
+
- lib/generators/comment/templates/app/views/comments/_comment_headline.html.erb
|
37
|
+
- lib/generators/comment/templates/app/views/comments/_form.html.erb
|
38
|
+
- lib/generators/comment/templates/app/views/comments/create.js.erb
|
39
|
+
- lib/generators/comment/templates/app/views/comments/destroy.js.erb
|
40
|
+
- lib/generators/comment/templates/app/views/comments/edit.js.erb
|
41
|
+
- lib/generators/comment/templates/app/views/comments/index.html.erb
|
42
|
+
- lib/generators/comment/templates/app/views/comments/new.js.erb
|
43
|
+
- lib/generators/comment/templates/app/views/comments/update.js.erb
|
44
|
+
- lib/generators/comment/templates/create_comments.rb
|
29
45
|
homepage: ''
|
30
46
|
licenses: []
|
31
47
|
post_install_message:
|