Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cs_comments_service
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
cs_comments_service
Commits
bfb43459
Commit
bfb43459
authored
Jun 26, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
parameters to retrieve comments up to a certain depth; untested
parent
259f6f93
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
6 deletions
+23
-6
app.rb
+15
-2
models/comment.rb
+6
-2
models/comment_thread.rb
+2
-2
No files found.
app.rb
View file @
bfb43459
...
@@ -19,7 +19,11 @@ ActiveRecord::Base.establish_connection(databases[env])
...
@@ -19,7 +19,11 @@ ActiveRecord::Base.establish_connection(databases[env])
# retrive all comments of a commentable object
# retrive all comments of a commentable object
get
'/api/v1/commentables/:commentable_type/:commentable_id/comments'
do
|
commentable_type
,
commentable_id
|
get
'/api/v1/commentables/:commentable_type/:commentable_id/comments'
do
|
commentable_type
,
commentable_id
|
comment_thread
=
CommentThread
.
find_or_create_by_commentable_type_and_commentable_id
(
commentable_type
,
commentable_id
)
comment_thread
=
CommentThread
.
find_or_create_by_commentable_type_and_commentable_id
(
commentable_type
,
commentable_id
)
comment_thread
.
json_comments
if
params
[
"to_depth"
]
comment_thread
.
json_comments
(
to_depth:
params
[
"to_depth"
].
to_i
)
else
comment_thread
.
json_comments
end
end
end
# create a new top-level comment
# create a new top-level comment
...
@@ -65,11 +69,20 @@ end
...
@@ -65,11 +69,20 @@ end
# get the information of a single comment
# get the information of a single comment
get
'/api/v1/comments/:comment_id'
do
|
comment_id
|
get
'/api/v1/comments/:comment_id'
do
|
comment_id
|
puts
params
comment
=
Comment
.
find_by_id
(
comment_id
)
comment
=
Comment
.
find_by_id
(
comment_id
)
if
comment
.
nil?
or
comment
.
is_root?
if
comment
.
nil?
or
comment
.
is_root?
error
400
,
{
:error
=>
"invalid comment id"
}.
to_json
error
400
,
{
:error
=>
"invalid comment id"
}.
to_json
else
else
comment
.
to_json
if
params
[
"recursive"
]
==
"true"
if
params
[
"to_depth"
]
comment
.
to_hash_tree
(
to_depth:
params
[
"to_depth"
].
to_i
).
to_json
else
comment
.
to_hash_tree
.
to_json
end
else
comment
.
to_json
end
end
end
end
end
...
...
models/comment.rb
View file @
bfb43459
...
@@ -21,8 +21,12 @@ class Comment < ActiveRecord::Base
...
@@ -21,8 +21,12 @@ class Comment < ActiveRecord::Base
nodes
.
map
{
|
node
,
sub_nodes
|
node
.
to_hash
.
merge
(
:children
=>
hash_tree
(
sub_nodes
).
compact
)}
nodes
.
map
{
|
node
,
sub_nodes
|
node
.
to_hash
.
merge
(
:children
=>
hash_tree
(
sub_nodes
).
compact
)}
end
end
def
to_hash_tree
def
to_hash_tree
(
args
=
nil
)
self
.
class
.
hash_tree
(
self
.
subtree
.
arrange
(
:order
=>
"updated_at DESC"
))
if
args
and
args
[
:to_depth
]
self
.
class
.
hash_tree
(
self
.
subtree
(
to_depth:
args
[
:to_depth
]).
arrange
(
:order
=>
"updated_at DESC"
))
else
self
.
class
.
hash_tree
(
self
.
subtree
.
arrange
(
:order
=>
"updated_at DESC"
))
end
end
end
def
to_hash
def
to_hash
...
...
models/comment_thread.rb
View file @
bfb43459
...
@@ -29,8 +29,8 @@ class CommentThread < ActiveRecord::Base
...
@@ -29,8 +29,8 @@ class CommentThread < ActiveRecord::Base
super_comment
.
descendants
super_comment
.
descendants
end
end
def
json_comments
def
json_comments
(
args
=
nil
)
super_comment
.
to_hash_tree
.
first
[
:children
].
to_json
super_comment
.
to_hash_tree
(
args
)
.
first
[
:children
].
to_json
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment