Commit 9203bad1 by Rocky Duan

specs for comment threads & comments & passed

parent b8b278a1
...@@ -4,11 +4,7 @@ require 'bundler' ...@@ -4,11 +4,7 @@ require 'bundler'
Bundler.setup Bundler.setup
Bundler.require Bundler.require
require './models/comment.rb' Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
require './models/comment_thread.rb'
require './models/user.rb'
require './models/commentable.rb'
require './models/feed.rb'
desc "Load the environment" desc "Load the environment"
task :environment do task :environment do
......
...@@ -4,11 +4,7 @@ require 'bundler' ...@@ -4,11 +4,7 @@ require 'bundler'
Bundler.setup Bundler.setup
Bundler.require Bundler.require
require './models/comment' Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
require './models/comment_thread'
require './models/commentable'
require './models/user'
require './models/feed'
env_index = ARGV.index("-e") env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index env_arg = ARGV[env_index + 1] if env_index
...@@ -55,7 +51,7 @@ config = YAML.load_file("config/application.yml") ...@@ -55,7 +51,7 @@ config = YAML.load_file("config/application.yml")
# delete the commentable object and all of its associated comment threads and comments # delete the commentable object and all of its associated comment threads and comments
delete '/api/v1/commentables/:commentable_type/:commentable_id' do |commentable_type, commentable_id| delete '/api/v1/commentables/:commentable_type/:commentable_id' do |commentable_type, commentable_id|
commentable = Commentable.find_or_create_by(commentable_type: commentable_type, commentable_id: commentable_id) commentable = Commentable.find_or_initialize_by(commentable_type: commentable_type, commentable_id: commentable_id)
commentable.destroy commentable.destroy
commentable.to_hash.to_json commentable.to_hash.to_json
end end
...@@ -94,7 +90,8 @@ end ...@@ -94,7 +90,8 @@ end
put '/api/v1/comment_threads/:comment_thread_id' do |comment_thread_id| put '/api/v1/comment_threads/:comment_thread_id' do |comment_thread_id|
comment_thread = CommentThread.find(comment_thread_id) comment_thread = CommentThread.find(comment_thread_id)
comment_thread.update!(params.slice(*%w[title body endorsed])).to_hash.to_json comment_thread.update_attributes!(params.slice(*%w[title body]))
comment_thread.to_hash.to_json
end end
# POST /api/v1/comment_threads/:comment_thread_id/comments # POST /api/v1/comment_threads/:comment_thread_id/comments
...@@ -130,7 +127,8 @@ end ...@@ -130,7 +127,8 @@ end
put '/api/v1/comments/:comment_id' do |comment_id| put '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
comment.update!(params.slice(*%w[body endorsed])).to_hash.to_json comment.update_attributes!(params.slice(*%w[body endorsed]))
comment.to_hash.to_json
end end
# POST /api/v1/comments/:comment_id # POST /api/v1/comments/:comment_id
...@@ -138,7 +136,7 @@ end ...@@ -138,7 +136,7 @@ end
post '/api/v1/comments/:comment_id' do |comment_id| post '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
sub_comment = comment.children.new(params.slice(*%w[body])) 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(external_id: params["user_id"])
sub_comment.save! sub_comment.save!
sub_comment.to_hash.to_json sub_comment.to_hash.to_json
......
...@@ -13,7 +13,7 @@ class Comment ...@@ -13,7 +13,7 @@ class Comment
belongs_to :author, class_name: "User", index: true belongs_to :author, class_name: "User", index: true
belongs_to :comment_thread, index: true belongs_to :comment_thread, index: true
attr_accessible :body, :course_id attr_accessible :body, :course_id, :endorsed
validates_presence_of :body validates_presence_of :body
validates_presence_of :course_id # do we really need this? validates_presence_of :course_id # do we really need this?
...@@ -26,8 +26,12 @@ class Comment ...@@ -26,8 +26,12 @@ class Comment
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 comment_thread def get_comment_thread
comment_thread || root.comment_thread if comment_thread
comment_thread
else
root.comment_thread
end
end end
def to_hash(params={}) def to_hash(params={})
...@@ -52,14 +56,14 @@ class Comment ...@@ -52,14 +56,14 @@ class Comment
feed = Feed.new( feed = Feed.new(
feed_type: "post_reply", feed_type: "post_reply",
info: { info: {
comment_thread_id: comment_thread.id, comment_thread_id: get_comment_thread.id,
comment_thread_title: comment_thread.title, comment_thread_title: get_comment_thread.title,
comment_id: id, comment_id: id,
}, },
) )
feed.actor = author feed.actor = author
feed.target = self feed.target = self
feed.subscribers << comment_thread.watchers feed.subscribers << get_comment_thread.watchers
feed.subscribers << author.followers feed.subscribers << author.followers
feed.save! feed.save!
end end
......
...@@ -37,10 +37,10 @@ class CommentThread ...@@ -37,10 +37,10 @@ class CommentThread
feed = Feed.new( feed = Feed.new(
feed_type: "post_topic", feed_type: "post_topic",
info: { info: {
commentable_id: commentable.id, commentable_id: commentable.commentable_id,
commentable_type: commentable.type, commentable_type: commentable.commentable_type,
comment_thread_id: comment_thread.id, comment_thread_id: id,
comment_thread_title: comment_thread.title, comment_thread_title: title,
}, },
) )
feed.actor = author feed.actor = author
......
...@@ -10,6 +10,8 @@ set :run, false ...@@ -10,6 +10,8 @@ set :run, false
set :raise_errors, true set :raise_errors, true
set :logging, false set :logging, false
Delayed::Worker.delay_jobs = false
def app def app
Sinatra::Application Sinatra::Application
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