AutoCommit Sam Aug 1 12:03:01 CEST 2015

This commit is contained in:
Andreas Stephanides
2015-08-01 12:03:01 +02:00
parent a079a88dbc
commit 234880d90e
13 changed files with 78 additions and 61 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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
end
o

View File

@@ -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

View File

@@ -1,7 +1,9 @@
<div id="<%= comment.divid %>">
<div id="<%= divid_for(comment) %>">
<a class="pull-left media-object" href="#"><%= image_tag comment.thumb_url %></a>
<div class="media-body">
<b><%= (!comment.anonym) ? comment.user.try(:email) : "Anonym" %></b> (<%= I18n.l(comment.created_at) %>)
<b><%= (!comment.anonym) ? comment.user.try(:email) : "Anonym" %>
</b> (<%= I18n.l(comment.created_at) %>)
<%= link_to ffi1_icon("remove9"), comment, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>:
<p>
<%= comment.text %>
@@ -9,9 +11,9 @@
<br><%= link_to "comment" , new_comment_path( commentable_type: "Comment", commentable_id: comment.id), remote:true %>
<% end %>
</p>
<div id="<%= Comment.formid_for(comment) %>">
</div>
<div id="<%= divid_for(comment,"newform") %>">
</div>
<%= render partial:"comments/comments", object: comment.children.order(:created_at).reverse_order if comment.children.size >0 %>
</div>
</div>

View File

@@ -1,4 +1,4 @@
<% unless comments.empty? %>
<% unless comments.nil? || comments.empty? %>
<div class="contentbox" style="border-right:none;border-bottom:none; border-top:none">
<ul class="unstyled media-list">
<% comments.each do |c| %>

View File

@@ -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 %>
<div id="<%= divid_for(comments_block,"newform") %>">
</div>
<% unless comments_block.comments.roots.empty? %>
<div class="row-fluid">
<div class="span12">
<div id="<%= Comment.wrapid_for( comments_block)%>">
<%= render partial:"comments/comments", object: comments_block.comments.order(:created_at).roots.reverse_order if params[:show_comments] %><%# can? fehlt!%>
</div>
</div>
</div>
<% end %>

View File

@@ -1,4 +1,4 @@
<div id="<%= form.formid %>">
<%= semantic_form_for form , remote: true, html: {class: "form-inline"} do |f| %>
<%= f.inputs do %>
<%= f.input :commentable_id, as: :hidden %>
@@ -11,4 +11,4 @@
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
</div>

View File

@@ -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 %>")

View File

@@ -1 +1,2 @@
$('#<%= @divid %>').remove()
$("#<%= Comment.wrapid_for(@commentable) %>").html("<%= escape_javascript render partial:"comments/comments", object: @comments %>")

View File

@@ -1 +1,2 @@
$("#<%= @comment.formid %>").replaceWith("<%= escape_javascript render partial: "form", object: @comment %>")
$("#<%= divid_for(@comment.commentable,"newform") %>").html("<%= escape_javascript render partial: "form", object: @comment %>")

View File

@@ -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) %>
<div id="<%= Comment.formid_for( @survey_question) %>">
</div>
<% unless @survey_question.comments.roots.empty? %>
<div class="row-fluid">
<div class="span12">
<div id="<%= Comment.wrapid_for( @survey_question)%>">
<%= render partial:"comments/comments", object: @survey_question.comments.order(:created_at).roots.reverse_order if params[:show_comments] %>
</div>
</div>
</div>
<% end %>
<%= render_comments_for(@survey_question) %>
<%= link_to 'Edit', edit_survey_question_path(@survey_question) %> |
<%= link_to 'Back', survey_questions_path %>

View File

@@ -250,3 +250,4 @@
root :to => 'home#index'
end