diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index b9e7e2f..6a29b07 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -114,10 +114,20 @@ class StoriesController < ApplicationController @short_url = @story.short_id_url @comments = Comment.ordered_for_story_or_thread_for_user(@story.id, nil, - @user ? @user : nil) - @comment = Comment.new + @user) - load_user_votes + respond_to do |format| + format.html { + @comment = Comment.new + + load_user_votes + + render :action => "show" + } + format.json { + render :json => @story.as_json(:with_comments => @comments) + } + end end def show_comment diff --git a/app/models/comment.rb b/app/models/comment.rb index 226b5f6..f42852a 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -59,6 +59,28 @@ class Comment < ActiveRecord::Base nil end + + def as_json(options = {}) + h = super(:only => [ + :short_id, + :created_at, + :updated_at, + :is_deleted, + :is_moderated, + ]) + h[:score] = score + + if self.is_gone? + h[:comment] = "#{self.gone_text}" + else + h[:comment] = markeddown_comment + end + + h[:url] = url + h[:indent_level] = indent_level + h[:commenting_user] = user + h + end def assign_short_id_and_upvote 10.times do |try| diff --git a/app/models/story.rb b/app/models/story.rb index 7a96384..5d23ae9 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -113,12 +113,17 @@ class Story < ActiveRecord::Base :created_at, :title, :url, - :comments_url, ]) h[:score] = score h[:comment_count] = comment_count h[:description] = markeddown_description + h[:comments_url] = comments_url h[:submitter_user] = user + + if options && options[:with_comments] + h[:comments] = options[:with_comments] + end + h end