Commit 1d1ca019 by Rocky Duan

bug caused by autosave indeed; fixed previous autosave bug

parent cb1c0abb
...@@ -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
......
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: true allow_anonymity: false
...@@ -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
...@@ -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
...@@ -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
......
...@@ -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
if not CommentService.config["allow_anonymity"]
validates_presence_of :actor 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
......
...@@ -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
......
...@@ -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
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