Commit 9203bad1 by Rocky Duan

specs for comment threads & comments & passed

parent b8b278a1
......@@ -4,11 +4,7 @@ require 'bundler'
Bundler.setup
Bundler.require
require './models/comment.rb'
require './models/comment_thread.rb'
require './models/user.rb'
require './models/commentable.rb'
require './models/feed.rb'
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
desc "Load the environment"
task :environment do
......
......@@ -4,11 +4,7 @@ require 'bundler'
Bundler.setup
Bundler.require
require './models/comment'
require './models/comment_thread'
require './models/commentable'
require './models/user'
require './models/feed'
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index
......@@ -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 '/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.to_hash.to_json
end
......@@ -94,7 +90,8 @@ end
put '/api/v1/comment_threads/:comment_thread_id' do |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
# POST /api/v1/comment_threads/:comment_thread_id/comments
......@@ -130,7 +127,8 @@ end
put '/api/v1/comments/:comment_id' do |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
# POST /api/v1/comments/:comment_id
......@@ -138,7 +136,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]))
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.save!
sub_comment.to_hash.to_json
......
......@@ -13,7 +13,7 @@ class Comment
belongs_to :author, class_name: "User", 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 :course_id # do we really need this?
......@@ -26,8 +26,12 @@ class Comment
nodes.map{|node, sub_nodes| node.to_hash.merge("children" => hash_tree(sub_nodes).compact)}
end
def comment_thread
comment_thread || root.comment_thread
def get_comment_thread
if comment_thread
comment_thread
else
root.comment_thread
end
end
def to_hash(params={})
......@@ -52,14 +56,14 @@ class Comment
feed = Feed.new(
feed_type: "post_reply",
info: {
comment_thread_id: comment_thread.id,
comment_thread_title: comment_thread.title,
comment_thread_id: get_comment_thread.id,
comment_thread_title: get_comment_thread.title,
comment_id: id,
},
)
feed.actor = author
feed.target = self
feed.subscribers << comment_thread.watchers
feed.subscribers << get_comment_thread.watchers
feed.subscribers << author.followers
feed.save!
end
......
......@@ -37,10 +37,10 @@ class CommentThread
feed = Feed.new(
feed_type: "post_topic",
info: {
commentable_id: commentable.id,
commentable_type: commentable.type,
comment_thread_id: comment_thread.id,
comment_thread_title: comment_thread.title,
commentable_id: commentable.commentable_id,
commentable_type: commentable.commentable_type,
comment_thread_id: id,
comment_thread_title: title,
},
)
feed.actor = author
......
......@@ -10,6 +10,8 @@ set :run, false
set :raise_errors, true
set :logging, false
Delayed::Worker.delay_jobs = false
def app
Sinatra::Application
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