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