Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

...
  • 2 commits
  • 13 files changed
  • 0 commit comments
  • 1 contributor
35 app/assets/javascripts/cache.js.coffee
View
@@ -1,4 +1,20 @@
+class @Flash
+ constructor: ->
+ @div = $('div#flash')
+ update: (type, message) ->
+ $div = @div
+ $div
+ .removeClass()
+ .addClass(type)
+ .html(message)
+ .fadeIn =>
+ setTimeout @hide, 3000
+ hide: =>
+ @div.fadeOut('slow')
+
$ ->
+ window.flash = new Flash
+
# Posts
$posts = $('article.post')
$posts.each (i, html) ->
@@ -95,5 +111,20 @@ $ ->
$comment.fadeOut
done: ->
$comment.remove()
- # if last comment show that there is no comments
-
+ if $('div.comments-articles').empty()
+ $('div.comments-empty').show()
+
+ $new_comment = $('div.new-comment-form form:first')
+ $comment_template = $('div.new-comment-template > article.comment')
+ $new_comment.on 'ajax:success', (e, data) ->
+ comment = $comment_template.clone()
+ comment
+ .removeClass('comment-template')
+ .data('id', data.id)
+ .find('div.comment-body')
+ .html(data.html)
+ $new_comment[0].reset()
+ comment.appendTo $('div.comments-articles')
+ $('div.comments-empty').hide()
+ $new_comment.on 'ajax:error', (e, xhr) ->
+ window.flash.update 'error', xhr.responseText
24 app/assets/stylesheets/styles.less
View
@@ -634,4 +634,26 @@ a.secondary {
width: 42px;
height: 42px;
}
-}
+}
+
+div#flash {
+ position: fixed;
+ z-index: 100;
+ font-weight: bold;
+ top: 0;
+ left: 0;
+ width: 100%;
+ text-align: center;
+ color: white;
+ line-height: 30px;
+ display: none;
+
+ &.error {
+ background: #FF7E79;
+ border-bottom: 2px solid #FF7E79 * 1.5;
+ }
+ &.notice {
+ background: #00ce00;
+ border-bottom: 2px solid #00ce00 * 1.5;
+ }
+}
8 app/controllers/application_controller.rb
View
@@ -2,9 +2,11 @@ class ApplicationController < ActionController::Base
enable_authorization
protect_from_forgery
- rescue_from Exception, with: :notify_batman
- rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
- rescue_from CanCan::Unauthorized, with: :handle_unauthorized
+ if Rails.env.production?
+ rescue_from Exception, with: :notify_batman
+ rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
+ rescue_from CanCan::Unauthorized, with: :handle_unauthorized
+ end
helper_method :user_signed_in?, :current_user, :sidebar_tags
prepend_before_filter :token_authentication
22 app/controllers/posts/comments_controller.rb
View
@@ -4,17 +4,14 @@ class Posts::CommentsController < ApplicationController
def create
@post = find_post(params[:post_id])
- @new_comment = @post.comments.build(params[:comment]) do |comment|
+ @comment = @post.comments.build(params[:comment]) do |comment|
comment.user = current_user
end
-
- if @new_comment.save
- @comment = @post.comments.build do |comment|
- comment.user = current_user
- end
- render json: { form: render_form(:build), comment: render_comment }
+ if @comment.save
+ render json: { html: @comment.html, id: @comment.id }, status: :created
else
- render_json_error("We are sorry, but comment couldn't be saved.")
+ errors = @comment.errors.to_a.to_sentence
+ render text: errors, status: :unprocessable_entity
end
end
@@ -48,13 +45,4 @@ def destroy
def find_post(id)
Post.find_by_param id
end
-
- def render_form(action)
- @action = action
- render_to_string(inline: "<%= render partial: 'form', locals: { comment: @comment, action: @action } %>")
- end
-
- def render_comment
- render_to_string(inline: "<%= render partial: 'comment', locals: { comment: @new_comment } %>")
- end
end
8 app/controllers/posts_controller.rb
View
@@ -28,8 +28,12 @@ def bookmarks
def show
@post = Post.find_by_param params[:id]
- redirect_to(@post, status: 301) if params[:id] != @post.to_param
- @comment = @post.comments.build if can? :create, :comments
+ redirect_to(@post, status: 301) if params[:id] != @post.to_param
+ if can? :create, :comments
+ @comment = @post.comments.build do |c|
+ c.user = current_user
+ end
+ end
@post.update_column(:page_views, @post.page_views + 1)
end
0  app/views/posts/comments/_build.html.slim → app/views/comments/_build.html.slim
View
File renamed without changes
31 app/views/comments/_comment.html.slim
View
@@ -0,0 +1,31 @@
+article class="comment" data-author="#{comment.user.username}" data-id="#{comment.id}"
+ div class="comment-author"
+ = link_to comment.user, title: comment.user.username do
+ = avatar_image comment.user, 84
+ div class="comment-container"
+ div class="comment-title"
+ = link_to comment.user, comment.user.username
+ | commented
+ = time_ago_in_words comment.created_at || Time.current
+ | ago
+ div class="comment-body markdown"
+ == Markdown.markdown comment.content, flavored: true
+ div class="comment-controls hidden"
+ ul
+ li class="comment-control-inactive"
+ = link_to '#', class: 'comment-control-edit secondary', remote: true do
+ i class="icon-pencil"
+ span Edit
+ li class="comment-control-inactive"
+ = link_to '#', class: 'comment-control-remove secondary', remote: true, method: :delete, data: { confirm: 'Are you sure?' } do
+ i class="icon-trash"
+ span Remove
+ li class="comment-control-active hidden"
+ = link_to '#', class: 'comment-control-save secondary', remote: true, method: :put do
+ i class="icon-save"
+ span Save
+ li class="comment-control-active hidden"
+ = link_to '#', class: 'comment-control-cancel secondary', remote: true do
+ i class="icon-share-alt"
+ span Cancel
+ div class="clear"
0  app/views/posts/comments/_edit.html.slim → app/views/comments/_edit.html.slim
View
File renamed without changes
0  app/views/posts/comments/_form.html.slim → app/views/comments/_form.html.slim
View
File renamed without changes
0  app/views/posts/comments/_preview.html.slim → app/views/comments/_preview.html.slim
View
File renamed without changes
1  app/views/layouts/new_application.html.slim
View
@@ -54,3 +54,4 @@ html
li
a class="secondary" href="#" Map
div class="clear"
+ div id="flash"
51 app/views/posts/_comments.html.slim
View
@@ -2,53 +2,26 @@ section class="comments"
header
h1 class="comments-title subtitle" Comments
- - comments = @post.persisted_comments
- - if comments.any?
- - comments.each do |comment|
- article class="comment" data-author="#{comment.user.username}"
- div class="comment-author"
- = link_to comment.user, title: comment.user.username do
- = avatar_image comment.user, 84
- div class="comment-container"
- div class="comment-title"
- = link_to comment.user, comment.user.username
- | commented
- = time_ago_in_words comment.created_at
- | ago
- div class="comment-body markdown"
- == Markdown.markdown comment.content, flavored: true
- div class="comment-controls hidden"
- ul
- li class="comment-control-inactive"
- = link_to [:edit, @post, comment], class: 'comment-control-edit secondary', remote: true do
- i class="icon-pencil"
- span Edit
- li class="comment-control-inactive"
- = link_to [@post, comment], class: 'comment-control-remove secondary', remote: true, method: :delete, data: { confirm: 'Are you sure?' } do
- i class="icon-trash"
- span Remove
- li class="comment-control-active hidden"
- = link_to [@post, comment], class: 'comment-control-save secondary', remote: true, method: :put do
- i class="icon-save"
- span Save
- li class="comment-control-active hidden"
- = link_to [@post, comment], class: 'comment-control-cancel secondary', remote: true do
- i class="icon-share-alt"
- span Cancel
- div class="clear"
- - else
+ div class="comments-articles"
+ - comments = @post.persisted_comments
+ - if comments.any?
+ - comments.each do |comment|
+ = render comment
+ - if comments.empty?
p class="comments-empty" No comments yet.
- if user_signed_in?
h2 class="new-comment-title" New Comment
+ div class="new-comment-template hidden"
+ = render @comment
div class="new-comment-author"
= link_to current_user, title: current_user.username do
= avatar_image current_user, 84
div class="new-comment-form"
- = form_for [@post, @comment] do |f|
+ = form_for @comment, url: post_comments_path(@post, format: :js), remote: true do |f|
fieldset
- = f.text_area :body, rows: 3
+ = f.text_area :content, rows: 3
div class="form-actions"
- = f.submit 'Preview', class: 'button'
- = f.submit 'Comment', class: 'button'
+ = f.submit 'Preview', class: 'button', data: { action: api_markdown_path }
+ = f.submit 'Comment', class: 'button', data: { action: url_for([@post, :comments]) }
div class="clear"
13 app/views/posts/comments/_comment.html.slim
View
@@ -1,13 +0,0 @@
-article class="comment" data-author="#{comment.user}"
- header
- h1= comment_title comment
- div class="avatar"
- = link_to comment.user do
- = avatar_image comment.user
-
- div class="controls"
- - if can?(:edit, comment) && comment.persisted?
- ul
- li= link_to 'Edit', edit_post_comment_path(comment.post, comment), remote: true, class: :edit_comment
- li= link_to 'Delete', [comment.post, comment], method: :delete, remote: true, class: :delete_comment
- div class="markdown group" = comment_content comment

No commit comments for this range

Something went wrong with that request. Please try again.