From 234880d90e91e5569bb2ed5328f49cc5f78b1cdb Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Sat, 1 Aug 2015 12:03:01 +0200 Subject: [PATCH] AutoCommit Sam Aug 1 12:03:01 CEST 2015 --- app/controllers/comments_controller.rb | 15 +++++++++----- app/helpers/comments_helper.rb | 3 +-- app/helpers/plugins_helper.rb | 13 +++++++----- app/models/comment.rb | 13 ++++++++---- app/views/comments/_comment.html.erb | 12 ++++++----- app/views/comments/_comments.html.erb | 22 ++++++++++----------- app/views/comments/_comments_block.html.erb | 13 ++++++++++++ app/views/comments/_form.html.erb | 22 ++++++++++----------- app/views/comments/create.js.erb | 4 ++-- app/views/comments/destroy.js.erb | 3 ++- app/views/comments/new.js.erb | 3 ++- app/views/survey/questions/show.html.erb | 15 +------------- config/routes.rb | 1 + 13 files changed, 78 insertions(+), 61 deletions(-) create mode 100644 app/views/comments/_comments_block.html.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index eff788b..5d64e40 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -2,8 +2,8 @@ class CommentsController < ApplicationController before_filter :decode_commentable_type def index @commentable=params[:commentable_type].constantize.find(params[:commentable_id]) unless params[:commentable_type].nil? or params[:commentable_id].nil? -num = {"Beispiel"=> 2, "Survey::Question"=> 7} - @comments=@commentable.comments.order(:created_at).roots.page(params[:page]).per(num[params[:commentable_type]]).reverse_order + + @comments=@commentable.comments.order(:created_at).roots.page(params[:page]).per(Comment::NUM[params[:commentable_type]]).reverse_order respond_to do |format| format.html # new.html.erb format.json { render json: @comment } @@ -31,6 +31,8 @@ num = {"Beispiel"=> 2, "Survey::Question"=> 7} def new @comment = Comment.new @comment.commentable=params[:commentable_type].constantize.find(params[:commentable_id]) unless params[:commentable_type].nil? or params[:commentable_id].nil? + + respond_to do |format| format.html # new.html.erb format.json { render json: @comment } @@ -51,6 +53,8 @@ num = {"Beispiel"=> 2, "Survey::Question"=> 7} c = params[:comment][:commentable_type].constantize.find(params[:comment][:commentable_id]) unless params[:comment][:commentable_type].nil? or params[:comment][:commentable_id].nil? @comment = Comment.build_for(c, current_user,"", params_new) + @comments=@comment.parent_object.comments.order(:created_at).roots.page(params[:page]).per(Comment::NUM[params[:commentable_type]]).reverse_order + respond_to do |format| if @comment format.html { redirect_to @comment.commentable, notice: 'Comment was successfully created.', show_comments: true } @@ -88,9 +92,10 @@ num = {"Beispiel"=> 2, "Survey::Question"=> 7} def destroy @comment = Comment.find(params[:id]) @commentable=@comment.commentable - @divid=@comment.divid - @comment.destroy + + @comment.destroy + @comments=@commentable.comments.order(:created_at).roots.page(params[:page]).per(Comment::NUM[params[:commentable_type]]).reverse_order respond_to do |format| format.html { redirect_to @commentable, :action=>"show"} format.json { head :no_content } @@ -99,7 +104,7 @@ num = {"Beispiel"=> 2, "Survey::Question"=> 7} end private def decode_commentable_type - params[:commentable_type].gsub("_","::") + params[:commentable_type].gsub!("_","::") unless params[:commentable_type].nil? end end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 2706ddb..363b793 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -3,7 +3,6 @@ module CommentsHelper divid_for(o,"comments") end def render_comments_for(o) - - + render partial: "comments/comments_block", object: o end end diff --git a/app/helpers/plugins_helper.rb b/app/helpers/plugins_helper.rb index 71e048b..d002cdc 100644 --- a/app/helpers/plugins_helper.rb +++ b/app/helpers/plugins_helper.rb @@ -1,11 +1,14 @@ module PluginsHelper - def divid_for(object,suffix="") - if object.nil? + def divid_for(obj,suffix="") + if obj.nil? "" else - object.class.to_s.to_lower.gsub("::","_")+ suffix.to_lower + object.id.to_s + obj.class.to_s.downcase.gsub("::","_")+ "_" + suffix.downcase + "_" + obj.id.to_s end end - + def div_tag_for(o,&block) + content = capture(&block) + content_tag(:div, content, :id=>divid_for(o)) + end end -o + diff --git a/app/models/comment.rb b/app/models/comment.rb index 317c539..3547121 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -9,7 +9,14 @@ class Comment < ActiveRecord::Base validate :user, :presence=>true validate :commentable, :presence=>true include IsCommentable - +NUM = {"Beispiel"=> 2, "Survey::Question"=> 7} + def parent_object + if self.root? + self.commentable + else + self.parent + end + end def self.build_for(set_commentable, user, text,attr={}) c = new raise "Tried to build comment for non commentable" unless set_commentable.try(:is_commentable?) @@ -39,9 +46,7 @@ class Comment < ActiveRecord::Base def self.switchshowid_for(c) "show_comments_" + c.class.to_s.gsub(":","_") + "_" + c.id.to_s end - def divid - "comment_" + id.to_s - end + def formid "comment_form_" + commentable_type.gsub(":","_") + "_" + commentable_id.to_s end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 6468ee6..098b241 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,7 +1,9 @@ -
+
<%= image_tag comment.thumb_url %>
- <%= (!comment.anonym) ? comment.user.try(:email) : "Anonym" %> (<%= I18n.l(comment.created_at) %>) + <%= (!comment.anonym) ? comment.user.try(:email) : "Anonym" %> + (<%= I18n.l(comment.created_at) %>) + <%= link_to ffi1_icon("remove9"), comment, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>:

<%= comment.text %> @@ -9,9 +11,9 @@
<%= link_to "comment" , new_comment_path( commentable_type: "Comment", commentable_id: comment.id), remote:true %> <% end %>

-
-
- <%= render partial:"comments/comments", object: comment.children.order(:created_at).reverse_order if comment.children.size >0 %> +
"> +
+<%= render partial:"comments/comments", object: comment.children.order(:created_at).reverse_order if comment.children.size >0 %>
diff --git a/app/views/comments/_comments.html.erb b/app/views/comments/_comments.html.erb index 4cda60e..0c5cb19 100644 --- a/app/views/comments/_comments.html.erb +++ b/app/views/comments/_comments.html.erb @@ -1,12 +1,12 @@ -<% unless comments.empty? %> -
-
    -<% comments.each do |c| %> -
  • <%= render c %>
  • -<% end %> -
-
-<% if comments.first.root? %> -<%= paginate comments, :remote=>true , :theme=>'twitter-bootstrap'%> -<% end %> +<% unless comments.nil? || comments.empty? %> +
+
    + <% comments.each do |c| %> +
  • <%= render c %>
  • + <% end %> +
+
+ <% if comments.first.root? %> + <%= paginate comments, :remote=>true , :theme=>'twitter-bootstrap'%> + <% end %> <% end %> diff --git a/app/views/comments/_comments_block.html.erb b/app/views/comments/_comments_block.html.erb new file mode 100644 index 0000000..3eec841 --- /dev/null +++ b/app/views/comments/_comments_block.html.erb @@ -0,0 +1,13 @@ +<%= link_to "Comment" , new_comment_path( commentable_type: comments_block.class.to_s.gsub("::","_"), commentable_id: comments_block.id), remote:true if can? :comment, comments_block %> <%# This is the button to add a new comment %> +<%= link_to "comments:.."+ comments_block.comments.count().to_s, comments_path(commentable_type: "Survey_Question", commentable_id: comments_block.id), remote:true, id: Comment.switchshowid_for( comments_block) %> <%# load the comments in the comments block %> +
"> +
+<% unless comments_block.comments.roots.empty? %> +
+
+
+ <%= render partial:"comments/comments", object: comments_block.comments.order(:created_at).roots.reverse_order if params[:show_comments] %><%# can? fehlt!%> +
+
+
+<% end %> diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index d4499d0..d9fa4c9 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,14 +1,14 @@ -
- <%= semantic_form_for form , remote: true, html: {class: "form-inline"} do |f| %> - <%= f.inputs do %> - <%= f.input :commentable_id, as: :hidden %> - <%= f.input :commentable_type, as: :hidden %> - <%= f.input :text, as: :string %> - <%= f.input :anonym %> - <% end %> - + +<%= semantic_form_for form , remote: true, html: {class: "form-inline"} do |f| %> + <%= f.inputs do %> + <%= f.input :commentable_id, as: :hidden %> + <%= f.input :commentable_type, as: :hidden %> + <%= f.input :text, as: :string %> + <%= f.input :anonym %> + <% end %> + <%= f.actions do %> <%= f.action :submit, :as => :input %> <% end %> - <% end %> -
+<% end %> + diff --git a/app/views/comments/create.js.erb b/app/views/comments/create.js.erb index 3f6e83f..9dce0fd 100644 --- a/app/views/comments/create.js.erb +++ b/app/views/comments/create.js.erb @@ -1,2 +1,2 @@ -$("#<%= Comment.formid_for(@comment.parent) %>").html("") -$("#<%= Comment.wrapid_for(@comment.commentable) %>").html("<%= escape_javascript render partial:"comments/comments", object: @comments %>") +$("#<%= divid_for(@comment.parent_object,"newform") %>").html("") +$("#<%= Comment.wrapid_for(@comment.parent_object) %>").html("<%= escape_javascript render partial:"comments/comments", object: @comments %>") diff --git a/app/views/comments/destroy.js.erb b/app/views/comments/destroy.js.erb index 389d41d..761bd41 100644 --- a/app/views/comments/destroy.js.erb +++ b/app/views/comments/destroy.js.erb @@ -1 +1,2 @@ -$('#<%= @divid %>').remove() + +$("#<%= Comment.wrapid_for(@commentable) %>").html("<%= escape_javascript render partial:"comments/comments", object: @comments %>") diff --git a/app/views/comments/new.js.erb b/app/views/comments/new.js.erb index 814b5c9..fbd63e3 100644 --- a/app/views/comments/new.js.erb +++ b/app/views/comments/new.js.erb @@ -1 +1,2 @@ -$("#<%= @comment.formid %>").replaceWith("<%= escape_javascript render partial: "form", object: @comment %>") \ No newline at end of file +$("#<%= divid_for(@comment.commentable,"newform") %>").html("<%= escape_javascript render partial: "form", object: @comment %>") + diff --git a/app/views/survey/questions/show.html.erb b/app/views/survey/questions/show.html.erb index 4c05a31..edf9f8b 100644 --- a/app/views/survey/questions/show.html.erb +++ b/app/views/survey/questions/show.html.erb @@ -6,20 +6,7 @@ <%= render @survey_question %> -<%= link_to "comment" , new_comment_path( commentable_type: "Survey::Question", commentable_id: @survey_question.id), remote:true if can? :comment, @survey_question %> -<%= link_to "comments:.."+ @survey_question.comments.count().to_s, comments_path(commentable_type: "Survey_Question", commentable_id: @survey_question.id), remote:true, id: Comment.switchshowid_for( @survey_question) %> -
-
-<% unless @survey_question.comments.roots.empty? %> -
-
-
- <%= render partial:"comments/comments", object: @survey_question.comments.order(:created_at).roots.reverse_order if params[:show_comments] %> -
-
-
-<% end %> - +<%= render_comments_for(@survey_question) %> <%= link_to 'Edit', edit_survey_question_path(@survey_question) %> | <%= link_to 'Back', survey_questions_path %> diff --git a/config/routes.rb b/config/routes.rb index ace8cc6..241a301 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -250,3 +250,4 @@ root :to => 'home#index' end +