forked from bofh/fetsite
AutoCommit Fre Jul 31 21:03:04 CEST 2015
This commit is contained in:
@@ -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
|
||||
|
||||
94
app/controllers/comments_controller.rb
Normal file
94
app/controllers/comments_controller.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
51
app/models/comment.rb
Normal file
51
app/models/comment.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -38,4 +38,20 @@
|
||||
</div>
|
||||
</div>
|
||||
<% 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) %>
|
||||
<div id="<%= Comment.formid_for(beispiel) %>">
|
||||
</div>
|
||||
<% unless beispiel.comments.roots.empty? %>
|
||||
<div class="row-fluid">
|
||||
<div class="span12">
|
||||
<div id="<%= Comment.wrapid_for(beispiel)%>">
|
||||
<%= render partial:"comments/comments", object: beispiel.comments.order(:created_at).roots.reverse_order if params[:show_comments] %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
19
app/views/comments/_comment.html.erb
Normal file
19
app/views/comments/_comment.html.erb
Normal file
@@ -0,0 +1,19 @@
|
||||
<div id="<%= comment.divid %>">
|
||||
<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) %>) <%= link_to ffi1_icon("remove9"), comment, method: :delete, data: { confirm: 'Are you sure?' } %>:
|
||||
<p><%= comment.text %>
|
||||
<% if can?(:comment, comment.commentable) %>
|
||||
<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>
|
||||
|
||||
<%= render partial:"comments/comments", object: comment.children.order(:created_at).reverse_order if comment.children.size >0 %>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
12
app/views/comments/_comments.html.erb
Normal file
12
app/views/comments/_comments.html.erb
Normal file
@@ -0,0 +1,12 @@
|
||||
<% unless comments.empty? %>
|
||||
<div class="contentbox" style="border-right:none;border-bottom:none; border-top:none">
|
||||
<ul class="unstyled media-list">
|
||||
<% comments.each do |c| %>
|
||||
<li class="media"><%= render c %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% if comments.first.root? %>
|
||||
<%= paginate comments, :remote=>true , :theme=>'twitter-bootstrap'%>
|
||||
<% end %>
|
||||
<% end %>
|
||||
14
app/views/comments/_form.html.erb
Normal file
14
app/views/comments/_form.html.erb
Normal file
@@ -0,0 +1,14 @@
|
||||
<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 %>
|
||||
<%= 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 %>
|
||||
</div>
|
||||
6
app/views/comments/edit.html.erb
Normal file
6
app/views/comments/edit.html.erb
Normal file
@@ -0,0 +1,6 @@
|
||||
<h1>Editing comment</h1>
|
||||
|
||||
<%= render 'form', object: @comment %>
|
||||
|
||||
<%= link_to 'Show', @comment %> |
|
||||
<%= link_to 'Back', comments_path %>
|
||||
3
app/views/comments/hide.js.erb
Normal file
3
app/views/comments/hide.js.erb
Normal file
@@ -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) %>")
|
||||
22
app/views/comments/index.html.erb
Normal file
22
app/views/comments/index.html.erb
Normal file
@@ -0,0 +1,22 @@
|
||||
<h1>Listing comments</h1>
|
||||
<%= render partial:"comments/comments", object: @comments %>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
|
||||
<% @comments.each do |comment| %>
|
||||
<tr>
|
||||
<td><%= link_to 'Show', comment %></td>
|
||||
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
|
||||
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<br />
|
||||
|
||||
<%= link_to 'New Comment', new_comment_path %>
|
||||
2
app/views/comments/index.js.erb
Normal file
2
app/views/comments/index.js.erb
Normal file
@@ -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) %>")
|
||||
5
app/views/comments/new.html.erb
Normal file
5
app/views/comments/new.html.erb
Normal file
@@ -0,0 +1,5 @@
|
||||
<h1>New comment</h1>
|
||||
|
||||
<%= render partial: 'form', object: @comment %>
|
||||
|
||||
<%= link_to 'Back', comments_path %>
|
||||
1
app/views/comments/new.js.erb
Normal file
1
app/views/comments/new.js.erb
Normal file
@@ -0,0 +1 @@
|
||||
$("#<%= @comment.formid %>").replaceWith("<%= escape_javascript render partial: "form", object: @comment %>")
|
||||
6
app/views/comments/show.html.erb
Normal file
6
app/views/comments/show.html.erb
Normal file
@@ -0,0 +1,6 @@
|
||||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<%= render @comment %>
|
||||
|
||||
<%= link_to 'Edit', edit_comment_path(@comment) %> |
|
||||
<%= link_to 'Back', comments_path %>
|
||||
@@ -1,3 +1,4 @@
|
||||
<div id="choice_<%= choice.id %>"
|
||||
<%
|
||||
if current_user.nil?
|
||||
t=choice.text
|
||||
@@ -12,3 +13,4 @@ end
|
||||
|
||||
<%= t %> <%= link_to fa_icon("pencil"), edit_survey_choice_path(choice) , class: "btn btn-link navbar-btn"
|
||||
%>
|
||||
</div>
|
||||
|
||||
1
app/views/survey/choices/_choice_form.html.erb
Normal file
1
app/views/survey/choices/_choice_form.html.erb
Normal file
@@ -0,0 +1 @@
|
||||
<div id="choice_<%= choice_form.id %>"
|
||||
@@ -15,3 +15,5 @@
|
||||
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
||||
|
||||
@@ -6,6 +6,20 @@
|
||||
|
||||
<%= 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 %>
|
||||
|
||||
|
||||
<%= link_to 'Edit', edit_survey_question_path(@survey_question) %> |
|
||||
<%= link_to 'Back', survey_questions_path %>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,23 +1,16 @@
|
||||
|
||||
Fetsite::Application.routes.draw do
|
||||
namespace :survey do
|
||||
resources :answers
|
||||
end
|
||||
resources :comments
|
||||
|
||||
|
||||
namespace :survey do
|
||||
resources :choices
|
||||
end
|
||||
|
||||
|
||||
namespace :survey do
|
||||
resources :questions do
|
||||
member do
|
||||
get :answer
|
||||
namespace :survey do
|
||||
resources :questions do
|
||||
member do
|
||||
get :answer
|
||||
put :answer
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
resources :choices
|
||||
resources :answers
|
||||
end
|
||||
|
||||
themes_for_rails
|
||||
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
|
||||
@@ -168,7 +161,11 @@
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
resources :comments do
|
||||
collection do
|
||||
get 'hide'
|
||||
end
|
||||
end
|
||||
resources :home, :only=>[:index] do
|
||||
get :search, :on => :collection
|
||||
collection do
|
||||
|
||||
19
db/migrate/20140723172934_create_comments.rb
Normal file
19
db/migrate/20140723172934_create_comments.rb
Normal file
@@ -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
|
||||
5
db/migrate/20150731183356_add_icon_to_choice.rb
Normal file
5
db/migrate/20150731183356_add_icon_to_choice.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class AddIconToChoice < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :survey_choices, :icon, :string
|
||||
end
|
||||
end
|
||||
20
lib/is_commentable.rb
Normal file
20
lib/is_commentable.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user