do not use external_id for users; store id directly
This commit is contained in:
Родитель
aa5c09203d
Коммит
47c2e04a03
6
Rakefile
6
Rakefile
|
@ -26,7 +26,7 @@ namespace :test do
|
|||
|
||||
commentable = Commentable.create!(commentable_type: "questions", commentable_id: "1")
|
||||
|
||||
user = User.create!(external_id: "1")
|
||||
user = User.create!(id: "1")
|
||||
|
||||
comment_thread = commentable.comment_threads.create!(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1")
|
||||
comment_thread.author = user
|
||||
|
@ -78,7 +78,7 @@ namespace :db do
|
|||
|
||||
level_limit = YAML.load_file("config/application.yml")["level_limit"]
|
||||
|
||||
user = User.create!(external_id: "1")
|
||||
user = User.create!(id: "1")
|
||||
|
||||
def generate_comments(commentable_type, commentable_id, level_limit, user)
|
||||
commentable = Commentable.create!(commentable_type: commentable_type, commentable_id: commentable_id)
|
||||
|
@ -115,7 +115,7 @@ namespace :db do
|
|||
puts "voting"
|
||||
users = []
|
||||
(1..10).each do |id|
|
||||
users << User.find_or_create_by(external_id: id.to_s)
|
||||
users << User.find_or_create_by(id: id.to_s)
|
||||
end
|
||||
|
||||
CommentThread.all.each do |c|
|
||||
|
|
32
app.rb
32
app.rb
|
@ -71,7 +71,7 @@ end
|
|||
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)
|
||||
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(id: params["user_id"])
|
||||
comment_thread.save!
|
||||
comment_thread.to_hash.to_json
|
||||
end
|
||||
|
@ -99,7 +99,7 @@ end
|
|||
post '/api/v1/comment_threads/:comment_thread_id/comments' do |comment_thread_id|
|
||||
comment_thread = CommentThread.find(comment_thread_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(id: params["user_id"])
|
||||
comment.save!
|
||||
comment.to_hash.to_json
|
||||
end
|
||||
|
@ -137,7 +137,7 @@ end
|
|||
post '/api/v1/comments/:comment_id' do |comment_id|
|
||||
comment = Comment.find(comment_id)
|
||||
sub_comment = comment.children.new(params.slice(*%w[body course_id]))
|
||||
sub_comment.author = User.find_or_create_by(external_id: params["user_id"])
|
||||
sub_comment.author = User.find_or_create_by(id: params["user_id"])
|
||||
sub_comment.save!
|
||||
sub_comment.to_hash.to_json
|
||||
end
|
||||
|
@ -156,7 +156,7 @@ end
|
|||
|
||||
put '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_id|
|
||||
comment = Comment.find(comment_id)
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
user.vote(comment, params["value"].intern)
|
||||
Comment.find(comment_id).to_hash.to_json
|
||||
end
|
||||
|
@ -166,7 +166,7 @@ end
|
|||
|
||||
delete '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_id|
|
||||
comment = Comment.find(comment_id)
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
user.unvote(comment)
|
||||
Comment.find(comment_id).to_hash.to_json
|
||||
end
|
||||
|
@ -176,7 +176,7 @@ end
|
|||
|
||||
put '/api/v1/votes/comment_threads/:comment_thread_id/users/:user_id' do |comment_thread_id, user_id|
|
||||
comment_thread = CommentThread.find(comment_thread_id)
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
user.vote(comment_thread, params["value"].intern)
|
||||
CommentThread.find(comment_thread_id).to_hash.to_json
|
||||
end
|
||||
|
@ -186,7 +186,7 @@ end
|
|||
|
||||
delete '/api/v1/votes/comment_threads/:comment_thread_id/users/:user_id' do |comment_thread_id, user_id|
|
||||
comment_thread = CommentThread.find(comment_thread_id)
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
user.unvote(comment_thread)
|
||||
CommentThread.find(comment_thread_id).to_hash.to_json
|
||||
end
|
||||
|
@ -195,7 +195,7 @@ end
|
|||
# get all subscribed feeds for the user
|
||||
|
||||
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(id: user_id)
|
||||
user.feeds.map(&:to_hash).to_json
|
||||
end
|
||||
|
||||
|
@ -203,8 +203,8 @@ end
|
|||
# follow user
|
||||
|
||||
post '/api/v1/users/:user_id/follow' do |user_id|
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
followed_user = User.find_or_create_by(external_id: params[:user_id])
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
followed_user = User.find_or_create_by(id: params[:user_id])
|
||||
user.follow(followed_user)
|
||||
user.to_hash.to_json
|
||||
end
|
||||
|
@ -213,8 +213,8 @@ end
|
|||
# unfollow user
|
||||
|
||||
post '/api/v1/users/:user_id/unfollow' do |user_id|
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
followed_user = User.find_or_create_by(external_id: params[:user_id])
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
followed_user = User.find_or_create_by(id: params[:user_id])
|
||||
user.unfollow(followed_user)
|
||||
user.to_hash.to_json
|
||||
end
|
||||
|
@ -223,7 +223,7 @@ end
|
|||
# watch a commentable
|
||||
|
||||
post '/api/v1/users/:user_id/watch/commentable' do |user_id|
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
commentable = Commentable.find_or_create_by(commentable_type: params[:commentable_type],
|
||||
commentable_id: parasm[:commentable_id])
|
||||
user.watch_commentable(commentable)
|
||||
|
@ -234,7 +234,7 @@ end
|
|||
# unwatch a commentable
|
||||
|
||||
post '/api/v1/users/:user_id/unwatch/commentable' do |user_id|
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
commentable = Commentable.find_or_create_by(commentable_type: params[:commentable_type],
|
||||
commentable_id: parasm[:commentable_id])
|
||||
user.unwatch_commentable(commentable)
|
||||
|
@ -245,7 +245,7 @@ end
|
|||
# watch a comment thread
|
||||
|
||||
post '/api/v1/users/:user_id/watch/comment_thread' do |user_id|
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
comment_thread = CommentThread.find(params[:comment_thread_id])
|
||||
user.watch_comment_thread(comment_thread)
|
||||
user.to_hash.to_json
|
||||
|
@ -255,7 +255,7 @@ end
|
|||
# unwatch a comment thread
|
||||
|
||||
post '/api/v1/users/:user_id/unwatch/comment_thread' do |user_id|
|
||||
user = User.find_or_create_by(external_id: user_id)
|
||||
user = User.find_or_create_by(id: user_id)
|
||||
comment_thread = CommentThread.find(params[:comment_thread_id])
|
||||
user.unwatch_comment_thread(comment_thread)
|
||||
user.to_hash.to_json
|
||||
|
|
|
@ -47,7 +47,7 @@ class Comment
|
|||
self.class.hash_tree(subtree(sort: sort_by_parent_and_time)).first
|
||||
else
|
||||
as_document.slice(*%w[body course_id endorsed _id]).
|
||||
merge("user_id" => author.external_id).
|
||||
merge("user_id" => author.id).
|
||||
merge("votes" => votes.slice(*%w[count up_count down_count point]))
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,7 +25,7 @@ class CommentThread
|
|||
|
||||
def to_hash(params={})
|
||||
doc = as_document.slice(*%w[title body course_id _id]).
|
||||
merge("user_id" => author.external_id).
|
||||
merge("user_id" => author.id).
|
||||
merge("votes" => votes.slice(*%w[count up_count down_count point]))
|
||||
if params[:recursive]
|
||||
doc = doc.merge("children" => comments.map{|c| c.to_hash(recursive: true)})
|
||||
|
|
|
@ -2,7 +2,7 @@ class User
|
|||
include Mongoid::Document
|
||||
include Mongo::Voter
|
||||
|
||||
field :external_id, type: String
|
||||
identity type: String
|
||||
|
||||
has_many :comments
|
||||
has_many :comment_threads, inverse_of: :author
|
||||
|
@ -11,15 +11,8 @@ class User
|
|||
has_and_belongs_to_many :followers, class_name: "User", inverse_of: :followings
|
||||
has_and_belongs_to_many :followings, class_name: "User", inverse_of: :followers
|
||||
|
||||
attr_accessible :external_id
|
||||
|
||||
validates_uniqueness_of :external_id
|
||||
validates_presence_of :external_id
|
||||
|
||||
index :external_id, unique: true
|
||||
|
||||
def to_hash(params={})
|
||||
as_document.slice(*%w[_id external_id])
|
||||
as_document.slice(*%w[_id])
|
||||
end
|
||||
|
||||
def follow(user)
|
||||
|
|
|
@ -18,7 +18,7 @@ def init_without_feeds
|
|||
commentable = Commentable.new(commentable_type: "questions", commentable_id: "1")
|
||||
commentable.save!
|
||||
|
||||
user = User.create!(external_id: "1")
|
||||
user = User.create!(id: "1")
|
||||
|
||||
comment_thread = commentable.comment_threads.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1")
|
||||
comment_thread.author = user
|
||||
|
@ -58,7 +58,7 @@ def init_without_feeds
|
|||
comment1.author = user
|
||||
comment1.save!
|
||||
|
||||
users = (2..10).map{|id| User.find_or_create_by(external_id: id.to_s)}
|
||||
users = (2..10).map{|id| User.find_or_create_by(id: id.to_s)}
|
||||
|
||||
Comment.all.each do |c|
|
||||
user.vote(c, :up) # make the first user always vote up for convenience
|
||||
|
@ -163,13 +163,13 @@ describe "app" do
|
|||
it "create a comment to the comment thread" do
|
||||
comment_thread = CommentThread.first.to_hash(recursive: true)
|
||||
user = User.first
|
||||
post "/api/v1/comment_threads/#{comment_thread["_id"]}/comments", body: "new comment", course_id: "1", user_id: User.first.external_id
|
||||
post "/api/v1/comment_threads/#{comment_thread["_id"]}/comments", body: "new comment", course_id: "1", user_id: User.first.id
|
||||
last_response.should be_ok
|
||||
changed_thread = CommentThread.find(comment_thread["_id"]).to_hash(recursive: true)
|
||||
changed_thread["children"].length.should == comment_thread["children"].length + 1
|
||||
comment = changed_thread["children"].select{|c| c["body"] == "new comment"}.first
|
||||
comment.should_not be_nil
|
||||
comment["user_id"].should == user.external_id
|
||||
comment["user_id"].should == user.id
|
||||
end
|
||||
end
|
||||
describe "DELETE /api/v1/comment_threads/:comment_thread_id" do
|
||||
|
@ -223,13 +223,13 @@ describe "app" do
|
|||
it "create a sub comment to the comment" do
|
||||
comment = Comment.first.to_hash(recursive: true)
|
||||
user = User.first
|
||||
post "/api/v1/comments/#{comment["_id"]}", body: "new comment", course_id: "1", user_id: User.first.external_id
|
||||
post "/api/v1/comments/#{comment["_id"]}", body: "new comment", course_id: "1", user_id: User.first.id
|
||||
last_response.should be_ok
|
||||
changed_comment = Comment.find(comment["_id"]).to_hash(recursive: true)
|
||||
changed_comment["children"].length.should == comment["children"].length + 1
|
||||
subcomment = changed_comment["children"].select{|c| c["body"] == "new comment"}.first
|
||||
subcomment.should_not be_nil
|
||||
subcomment["user_id"].should == user.external_id
|
||||
subcomment["user_id"].should == user.id
|
||||
end
|
||||
end
|
||||
describe "DELETE /api/v1/comments/:comment_id" do
|
||||
|
@ -251,7 +251,7 @@ describe "app" do
|
|||
comment = Comment.first
|
||||
prev_up_votes = comment.up_votes_count
|
||||
prev_down_votes = comment.down_votes_count
|
||||
put "/api/v1/votes/comments/#{comment.id}/users/#{user.external_id}", value: "down"
|
||||
put "/api/v1/votes/comments/#{comment.id}/users/#{user.id}", value: "down"
|
||||
comment = Comment.find(comment.id)
|
||||
comment.up_votes_count.should == prev_up_votes - 1
|
||||
comment.down_votes_count.should == prev_down_votes + 1
|
||||
|
@ -263,7 +263,7 @@ describe "app" do
|
|||
comment = Comment.first
|
||||
prev_up_votes = comment.up_votes_count
|
||||
prev_down_votes = comment.down_votes_count
|
||||
delete "/api/v1/votes/comments/#{comment.id}/users/#{user.external_id}"
|
||||
delete "/api/v1/votes/comments/#{comment.id}/users/#{user.id}"
|
||||
comment = Comment.find(comment.id)
|
||||
comment.up_votes_count.should == prev_up_votes - 1
|
||||
comment.down_votes_count.should == prev_down_votes
|
||||
|
@ -275,7 +275,7 @@ describe "app" do
|
|||
comment_thread = CommentThread.first
|
||||
prev_up_votes = comment_thread.up_votes_count
|
||||
prev_down_votes = comment_thread.down_votes_count
|
||||
put "/api/v1/votes/comment_threads/#{comment_thread.id}/users/#{user.external_id}", value: "down"
|
||||
put "/api/v1/votes/comment_threads/#{comment_thread.id}/users/#{user.id}", value: "down"
|
||||
comment_thread = CommentThread.find(comment_thread.id)
|
||||
comment_thread.up_votes_count.should == prev_up_votes - 1
|
||||
comment_thread.down_votes_count.should == prev_down_votes + 1
|
||||
|
@ -287,7 +287,7 @@ describe "app" do
|
|||
comment_thread = CommentThread.first
|
||||
prev_up_votes = comment_thread.up_votes_count
|
||||
prev_down_votes = comment_thread.down_votes_count
|
||||
delete "/api/v1/votes/comment_threads/#{comment_thread.id}/users/#{user.external_id}"
|
||||
delete "/api/v1/votes/comment_threads/#{comment_thread.id}/users/#{user.id}"
|
||||
comment_thread = CommentThread.find(comment_thread.id)
|
||||
comment_thread.up_votes_count.should == prev_up_votes - 1
|
||||
comment_thread.down_votes_count.should == prev_down_votes
|
||||
|
|
Загрузка…
Ссылка в новой задаче