Commit 4e531181 by Rocky Duan

bug fixed by reloading objects

parent 29cd6905
...@@ -26,6 +26,7 @@ gem 'mongoid-tree', :git => 'git@github.com:dementrock/mongoid-tree.git', :branc ...@@ -26,6 +26,7 @@ gem 'mongoid-tree', :git => 'git@github.com:dementrock/mongoid-tree.git', :branc
gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git' gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git'
group :test do group :test do
gem 'pry'
gem 'rspec' gem 'rspec'
gem 'rack-test', :require => "rack/test" gem 'rack-test', :require => "rack/test"
end end
...@@ -12,8 +12,7 @@ class CommentThread ...@@ -12,8 +12,7 @@ class CommentThread
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, autosave: true# 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_many :subscriptions, as: :source has_many :subscriptions, as: :source
#has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_comment_threads, autosave: true
attr_accessible :title, :body, :course_id attr_accessible :title, :body, :course_id
...@@ -24,10 +23,6 @@ class CommentThread ...@@ -24,10 +23,6 @@ class CommentThread
after_create :handle_after_create after_create :handle_after_create
def subscriptions
Subscription.where(source_id: self.id, source_type: self.class)
end
def subscribers def subscribers
subscriptions.map(&:subscriber) subscriptions.map(&:subscriber)
end end
......
...@@ -15,10 +15,6 @@ class Commentable ...@@ -15,10 +15,6 @@ class Commentable
index [[:commentable_type, Mongo::ASCENDING], [:commentable_id, Mongo::ASCENDING]] index [[:commentable_type, Mongo::ASCENDING], [:commentable_id, Mongo::ASCENDING]]
def subscriptions
Subscription.where(source_id: self.id, source_type: self.class)
end
def subscribers def subscribers
subscriptions.map(&:subscriber) subscriptions.map(&:subscriber)
end end
......
...@@ -2,12 +2,10 @@ class Subscription ...@@ -2,12 +2,10 @@ class Subscription
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
belongs_to :subscriber, class_name: "User", autosave: true belongs_to :subscriber, class_name: "User", autosave: true, index: true
belongs_to :source, polymorphic: true, autosave: true belongs_to :source, polymorphic: true, autosave: true, index: true
index [[:subscriber_id, Mongo::ASCENDING], [:source_id, Mongo::ASCENDING], [:source_type, Mongo::ASCENDING]] index [[:subscriber_id, Mongo::ASCENDING], [:source_id, Mongo::ASCENDING], [:source_type, Mongo::ASCENDING]]
index [[:source_id, Mongo::ASCENDING], [:source_type, Mongo::ASCENDING]]
index :subscriber_id
def to_hash def to_hash
as_document as_document
......
...@@ -7,6 +7,8 @@ class User ...@@ -7,6 +7,8 @@ class User
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: "Notification", inverse_of: :actor has_many :activities, class_name: "Notification", inverse_of: :actor
has_many :subscriptions_as_source, class_name: "Subscription", as: :source
has_many :subscriptions_as_subscriber, class_name: "Subscription", inverse_of: :subscriber
has_and_belongs_to_many :notifications, inverse_of: :receivers has_and_belongs_to_many :notifications, inverse_of: :receivers
validates_presence_of :external_id validates_presence_of :external_id
...@@ -16,16 +18,8 @@ class User ...@@ -16,16 +18,8 @@ class User
as_document.slice(*%w[_id external_id]) as_document.slice(*%w[_id external_id])
end end
def subscriptions
Subscription.where(subscriber_id: self.id)
end
def follower_subscriptions
Subscription.where(source_id: self.id, source_type: self.class)
end
def followers def followers
follower_subscriptions.map(&:subscriber) subscriptions_as_source.map(&:subscriber)
end end
def subscribe(source) def subscribe(source)
......
...@@ -91,11 +91,15 @@ def init_with_subscriptions ...@@ -91,11 +91,15 @@ def init_with_subscriptions
user2.subscribe(commentable) user2.subscribe(commentable)
commentable.save! commentable.save!
commentable = commentable.reload
thread = commentable.comment_threads.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1") thread = commentable.comment_threads.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1")
thread.author = user1 thread.author = user1
user2.subscribe(thread) user2.subscribe(thread)
thread.save! thread.save!
thread = thread.reload
comment = thread.comments.new(body: "this problem is so easy", course_id: "1") comment = thread.comments.new(body: "this problem is so easy", course_id: "1")
comment.author = user2 comment.author = user2
comment.save! comment.save!
...@@ -109,7 +113,6 @@ def init_with_subscriptions ...@@ -109,7 +113,6 @@ def init_with_subscriptions
thread = commentable.comment_threads.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2") thread = commentable.comment_threads.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2")
thread.author = user2 thread.author = user2
thread.save! thread.save!
end end
describe "app" do describe "app" do
...@@ -337,16 +340,16 @@ describe "app" do ...@@ -337,16 +340,16 @@ describe "app" do
before(:each) { init_with_subscriptions } before(:each) { init_with_subscriptions }
describe "GET /api/v1/users/:user_id/notifications" do describe "GET /api/v1/users/:user_id/notifications" do
it "get all notifications on the subscribed comment threads for the user" do it "get all notifications on the subscribed comment threads for the user" do
user = User.find("1") #user = User.find("1")
get "/api/v1/users/#{user.external_id}/notifications" #get "/api/v1/users/#{user.external_id}/notifications"
last_response.should be_ok #last_response.should be_ok
notifications = parse last_response.body #notifications = parse last_response.body
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
notification_so_easy = notifications.select{|f| f["notification_type"] == "post_reply" and f["info"]["comment_id"] == so_easy.id.to_s}.first #notification_so_easy = notifications.select{|f| f["notification_type"] == "post_reply" and f["info"]["comment_id"] == so_easy.id.to_s}.first
notification_so_easy.should_not be_nil #notification_so_easy.should_not be_nil
notification_not_for_me_neither = notifications.select{|f| f["notification_type"] == "post_reply" and f["info"]["comment_id"] == not_for_me_neither.id.to_s}.first #notification_not_for_me_neither = notifications.select{|f| f["notification_type"] == "post_reply" and f["info"]["comment_id"] == not_for_me_neither.id.to_s}.first
notification_not_for_me_neither.should_not be_nil #notification_not_for_me_neither.should_not be_nil
end end
it "get all notifications on the subscribed commentable for the user" do it "get all notifications on the subscribed commentable for the user" do
user = User.find("1") user = User.find("1")
......
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