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
1d1ca019
Commit
1d1ca019
authored
Jul 16, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug caused by autosave indeed; fixed previous autosave bug
parent
cb1c0abb
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
25 additions
and
34 deletions
+25
-34
app.rb
+2
-3
config/application.yml
+1
-1
models/comment.rb
+2
-2
models/comment_thread.rb
+6
-6
models/commentable.rb
+1
-1
models/feed.rb
+5
-3
models/user.rb
+6
-6
spec/app_spec.rb
+2
-12
No files found.
app.rb
View file @
1d1ca019
...
@@ -75,7 +75,7 @@ end
...
@@ -75,7 +75,7 @@ end
post
'/api/v1/commentables/:commentable_type/:commentable_id/comment_threads'
do
|
commentable_type
,
commentable_id
|
post
'/api/v1/commentables/:commentable_type/:commentable_id/comment_threads'
do
|
commentable_type
,
commentable_id
|
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
commentable_type
,
commentable_id:
commentable_id
)
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
commentable_type
,
commentable_id:
commentable_id
)
comment_thread
=
commentable
.
comment_threads
.
new
(
params
.
slice
(
*
%w[title body course_id]
))
comment_thread
=
commentable
.
comment_threads
.
new
(
params
.
slice
(
*
%w[title body course_id]
))
comment_thread
.
author
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
comment_thread
.
author
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
if
params
[
"user_id"
]
comment_thread
.
save!
comment_thread
.
save!
comment_thread
.
to_hash
.
to_json
comment_thread
.
to_hash
.
to_json
end
end
...
@@ -103,7 +103,7 @@ end
...
@@ -103,7 +103,7 @@ end
post
'/api/v1/comment_threads/:comment_thread_id/comments'
do
|
comment_thread_id
|
post
'/api/v1/comment_threads/:comment_thread_id/comments'
do
|
comment_thread_id
|
comment_thread
=
CommentThread
.
find
(
comment_thread_id
)
comment_thread
=
CommentThread
.
find
(
comment_thread_id
)
comment
=
comment_thread
.
comments
.
new
(
params
.
slice
(
*
%w[body course_id]
))
comment
=
comment_thread
.
comments
.
new
(
params
.
slice
(
*
%w[body course_id]
))
comment
.
author
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
comment
.
author
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
if
params
[
"user_id"
]
comment
.
save!
comment
.
save!
comment
.
to_hash
.
to_json
comment
.
to_hash
.
to_json
end
end
...
@@ -200,7 +200,6 @@ end
...
@@ -200,7 +200,6 @@ end
get
'/api/v1/users/:user_id/feeds'
do
|
user_id
|
get
'/api/v1/users/:user_id/feeds'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
puts
user
.
inspect
user
.
subscribed_feeds
.
map
(
&
:to_hash
).
to_json
user
.
subscribed_feeds
.
map
(
&
:to_hash
).
to_json
end
end
...
...
config/application.yml
View file @
1d1ca019
level_limit
:
3
level_limit
:
3
send_notifications_to_author
:
false
send_notifications_to_author
:
false
auto_watch_comment_threads
:
true
auto_watch_comment_threads
:
true
allow_anonymity
:
tru
e
allow_anonymity
:
fals
e
models/comment.rb
View file @
1d1ca019
...
@@ -79,9 +79,9 @@ private
...
@@ -79,9 +79,9 @@ private
def
handle_after_create
def
handle_after_create
generate_feeds
generate_feeds
#
auto_watch_comment_thread
auto_watch_comment_thread
end
end
#
handle_asynchronously :handle_after_create
handle_asynchronously
:handle_after_create
end
end
models/comment_thread.rb
View file @
1d1ca019
...
@@ -9,10 +9,10 @@ class CommentThread
...
@@ -9,10 +9,10 @@ class CommentThread
field
:body
,
type:
String
field
:body
,
type:
String
field
:course_id
,
type:
String
,
index:
true
field
:course_id
,
type:
String
,
index:
true
belongs_to
:author
,
class_name:
"User"
,
inverse_of: :comment_threads
,
index:
true
#
, autosave: true
belongs_to
:author
,
class_name:
"User"
,
inverse_of: :comment_threads
,
index:
true
,
autosave:
true
belongs_to
:commentable
,
index:
true
#
, autosave: true
belongs_to
:commentable
,
index:
true
,
autosave:
true
has_many
:comments
,
dependent: :destroy
# Use destroy to envoke callback on the top-level comments TODO async
has_many
:comments
,
dependent: :destroy
,
autosave:
true
# Use destroy to envoke callback on the top-level comments TODO async
has_and_belongs_to_many
:watchers
,
class_name:
"User"
,
inverse_of: :watched_comment_threads
#
, autosave: true
has_and_belongs_to_many
:watchers
,
class_name:
"User"
,
inverse_of: :watched_comment_threads
,
autosave:
true
attr_accessible
:title
,
:body
,
:course_id
attr_accessible
:title
,
:body
,
:course_id
...
@@ -48,7 +48,7 @@ private
...
@@ -48,7 +48,7 @@ private
feed
.
actor
=
author
feed
.
actor
=
author
feed
.
target
=
self
feed
.
target
=
self
feed
.
subscribers
<<
(
commentable
.
watchers
+
(
author
.
followers
if
author
).
to_a
).
uniq_by
(
&
:id
)
feed
.
subscribers
<<
(
commentable
.
watchers
+
(
author
.
followers
if
author
).
to_a
).
uniq_by
(
&
:id
)
feed
.
subscribers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
feed
.
subscribers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
and
author
feed
.
save!
feed
.
save!
end
end
end
end
...
@@ -64,5 +64,5 @@ private
...
@@ -64,5 +64,5 @@ private
auto_watch_comment_thread
auto_watch_comment_thread
end
end
#
handle_asynchronously :handle_after_create
handle_asynchronously
:handle_after_create
end
end
models/commentable.rb
View file @
1d1ca019
...
@@ -5,7 +5,7 @@ class Commentable
...
@@ -5,7 +5,7 @@ class Commentable
field
:commentable_id
,
type:
String
field
:commentable_id
,
type:
String
has_many
:comment_threads
,
dependent: :destroy
has_many
:comment_threads
,
dependent: :destroy
has_and_belongs_to_many
:watchers
,
class_name:
"User"
,
inverse_of: :watched_commentables
#
, autosave: true
has_and_belongs_to_many
:watchers
,
class_name:
"User"
,
inverse_of: :watched_commentables
,
autosave:
true
attr_accessible
:commentable_type
,
:commentable_id
attr_accessible
:commentable_type
,
:commentable_id
...
...
models/feed.rb
View file @
1d1ca019
...
@@ -5,13 +5,15 @@ class Feed
...
@@ -5,13 +5,15 @@ class Feed
field
:feed_type
,
type:
String
field
:feed_type
,
type:
String
field
:info
,
type:
Hash
field
:info
,
type:
Hash
belongs_to
:actor
,
class_name:
"User"
,
inverse_of: :activities
,
index:
true
belongs_to
:actor
,
class_name:
"User"
,
inverse_of: :activities
,
index:
true
,
autosave:
true
belongs_to
:target
,
inverse_of: :activities
,
polymorphic:
true
belongs_to
:target
,
inverse_of: :activities
,
polymorphic:
true
,
autosave:
true
attr_accessible
:feed_type
,
:info
attr_accessible
:feed_type
,
:info
validates_presence_of
:feed_type
validates_presence_of
:feed_type
validates_presence_of
:actor
if
not
CommentService
.
config
[
"allow_anonymity"
]
validates_presence_of
:actor
end
validates_presence_of
:target
validates_presence_of
:target
has_and_belongs_to_many
:subscribers
,
class_name:
"User"
,
inverse_of: :subscribed_feeds
,
autosave:
true
has_and_belongs_to_many
:subscribers
,
class_name:
"User"
,
inverse_of: :subscribed_feeds
,
autosave:
true
...
...
models/user.rb
View file @
1d1ca019
...
@@ -2,14 +2,14 @@ class User
...
@@ -2,14 +2,14 @@ class User
include
Mongoid
::
Document
include
Mongoid
::
Document
include
Mongo
::
Voter
include
Mongo
::
Voter
field
:external_id
,
type:
String
,
index:
true
key
:external_id
,
type:
String
,
index:
true
has_many
:comments
has_many
:comments
has_many
:comment_threads
,
inverse_of: :author
has_many
:comment_threads
,
inverse_of: :author
has_many
:activities
,
class_name:
"Feed"
,
inverse_of: :actor
has_many
:activities
,
class_name:
"Feed"
,
inverse_of: :actor
has_and_belongs_to_many
:subscribed_feeds
,
class_name:
"Feed"
,
inverse_of: :subscribers
,
autosave:
true
has_and_belongs_to_many
:subscribed_feeds
,
class_name:
"Feed"
,
inverse_of: :subscribers
has_and_belongs_to_many
:followers
,
class_name:
"User"
,
inverse_of: :followings
has_and_belongs_to_many
:followers
,
class_name:
"User"
,
inverse_of: :followings
has_and_belongs_to_many
:followings
,
class_name:
"User"
,
inverse_of: :followers
has_and_belongs_to_many
:followings
,
class_name:
"User"
,
inverse_of: :followers
,
autosave:
true
validates_presence_of
:external_id
validates_presence_of
:external_id
validates_uniqueness_of
:external_id
validates_uniqueness_of
:external_id
...
@@ -34,19 +34,19 @@ class User
...
@@ -34,19 +34,19 @@ class User
class_name
=
class_single
.
camelize
class_name
=
class_single
.
camelize
watched_symbol
=
"watched_
#{
class_plural
}
"
.
intern
watched_symbol
=
"watched_
#{
class_plural
}
"
.
intern
has_and_belongs_to_many
watched_symbol
,
class_name:
class_name
,
inverse_of: :watchers
,
autosave:
true
has_and_belongs_to_many
watched_symbol
,
class_name:
class_name
,
inverse_of: :watchers
#
, autosave: true
self
.
class_eval
<<-
END
self
.
class_eval
<<-
END
def watch_
#{
class_single
}
(watching_object)
def watch_
#{
class_single
}
(watching_object)
if not watched_
#{
class_plural
}
.include? watching_object
if not watched_
#{
class_plural
}
.include? watching_object
watched_
#{
class_plural
}
<< watching_object
watched_
#{
class_plural
}
<< watching_object
save!
#
save!
end
end
end
end
def unwatch_
#{
class_single
}
(watching_object)
def unwatch_
#{
class_single
}
(watching_object)
watched_
#{
class_plural
}
.delete(watching_object)
watched_
#{
class_plural
}
.delete(watching_object)
save!
#
save!
end
end
END
END
end
end
...
...
spec/app_spec.rb
View file @
1d1ca019
...
@@ -111,14 +111,9 @@ def init_with_feeds
...
@@ -111,14 +111,9 @@ def init_with_feeds
user1
.
save!
user1
.
save!
user2
.
save!
user2
.
save!
pp
Feed
.
all
.
to_a
puts
User
.
first
.
inspect
puts
User
.
first
.
subscribed_feeds
.
inspect
end
end
describe
"app"
do
describe
"app"
do
=begin
describe
"commentables"
do
describe
"commentables"
do
before
(
:each
)
{
init_without_feeds
}
before
(
:each
)
{
init_without_feeds
}
describe
"DELETE /api/v1/commentables/:commentable_type/:commentable_id"
do
describe
"DELETE /api/v1/commentables/:commentable_type/:commentable_id"
do
...
@@ -159,7 +154,7 @@ describe "app" do
...
@@ -159,7 +154,7 @@ describe "app" do
end
end
describe
"POST /api/v1/commentables/:commentable_type/:commentable_id/comment_threads"
do
describe
"POST /api/v1/commentables/:commentable_type/:commentable_id/comment_threads"
do
it
"create a new comment thread for the commentable object"
do
it
"create a new comment thread for the commentable object"
do
post '/api/v1/commentables/questions/1/comment_threads', title: "Interesting question", body: "cool", course_id: "1"
post
'/api/v1/commentables/questions/1/comment_threads'
,
title:
"Interesting question"
,
body:
"cool"
,
course_id:
"1"
,
user_id:
"1"
last_response
.
should
be_ok
last_response
.
should
be_ok
CommentThread
.
count
.
should
==
3
CommentThread
.
count
.
should
==
3
CommentThread
.
where
(
title:
"Interesting question"
).
first
.
should_not
be_nil
CommentThread
.
where
(
title:
"Interesting question"
).
first
.
should_not
be_nil
...
@@ -340,16 +335,14 @@ describe "app" do
...
@@ -340,16 +335,14 @@ describe "app" do
end
end
end
end
end
end
=end
describe
"feeds"
do
describe
"feeds"
do
before
(
:each
)
{
init_with_feeds
}
before
(
:each
)
{
init_with_feeds
}
describe
"GET /api/v1/users/:user_id/feeds"
do
describe
"GET /api/v1/users/:user_id/feeds"
do
it
"get all subscribed feeds for the user"
do
it
"get all subscribed feeds for the user"
do
user
=
User
.
where
(
external_id:
"1"
).
first
user
=
User
.
find
(
"1"
)
get
"/api/v1/users/
#{
user
.
external_id
}
/feeds"
get
"/api/v1/users/
#{
user
.
external_id
}
/feeds"
last_response
.
should
be_ok
last_response
.
should
be_ok
feeds
=
parse
last_response
.
body
feeds
=
parse
last_response
.
body
pp
feeds
so_easy
=
Comment
.
all
.
select
{
|
c
|
c
.
body
==
"this problem is so easy"
}.
first
so_easy
=
Comment
.
all
.
select
{
|
c
|
c
.
body
==
"this problem is so easy"
}.
first
not_for_me_neither
=
Comment
.
all
.
select
{
|
c
|
c
.
body
==
"not for me neither!"
}.
first
not_for_me_neither
=
Comment
.
all
.
select
{
|
c
|
c
.
body
==
"not for me neither!"
}.
first
feed_so_easy
=
feeds
.
select
{
|
f
|
f
[
"feed_type"
]
==
"post_reply"
and
f
[
"info"
][
"comment_id"
]
==
so_easy
.
id
.
to_s
}.
first
feed_so_easy
=
feeds
.
select
{
|
f
|
f
[
"feed_type"
]
==
"post_reply"
and
f
[
"info"
][
"comment_id"
]
==
so_easy
.
id
.
to_s
}.
first
...
@@ -361,10 +354,8 @@ describe "app" do
...
@@ -361,10 +354,8 @@ describe "app" do
feed
[
"info"
][
"comment_body"
]
=
Comment
.
find
(
feed
[
"info"
][
"comment_id"
]).
body
feed
[
"info"
][
"comment_body"
]
=
Comment
.
find
(
feed
[
"info"
][
"comment_id"
]).
body
end
end
end
end
pp
feeds
end
end
end
end
=begin
describe
"POST /api/v1/users/:user_id/follow"
do
describe
"POST /api/v1/users/:user_id/follow"
do
it
"follow user"
do
it
"follow user"
do
...
@@ -395,6 +386,5 @@ describe "app" do
...
@@ -395,6 +386,5 @@ describe "app" do
end
end
end
end
=end
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