make_commentable 0.0.8.1.1 → 0.0.8.1.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ class CommentGenerator < Rails::Generators::Base
|
|
4
4
|
include Rails::Generators::Migration
|
5
5
|
|
6
6
|
def self.source_root
|
7
|
-
@
|
7
|
+
@_make_commentable_source_root ||= File.expand_path("../templates", __FILE__)
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.next_migration_number(path)
|
@@ -0,0 +1,105 @@
|
|
1
|
+
class CommentsController < ApplicationController
|
2
|
+
# GET /comments
|
3
|
+
# GET /comments.json
|
4
|
+
def index
|
5
|
+
@comments = Comment.all
|
6
|
+
|
7
|
+
respond_to do |format|
|
8
|
+
format.html # index.html.erb
|
9
|
+
format.json { render json: @comments }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# GET /comments/1
|
14
|
+
# GET /comments/1.json
|
15
|
+
def show
|
16
|
+
@comment = Comment.find(params[:id])
|
17
|
+
|
18
|
+
respond_to do |format|
|
19
|
+
format.html # show.html.erb
|
20
|
+
format.json { render json: @comment }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# GET /comments/new
|
25
|
+
# GET /comments/new.json
|
26
|
+
def new
|
27
|
+
@comment = Comment.new
|
28
|
+
|
29
|
+
respond_to do |format|
|
30
|
+
format.html # new.html.erb
|
31
|
+
format.json { render json: @comment }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# GET /comments/1/edit
|
36
|
+
def edit
|
37
|
+
@comment = Comment.find(params[:id])
|
38
|
+
end
|
39
|
+
|
40
|
+
# POST /comments
|
41
|
+
# POST /comments.json
|
42
|
+
def create
|
43
|
+
@source = find_commentable
|
44
|
+
@comment = @source.comments.new(params[:comment])
|
45
|
+
|
46
|
+
if params[:comment][:parent_id] != ''
|
47
|
+
parent_comment = Comment.find(params[:comment][:parent_id])
|
48
|
+
@comment.parent = parent_comment
|
49
|
+
end
|
50
|
+
|
51
|
+
@comment.user = current_user
|
52
|
+
|
53
|
+
respond_to do |format|
|
54
|
+
if @comment.save
|
55
|
+
format.html { flash[:notice] = 'comment added'; redirect_to :back }
|
56
|
+
format.js { render :layout => false }
|
57
|
+
else
|
58
|
+
format.html { flash[:error] = 'error happened'; render :action => 'new' }
|
59
|
+
format.js { render :layout => false }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# PUT /comments/1
|
65
|
+
# PUT /comments/1.json
|
66
|
+
def update
|
67
|
+
@comment = Comment.find(params[:id])
|
68
|
+
|
69
|
+
respond_to do |format|
|
70
|
+
if @comment.update_attributes(params[:comment])
|
71
|
+
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
|
72
|
+
format.json { head :no_content }
|
73
|
+
else
|
74
|
+
format.html { render action: "edit" }
|
75
|
+
format.json { render json: @comment.errors, status: :unprocessable_entity }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# DELETE /comments/1
|
81
|
+
# DELETE /comments/1.json
|
82
|
+
def destroy
|
83
|
+
@comment = Comment.find(params[:id])
|
84
|
+
@comment.destroy
|
85
|
+
|
86
|
+
respond_to do |format|
|
87
|
+
format.html { redirect_to comments_url }
|
88
|
+
format.json { head :no_content }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
# find commentable model
|
95
|
+
# name like 'company_id'
|
96
|
+
# value model's id
|
97
|
+
def find_commentable
|
98
|
+
params.each do |name, value|
|
99
|
+
if name =~ /(.+)_id$/
|
100
|
+
return $1.classify.constantize.find(value)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
nil
|
104
|
+
end
|
105
|
+
end
|