From 27d7568a839546566a19c6e7e89e34b7a76c9bc3 Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Mon, 13 Apr 2015 15:51:41 +0200 Subject: [PATCH] comments --- app/assets/stylesheets/public.scss | 42 ++++++++++++++ app/controllers/public/comments_controller.rb | 9 ++- app/models/comment.rb | 1 + app/views/public/articles/show.html.haml | 6 +- app/views/public/comments/_comment.html.haml | 49 +++++++++-------- app/views/public/comments/_form.html.haml | 55 ++++++++++--------- app/views/public/comments/create.js.erb | 10 +--- app/views/public/comments/new.js.erb | 9 +++ ...0150411203736_add_parent_id_to_comments.rb | 5 ++ db/schema.rb | 6 +- 10 files changed, 129 insertions(+), 63 deletions(-) create mode 100644 app/views/public/comments/new.js.erb create mode 100644 db/migrate/20150411203736_add_parent_id_to_comments.rb diff --git a/app/assets/stylesheets/public.scss b/app/assets/stylesheets/public.scss index 7d136ed..d50fae8 100644 --- a/app/assets/stylesheets/public.scss +++ b/app/assets/stylesheets/public.scss @@ -106,3 +106,45 @@ article{ font-size:1em; } + +.comment{ + .header{ + margin-top:0; + font-size:14px; + font-family:"jaf-bernino-sans", sans-serif; + } + aside{ + float:left; + img{ + width:50px; + height:50px; + border-radius:50%; + } + } + .comment_body{ + margin-left:60px; + background:rgba(250,250,250,1); + padding:20px; + margin-bottom:20px; + } + blockquote{ + margin:0; + margin-top:1em; + padding:0; + + } + +} + +.child_comments{ + padding-left:60px; + +} + +.comment_form{ + padding:10px; + background: rgba(250,250,250,1); + margin-bottom:20px; + +} + diff --git a/app/controllers/public/comments_controller.rb b/app/controllers/public/comments_controller.rb index 54745d5..37c70c8 100644 --- a/app/controllers/public/comments_controller.rb +++ b/app/controllers/public/comments_controller.rb @@ -1,11 +1,16 @@ class Public::CommentsController < ApplicationController + def new + @comment = Comment.new(:parent_id => params[:parent_id],:commentable_id => params[:commentable_id],:commentable_type => params[:commentable_type]) + + end + def create - @comment = Comment.new(params.require(:comment).permit(:website, :pseudo, :email, :comment, :commentable_id, :commentable_type)) + @comment = Comment.new(params.require(:comment).permit(:website, :pseudo, :email, :comment, :commentable_id, :commentable_type, :parent_id)) - + @comment.enabled = true if @comment.save commentable = @comment.commentable @comment_id = @comment.id diff --git a/app/models/comment.rb b/app/models/comment.rb index f98402c..abf1303 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -10,6 +10,7 @@ class Comment < ActiveRecord::Base scope :recents, -> {where("enabled = ?",true ).order("created_at ASC")} + acts_as_tree diff --git a/app/views/public/articles/show.html.haml b/app/views/public/articles/show.html.haml index 2e5972b..45b5de1 100644 --- a/app/views/public/articles/show.html.haml +++ b/app/views/public/articles/show.html.haml @@ -11,10 +11,10 @@ #comments %h2 Commentaires - =render :partial => "public/comments/comment", :collection => @article.comments.recents + =render :partial => "public/comments/comment", :collection => @article.comments.where(:enabled => true, :parent_id => nil).order("created_at DESC") - #comment_form.background_opacity.padding_20 - =render :partial => "public/comments/form" + + =render :partial => "public/comments/form" diff --git a/app/views/public/comments/_comment.html.haml b/app/views/public/comments/_comment.html.haml index 7bae05d..c386e28 100644 --- a/app/views/public/comments/_comment.html.haml +++ b/app/views/public/comments/_comment.html.haml @@ -1,26 +1,29 @@ .comment#comment{:class => ("author" if comment.email == "info@nicolasbally.com"), :id => comment.id} - %aside=gravatar_image_tag(comment.email? ? comment.email : comment.pseudo, :gravatar => {:default => "http://#{HOSTNAME}/default_avatar.jpg"}) - - - %p.header - - Commentaire de - %strong - - -if comment.website? - =link_to comment.pseudo, comment.website, :target => "_blank" - -else - =comment.pseudo - - %time.updated{:datetime => Time.now, :pubdate => true} - envoyé - ="le "+l(comment.created_at, :format => :human_date) - - - + %aside=gravatar_image_tag(comment.email? ? comment.email : comment.pseudo, :gravatar => {:default => "http://#{HOSTNAME}/default_avatar.jpg"}) + + + .comment_body + %div.header + .right.coms + =link_to ic(:share), new_public_comment_path(:commentable_id => comment.commentable_id, :commentable_type =>comment.commentable_type, :parent_id => comment.id), :remote => true, :class => "add_comment" + %strong + + -if comment.website? + =link_to(comment.pseudo, comment.website, :target => "_blank")+"," + -else + =comment.pseudo+"," + + + %time.updated{:datetime => Time.now, :pubdate => true} + ="le "+l(comment.created_at, :format => :human_date) + + + + + %blockquote=simple_format comment.comment + + .child_comments + =render comment.children.where(:enabled => true).order(:created_at) + - %blockquote=simple_format comment.comment - - - \ No newline at end of file diff --git a/app/views/public/comments/_form.html.haml b/app/views/public/comments/_form.html.haml index ac122f4..3dc94b8 100644 --- a/app/views/public/comments/_form.html.haml +++ b/app/views/public/comments/_form.html.haml @@ -1,32 +1,33 @@ +#comment_form.comment_form + %h3 + Ajouter un commentaire -%h3 - Ajouter un commentaire + =form_for [:public,(@comment || Comment.new(:commentable_id => @article.id, :commentable_type => "Article"))], :remote => true do |f| + =f.hidden_field :commentable_id + =f.hidden_field :commentable_type + =f.hidden_field :parent_id -=form_for [:public,(@comment || Comment.new(:commentable_id => @article.id, :commentable_type => "Article"))], :remote => true do |f| - =f.hidden_field :commentable_id - =f.hidden_field :commentable_type + %p + =f.label :pseudo, "Pseudo :" + + =f.text_field :pseudo + + %p - %p - =f.label :pseudo, "Pseudo :" - - =f.text_field :pseudo - - %p + =f.label :email, "Email :" + =f.text_field :email + + %p - =f.label :email, "Email :" - =f.text_field :email - - %p + =f.label :website, "Site :" + =f.text_field :website + + %p - =f.label :website, "Site :" - =f.text_field :website - - %p - - =f.label :comment, "Commentaire :" - =f.text_area :comment, :style => "width:98%;height:200px;" - - - - - =f.submit "Envoyer", :class => "submit" \ No newline at end of file + =f.label :comment, "Commentaire :" + =f.text_area :comment, :style => "width:98%;height:200px;" + + + + + =f.submit "Envoyer", :class => "submit btn" \ No newline at end of file diff --git a/app/views/public/comments/create.js.erb b/app/views/public/comments/create.js.erb index b559899..359aa19 100644 --- a/app/views/public/comments/create.js.erb +++ b/app/views/public/comments/create.js.erb @@ -1,12 +1,8 @@ $("#comment_form").html("<%= escape_javascript(render(:partial => "form")) %>"); -$("#comments").html("<%= escape_javascript(render(@comment.commentable.comments.recents)) %>"); +$("#comments").html("<%= escape_javascript(render(@comment.commentable.comments.where(:enabled => true).order("created_at DESC"))) %>"); - - - -$(document.body).animate({ - 'scrollTop': ( $("#comment_<%= @comment_id %>").offset().top - 150) -}, 700); \ No newline at end of file +$('#comments').html("<%= escape_javascript(render(@comment.commentable.comments.where(:enabled => true, :parent_id => nil).order("created_at DESC")))%>"); +$(".add_comment").show(); \ No newline at end of file diff --git a/app/views/public/comments/new.js.erb b/app/views/public/comments/new.js.erb new file mode 100644 index 0000000..af34cd8 --- /dev/null +++ b/app/views/public/comments/new.js.erb @@ -0,0 +1,9 @@ +$("#comment_form").remove(); + +<% if @comment.parent_id == nil %> +$('#comments').append("<%= escape_javascript(render(:partial => "form"))%>"); + +<% else %> +$('#comment_<%= @comment.parent_id %>').children(".child_comments").append("<%= escape_javascript(render(:partial => "form"))%>"); + +<% end %> diff --git a/db/migrate/20150411203736_add_parent_id_to_comments.rb b/db/migrate/20150411203736_add_parent_id_to_comments.rb new file mode 100644 index 0000000..0985192 --- /dev/null +++ b/db/migrate/20150411203736_add_parent_id_to_comments.rb @@ -0,0 +1,5 @@ +class AddParentIdToComments < ActiveRecord::Migration + def change + add_column :comments, :parent_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 8a8f8f7..272c261 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150411165401) do +ActiveRecord::Schema.define(version: 20150411203736) do create_table "admins", force: :cascade do |t| t.string "email", limit: 255, default: "", null: false @@ -106,6 +106,9 @@ ActiveRecord::Schema.define(version: 20150411165401) do t.integer "image_file_id", limit: 4 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "parent_id", limit: 4 + t.integer "position", limit: 4 + t.boolean "front_page", limit: 1 end create_table "cel_tables", force: :cascade do |t| @@ -147,6 +150,7 @@ ActiveRecord::Schema.define(version: 20150411165401) do t.integer "user_id", limit: 4 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "parent_id", limit: 4 end add_index "comments", ["commentable_id"], name: "index_comments_on_commentable_id", using: :btree