From 699517fb565a57e27da9da52495e1e1ddd612d5c Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Fri, 17 Oct 2014 09:54:39 +0200 Subject: [PATCH] Revert "removecomments" This reverts commit 6ee1468ce3f54c32f01fb21653e3350e0ade83e5. --- app/controllers/comments_controller.rb | 94 ++++++++++++++++++++ app/models/ability.rb | 2 +- app/models/beispiel.rb | 2 +- app/models/comment.rb | 51 +++++++++++ app/views/beispiele/_beispiel.html.erb | 13 +++ app/views/comments/_comment.html.erb | 19 ++++ app/views/comments/_comments.html.erb | 12 +++ app/views/comments/_form.html.erb | 14 +++ app/views/comments/edit.html.erb | 6 ++ app/views/comments/hide.js.erb | 3 + app/views/comments/index.html.erb | 22 +++++ app/views/comments/index.js.erb | 2 + app/views/comments/new.html.erb | 5 ++ app/views/comments/new.js.erb | 1 + app/views/comments/show.html.erb | 6 ++ config/application.rb | 2 +- config/routes.rb | 8 +- db/migrate/20140723172934_create_comments.rb | 19 ++++ lib/is_commentable.rb | 20 +++++ 19 files changed, 296 insertions(+), 5 deletions(-) create mode 100644 app/controllers/comments_controller.rb create mode 100644 app/models/comment.rb create mode 100644 app/views/comments/_comment.html.erb create mode 100644 app/views/comments/_comments.html.erb create mode 100644 app/views/comments/_form.html.erb create mode 100644 app/views/comments/edit.html.erb create mode 100644 app/views/comments/hide.js.erb create mode 100644 app/views/comments/index.html.erb create mode 100644 app/views/comments/index.js.erb create mode 100644 app/views/comments/new.html.erb create mode 100644 app/views/comments/new.js.erb create mode 100644 app/views/comments/show.html.erb create mode 100644 db/migrate/20140723172934_create_comments.rb create mode 100644 lib/is_commentable.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..39e0490 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,94 @@ +class CommentsController < ApplicationController + def index + @commentable=params[:commentable_type].constantize.find(params[:commentable_id]) unless params[:commentable_type].nil? or params[:commentable_id].nil? + @comments=@commentable.comments.order(:created_at).roots.page(params[:page]).per(2).reverse_order + respond_to do |format| + format.html # new.html.erb + format.json { render json: @comment } + format.js + end + + end + def hide + @commentable=params[:commentable_type].constantize.find(params[:commentable_id]) unless params[:commentable_type].nil? or params[:commentable_id].nil? + respond_to do |format| + format.js + end + + end + def show + @comment = Comment.find(params[:id]) + + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @comment } + end + end + 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 } + format.js + end + end + + # GET /comments/1/edit + def edit + @comment = Comment.find(params[:id]) + end + + # POST /comments + # POST /comments.json + def create + params_new= params[:comment].select {|i| !["commentable_id", "commentable_type"].include?(i)} + + 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) + respond_to do |format| + if @comment + format.html { redirect_to @comment.commentable, notice: 'Comment was successfully created.', show_comments: true } + format.json { render json: @comment, status: :created, location: @comment } + else + format.html { render action: "new" } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # PUT /comments/1 + # PUT /comments/1.json + def update + + params[:comment].select! {|i| !["commentable_id", "commentable_type"].include?(i)} + @comment = Comment.find(params[:id]) + @comment.commentable=params[:comment][:commentable_type].constantize.find(params[:comment][:commentable_id]) unless params[:comment][:commentable_type].nil? or params[:comment][:commentable_id].nil? + respond_to do |format| + + if @comment.update_attributes(params[:comment]) + format.html { redirect_to @comment.commentable, notice: 'Comment was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /comments/1 + # DELETE /comments/1.json + def destroy + @comment = Comment.find(params[:id]) + @commentable=@comment.commentable + @comment.destroy + + respond_to do |format| + format.html { redirect_to @commentable, :action=>"show"} + format.json { head :no_content } + + end + end +end diff --git a/app/models/ability.rb b/app/models/ability.rb index 1cc4986..52c3a3f 100755 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -5,7 +5,7 @@ class Ability loggedin=!(user.nil?) user ||= User.new # guest user (not logged in) - + can :manage, Comment #----------------------------------------------------- # Rechteverwaltung fuer Studien Modul can [:show, :index], Studium diff --git a/app/models/beispiel.rb b/app/models/beispiel.rb index 2fbf52f..9d94c06 100755 --- a/app/models/beispiel.rb +++ b/app/models/beispiel.rb @@ -16,7 +16,7 @@ class Beispiel < ActiveRecord::Base attr_accessible :desc, :name, :lva_id, :beispieldatei, :beispieldatei_cache, :datum acts_as_votable belongs_to :lva - + include IsCommentable mount_uploader :beispieldatei, AttachmentUploader validates :beispieldatei, :presence => true validates :name, :presence => true diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..dd238b5 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,51 @@ +class Comment < ActiveRecord::Base + attr_accessible :text,:anonym, :intern, :hidden + # commentable depth, official, intern, anonym + acts_as_votable + acts_as_nested_set :scope => [:commentable_id, :commentable_type] + belongs_to :commentable, :polymorphic=> true + belongs_to :user + validate :text, :presence=>true + validate :user, :presence=>true + validate :commentable, :presence=>true + include IsCommentable + + 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?) + c.user=user + c.text=text + c.assign_attributes(attr) + + unless set_commentable.class.to_s == "Comment" + c.commentable=set_commentable + c.save + else + + c.commentable=set_commentable.commentable + c.save + c.move_to_child_of(set_commentable) + end + c + end + def thumb_url + t_url= user.fetprofile.picture.thumb.url unless user.nil? or user.fetprofile.nil? + t_url + end + def self.wrapid_for(c) + "comments_" + c.class.to_s + "_" + c.id.to_s + end + + def self.switchshowid_for(c) + "show_comments_" + c.class.to_s + "_" + c.id.to_s + end + def divid + "comment_" + id.to_s + end + def formid +"comment_form_" + commentable_type + "_" + commentable_id.to_s + end + def self.formid_for(c) + "comment_form_" + c.class.to_s + "_" + c.id.to_s + end +end diff --git a/app/views/beispiele/_beispiel.html.erb b/app/views/beispiele/_beispiel.html.erb index 88ab024..8353ceb 100644 --- a/app/views/beispiele/_beispiel.html.erb +++ b/app/views/beispiele/_beispiel.html.erb @@ -33,5 +33,18 @@ + <%= link_to "comment" , new_comment_path( commentable_type: "Beispiel", commentable_id: beispiel.id), remote:true if can? :comment, beispiel %> + <%= link_to "comments:.."+beispiel.comments.count().to_s, comments_path(commentable_type: "Beispiel", commentable_id: beispiel.id), remote:true, id: Comment.switchshowid_for(beispiel) %> +
+
+ <% unless beispiel.comments.roots.empty? %> +
+
+
+ <%= render partial:"comments/comments", object: beispiel.comments.order(:created_at).roots.reverse_order if params[:show_comments] %> +
+
+
+ <% end %> diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb new file mode 100644 index 0000000..2740c2f --- /dev/null +++ b/app/views/comments/_comment.html.erb @@ -0,0 +1,19 @@ +
+ <%= image_tag comment.thumb_url %> +
+ + <%= (!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?' } %>: +

<%= comment.text %> +<% if can?(:comment, comment.commentable) %> +
<%= 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 %> + + +
+ +
diff --git a/app/views/comments/_comments.html.erb b/app/views/comments/_comments.html.erb new file mode 100644 index 0000000..4cda60e --- /dev/null +++ b/app/views/comments/_comments.html.erb @@ -0,0 +1,12 @@ +<% unless comments.empty? %> +
+ +
+<% if comments.first.root? %> +<%= paginate comments, :remote=>true , :theme=>'twitter-bootstrap'%> +<% end %> +<% end %> diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb new file mode 100644 index 0000000..0f9d2c8 --- /dev/null +++ b/app/views/comments/_form.html.erb @@ -0,0 +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 %> + + <%= f.actions do %> + <%= f.action :submit, :as => :input %> + <% end %> +<% end %> +
diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb new file mode 100644 index 0000000..ba1980e --- /dev/null +++ b/app/views/comments/edit.html.erb @@ -0,0 +1,6 @@ +

Editing comment

+ +<%= render 'form', object: @comment %> + +<%= link_to 'Show', @comment %> | +<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/hide.js.erb b/app/views/comments/hide.js.erb new file mode 100644 index 0000000..53ca8af --- /dev/null +++ b/app/views/comments/hide.js.erb @@ -0,0 +1,3 @@ +$("#<%= Comment.wrapid_for(@commentable) %>").html("<%= escape_javascript "" %>") + +$("#<%= Comment.switchshowid_for(@commentable) %>").attr("href","<%= escape_javascript comments_path(commentable_type: @commentable.class.to_s, commentable_id: @commentable.id) %>") diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb new file mode 100644 index 0000000..d97f147 --- /dev/null +++ b/app/views/comments/index.html.erb @@ -0,0 +1,22 @@ +

Listing comments

+<%= render partial:"comments/comments", object: @comments %> + + + + + + + + +<% @comments.each do |comment| %> + + + + + +<% end %> +
<%= link_to 'Show', comment %><%= link_to 'Edit', edit_comment_path(comment) %><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Comment', new_comment_path %> diff --git a/app/views/comments/index.js.erb b/app/views/comments/index.js.erb new file mode 100644 index 0000000..13d7356 --- /dev/null +++ b/app/views/comments/index.js.erb @@ -0,0 +1,2 @@ +$("#<%= Comment.wrapid_for(@commentable) %>").html("<%= escape_javascript render partial:"comments/comments", object: @comments %>") +$("#<%= Comment.switchshowid_for(@commentable) %>").attr("href","<%= escape_javascript hide_comments_path(commentable_type: @commentable.class.to_s, commentable_id: @commentable.id) %>") diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 0000000..4d10894 --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,5 @@ +

New comment

+ +<%= render partial: 'form', object: @comment %> + +<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/new.js.erb b/app/views/comments/new.js.erb new file mode 100644 index 0000000..814b5c9 --- /dev/null +++ b/app/views/comments/new.js.erb @@ -0,0 +1 @@ +$("#<%= @comment.formid %>").replaceWith("<%= escape_javascript render partial: "form", object: @comment %>") \ No newline at end of file diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb new file mode 100644 index 0000000..a0e0552 --- /dev/null +++ b/app/views/comments/show.html.erb @@ -0,0 +1,6 @@ +

<%= notice %>

+ +<%= render @comment %> + +<%= link_to 'Edit', edit_comment_path(@comment) %> | +<%= link_to 'Back', comments_path %> diff --git a/config/application.rb b/config/application.rb index eccce65..4c31c97 100755 --- a/config/application.rb +++ b/config/application.rb @@ -2,7 +2,7 @@ require File.expand_path('../boot', __FILE__) require 'rails/all' require File.expand_path('lib/like_voteable.rb') - +require File.expand_path('lib/is_commentable.rb') if defined?(Bundler) # If you precompile assets before deploying to production, use this line diff --git a/config/routes.rb b/config/routes.rb index a7c5835..6d3a5ac 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Fetsite::Application.routes.draw do - + resources :comments themes_for_rails @@ -126,7 +126,11 @@ Fetsite::Application.routes.draw do end end end - + resources :comments do + collection do + get 'hide' + end + end resources :home, :only=>[:index] do get :search, :on => :collection collection do diff --git a/db/migrate/20140723172934_create_comments.rb b/db/migrate/20140723172934_create_comments.rb new file mode 100644 index 0000000..8b0ab02 --- /dev/null +++ b/db/migrate/20140723172934_create_comments.rb @@ -0,0 +1,19 @@ +class CreateComments < ActiveRecord::Migration + def change + create_table :comments do |t| + t.integer :user_id + t.integer :commentable_id + t.string :commentable_type + t.text :text + t.integer :parent_id + t.integer :lft + t.integer :rgt + t.integer :depth + t.boolean :hidden + t.boolean :official + t.boolean :intern + t.boolean :anonym + t.timestamps + end + end +end diff --git a/lib/is_commentable.rb b/lib/is_commentable.rb new file mode 100644 index 0000000..c5c1d56 --- /dev/null +++ b/lib/is_commentable.rb @@ -0,0 +1,20 @@ +module IsCommentable + def self.included(base) + base.class_eval do + include InstanceMethods + has_many :comments, as: :commentable, dependent: :destroy +# extend ClassMethods + + end + + end + module InstanceMethods + def is_commentable? + true + end + def comment(user, text, attr={}) + comments << Comment.build_for(self, user, text, attr) + end + end + +end