diff --git a/app/controllers/beispiele_controller.rb b/app/controllers/beispiele_controller.rb index 317885a..ce72054 100755 --- a/app/controllers/beispiele_controller.rb +++ b/app/controllers/beispiele_controller.rb @@ -22,7 +22,7 @@ class BeispieleController < ApplicationController # @lva = params([:lva]) unless params([:lva]).nil? @beispiel = Beispiel.find(params[:id]) respond_to do |format| - format.html { redirect_to lva_path(@beispiel.lva )} + format.html { redirect_to lva_path(@beispiel.lva , show_comments: params[:show_comments])} format.js end end 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 82c48f8..f0f992a 100755 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -10,8 +10,8 @@ class Ability end end user ||= User.new # guest user (not logged in) - - + can :manage, Survey::Question + can :manage, Comment #----------------------------------------------------- # Rechteverwaltung fuer Studien Modul can [:show, :index], Studium, :visible=>true diff --git a/app/models/beispiel.rb b/app/models/beispiel.rb index 4725133..69f6660 100755 --- a/app/models/beispiel.rb +++ b/app/models/beispiel.rb @@ -17,6 +17,7 @@ class Beispiel < ActiveRecord::Base acts_as_votable acts_as_flagable belongs_to :lva + belongs_to :lecturer FLAG_ICONS = {"badquality"=>"fa fa-flag","goodquality"=>"fa fa-flag", "delete"=>"fa fa-trash"} scope :not_flag_badquality, ->{where("flag_badquality IS NULL OR flag_badquality=?",false)} @@ -25,6 +26,9 @@ class Beispiel < ActiveRecord::Base scope :flag_delete, ->{where("flag_delete=?",true)} mount_uploader :beispieldatei, AttachmentUploader + + include IsCommentable + validates :beispieldatei, :presence => true validates :name, :presence => true validates :lva_id, :presence => true diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..95c1630 --- /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.gsub(":","_") + "_" + c.id.to_s + end + + 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 + def self.formid_for(c) + "comment_form_" + c.class.to_s.gsub(":","_") + "_" + c.id.to_s + end +end diff --git a/app/models/survey/question.rb b/app/models/survey/question.rb index 6bba056..a6df7df 100644 --- a/app/models/survey/question.rb +++ b/app/models/survey/question.rb @@ -3,6 +3,8 @@ class Survey::Question < ActiveRecord::Base belongs_to :parent, polymorphic: true has_many :choices has_many :answers, through: :choices + include IsCommentable + def add_yesno_choices c=Survey::Choice.new(title: "Ja") c.save diff --git a/app/views/beispiele/_beispiel.html.erb b/app/views/beispiele/_beispiel.html.erb index 9e1aa4e..d5f0356 100644 --- a/app/views/beispiele/_beispiel.html.erb +++ b/app/views/beispiele/_beispiel.html.erb @@ -38,4 +38,20 @@ <% end %> + + <%= 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) %> +
<%= comment.text %>
+<% if can?(:comment, comment.commentable) %>
+
<%= link_to "comment" , new_comment_path( commentable_type: "Comment", commentable_id: comment.id), remote:true %>
+<% end %>
+
| + | + | + |
|---|---|---|
| <%= link_to 'Show', comment %> | +<%= link_to 'Edit', edit_comment_path(comment) %> | +<%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %> | +
<%= notice %>
+ +<%= render @comment %> + +<%= link_to 'Edit', edit_comment_path(@comment) %> | +<%= link_to 'Back', comments_path %> diff --git a/app/views/survey/choices/_choice.html.erb b/app/views/survey/choices/_choice.html.erb index 0954001..4b8ce9b 100644 --- a/app/views/survey/choices/_choice.html.erb +++ b/app/views/survey/choices/_choice.html.erb @@ -1,3 +1,4 @@ +