From 936dac6baa167b20cd3d2ffbd560d81180b840cb Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Fri, 6 Apr 2012 23:46:44 +0200 Subject: [PATCH] Markdown in comments --- .../backbone/templates/comments/show.jst.hbs | 4 ++-- app/controllers/api/comments_controller.rb | 5 +++-- app/decorators/application_decorator.rb | 10 +++++++++- app/decorators/comment_decorator.rb | 10 ++++++++++ config/initializers/markdown.rb | 5 +++++ spec/decorators/comment_decorator_spec.rb | 5 +++++ 6 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 app/decorators/comment_decorator.rb create mode 100644 config/initializers/markdown.rb create mode 100644 spec/decorators/comment_decorator_spec.rb diff --git a/app/assets/javascripts/backbone/templates/comments/show.jst.hbs b/app/assets/javascripts/backbone/templates/comments/show.jst.hbs index 75618fc..199cc88 100644 --- a/app/assets/javascripts/backbone/templates/comments/show.jst.hbs +++ b/app/assets/javascripts/backbone/templates/comments/show.jst.hbs @@ -1,5 +1,5 @@
- {{user.nickanme}} + {{user.nickname}}
@@ -10,7 +10,7 @@ | remove {{/if}}
-
{{body}}
+
{{{processed_body}}}
diff --git a/app/controllers/api/comments_controller.rb b/app/controllers/api/comments_controller.rb index 1517c67..8bbe0ce 100644 --- a/app/controllers/api/comments_controller.rb +++ b/app/controllers/api/comments_controller.rb @@ -5,7 +5,7 @@ class Api::CommentsController < ApplicationController before_filter :load_asciicast, :only => [:index, :create] def index - respond_with @asciicast.comments + respond_with CommentDecorator.decorate(@asciicast.comments) end def create @@ -15,7 +15,8 @@ class Api::CommentsController < ApplicationController @comment.save - respond_with @comment, :location => api_comment_url(@comment) + comment = CommentDecorator.new(@comment) + respond_with comment, :location => api_comment_url(@comment) end def destroy diff --git a/app/decorators/application_decorator.rb b/app/decorators/application_decorator.rb index 9dd031c..208d91f 100644 --- a/app/decorators/application_decorator.rb +++ b/app/decorators/application_decorator.rb @@ -25,4 +25,12 @@ class ApplicationDecorator < Draper::Base # def updated_at # formatted_timestamp(model.updated_at) # end -end \ No newline at end of file + + def markdown(text) + MKD_RENDERER.render(text) + end + + def as_json(*args) + model.as_json(*args) + end +end diff --git a/app/decorators/comment_decorator.rb b/app/decorators/comment_decorator.rb new file mode 100644 index 0000000..223741f --- /dev/null +++ b/app/decorators/comment_decorator.rb @@ -0,0 +1,10 @@ +class CommentDecorator < ApplicationDecorator + decorates :comment + + def as_json(*args) + data = model.as_json(*args) + data['processed_body'] = markdown(data['body']) + data + end + +end diff --git a/config/initializers/markdown.rb b/config/initializers/markdown.rb new file mode 100644 index 0000000..5971266 --- /dev/null +++ b/config/initializers/markdown.rb @@ -0,0 +1,5 @@ +MKD_RENDERER = Redcarpet::Markdown.new( + Redcarpet::Render::HTML.new(:filter_html => true), + :no_intra_emphasis => true, + :autolink => true +) diff --git a/spec/decorators/comment_decorator_spec.rb b/spec/decorators/comment_decorator_spec.rb new file mode 100644 index 0000000..f379a84 --- /dev/null +++ b/spec/decorators/comment_decorator_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe CommentDecorator do + before { ApplicationController.new.set_current_view_context } +end