comments
This commit is contained in:
4
Gemfile
4
Gemfile
@@ -43,7 +43,9 @@ gem 'jquery-rails'
|
|||||||
|
|
||||||
# Formbuilder for easier form generation
|
# Formbuilder for easier form generation
|
||||||
gem 'formtastic', '~>2.2.1'
|
gem 'formtastic', '~>2.2.1'
|
||||||
gem 'formtastic-bootstrap', '~>2.1.3' #, :git => "git://github.com/mjbellantoni/formtastic-bootstrap.git"
|
|
||||||
|
# gem 'formtastic-bootstrap', '~>2.1.3' #, :git => "git://github.com/mjbellantoni/formtastic-bootstrap.git"
|
||||||
|
gem 'formtastic-bootstrap', '~>3.0.0' #, :git => "git://github.com/mjbellantoni/formtastic-bootstrap.git"
|
||||||
# TinyMCE
|
# TinyMCE
|
||||||
gem "tinymce-rails" , '~>4.1.0'
|
gem "tinymce-rails" , '~>4.1.0'
|
||||||
|
|
||||||
|
|||||||
3
app/assets/javascripts/comments.js.coffee
Normal file
3
app/assets/javascripts/comments.js.coffee
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Place all the behaviors and hooks related to the matching controller here.
|
||||||
|
# All this logic will automatically be available in application.js.
|
||||||
|
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
|
||||||
@@ -115,3 +115,4 @@ $box-background: white;
|
|||||||
|
|
||||||
@import 'layout';
|
@import 'layout';
|
||||||
@import 'calendars';
|
@import 'calendars';
|
||||||
|
@import 'formtastic-bootstrap'
|
||||||
@@ -17,7 +17,10 @@ class BeispieleController < ApplicationController
|
|||||||
def show
|
def show
|
||||||
# @lva = params([:lva]) unless params([:lva]).nil?
|
# @lva = params([:lva]) unless params([:lva]).nil?
|
||||||
@beispiel = Beispiel.find(params[:id])
|
@beispiel = Beispiel.find(params[:id])
|
||||||
redirect_to @beispiel.lva
|
respond_to do |format|
|
||||||
|
format.html { redirect_to @beispiel.lva }
|
||||||
|
format.js
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# GET /beispiele/new
|
# GET /beispiele/new
|
||||||
|
|||||||
83
app/controllers/comments_controller.rb
Normal file
83
app/controllers/comments_controller.rb
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
class CommentsController < ApplicationController
|
||||||
|
def index
|
||||||
|
@comments=Comment.all
|
||||||
|
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)
|
||||||
|
#raise @comment.to_yaml.to_s
|
||||||
|
# @comment.commentable= c
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
if @comment
|
||||||
|
format.html { redirect_to @comment.commentable, notice: 'Comment was successfully created.' }
|
||||||
|
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])
|
||||||
|
@comment.destroy
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.html { redirect_to comments_url }
|
||||||
|
format.json { head :no_content }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
2
app/helpers/comments_helper.rb
Normal file
2
app/helpers/comments_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
module CommentsHelper
|
||||||
|
end
|
||||||
@@ -4,6 +4,8 @@ class Ability
|
|||||||
def initialize(user)
|
def initialize(user)
|
||||||
loggedin=!(user.nil?)
|
loggedin=!(user.nil?)
|
||||||
user ||= User.new # guest user (not logged in)
|
user ||= User.new # guest user (not logged in)
|
||||||
|
|
||||||
|
can :manage, Comment
|
||||||
#-----------------------------------------------------
|
#-----------------------------------------------------
|
||||||
# Rechteverwaltung fuer Studien Modul
|
# Rechteverwaltung fuer Studien Modul
|
||||||
can [:show, :index], Studium
|
can [:show, :index], Studium
|
||||||
@@ -97,6 +99,8 @@ class Ability
|
|||||||
if user.has_role?("fetadmin")
|
if user.has_role?("fetadmin")
|
||||||
can :addfetuser, User
|
can :addfetuser, User
|
||||||
can :addfetadmin, User
|
can :addfetadmin, User
|
||||||
|
can :edit, User
|
||||||
|
can :manage, User
|
||||||
end
|
end
|
||||||
|
|
||||||
if user.has_role?("newsadmin") || user.has_role?( "fetadmin") || user.has_role?( "fetuser")
|
if user.has_role?("newsadmin") || user.has_role?( "fetadmin") || user.has_role?( "fetuser")
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class Beispiel < ActiveRecord::Base
|
|||||||
attr_accessible :desc, :name, :lva_id, :beispieldatei, :beispieldatei_cache, :datum
|
attr_accessible :desc, :name, :lva_id, :beispieldatei, :beispieldatei_cache, :datum
|
||||||
acts_as_votable
|
acts_as_votable
|
||||||
belongs_to :lva
|
belongs_to :lva
|
||||||
|
include IsCommentable
|
||||||
mount_uploader :beispieldatei, AttachmentUploader
|
mount_uploader :beispieldatei, AttachmentUploader
|
||||||
validates :beispieldatei, :presence => true
|
validates :beispieldatei, :presence => true
|
||||||
validates :name, :presence => true
|
validates :name, :presence => true
|
||||||
@@ -35,4 +35,7 @@ class Beispiel < ActiveRecord::Base
|
|||||||
"delete_type" => "DELETE"
|
"delete_type" => "DELETE"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
def divid
|
||||||
|
"beispiel_"+id.to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
44
app/models/comment.rb
Normal file
44
app/models/comment.rb
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
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 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
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
class Fetprofile < ActiveRecord::Base
|
class Fetprofile < ActiveRecord::Base
|
||||||
attr_accessible :active, :desc, :fetmailalias, :nachname, :picture, :short, :vorname, :memberships_attributes, :remove_picture, :picture_cache, :plz, :street, :city, :instant,:skype, :telnr, :hdynr, :birth_day, :birth_month, :birth_year,:geschlecht
|
attr_accessible :active, :desc, :fetmailalias, :nachname, :picture, :short, :vorname, :memberships_attributes, :remove_picture, :picture_cache, :plz, :street, :city, :instant,:skype, :telnr, :hdynr, :birth_day, :birth_month, :birth_year,:geschlecht
|
||||||
has_many :memberships, dependent: :delete_all
|
has_many :memberships, dependent: :destroy
|
||||||
has_many :gremien, :through=> :membership
|
has_many :gremien, :through=> :membership
|
||||||
mount_uploader :picture, PictureUploader
|
mount_uploader :picture, PictureUploader
|
||||||
has_paper_trail
|
has_paper_trail
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
<div id="<%= beispiel.divid %>" class="contentbox">
|
||||||
<div class="row-fluid contentbox">
|
<div class="row-fluid">
|
||||||
<div class="span8">
|
<div class="span8">
|
||||||
|
|
||||||
<b><%=link_to ffi1_icon("note20")+" " + beispiel.name, beispiel.beispieldatei.url, title: beispiel.desc %></b>
|
<b><%=link_to ffi1_icon("note20")+" " + beispiel.name, beispiel.beispieldatei.url, title: beispiel.desc %></b>
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
<%= I18n.t("file.size") + ": " + (beispiel.beispieldatei.size/1024.0).round(2).to_s %>KiB <br>
|
<%= I18n.t("file.size") + ": " + (beispiel.beispieldatei.size/1024.0).round(2).to_s %>KiB <br>
|
||||||
<span class="linklist"><%=
|
<span class="linklist"><%=
|
||||||
if can?(:like, beispiel)
|
if can?(:like, beispiel)
|
||||||
link_to ffi1_icon("like3")+" like" + "("+beispiel.get_likes.size.to_s+")", like_beispiel_path(beispiel),title: "liked by " + ((current_user.liked?(beispiel)) ? ("you and " + ((beispiel.get_likes.size - 1).to_s + " others")) : beispiel.get_likes.size.to_s)
|
link_to ffi1_icon("like3")+" like" + "("+beispiel.get_likes.size.to_s+")", like_beispiel_path(beispiel),title: "liked by " + ((current_user.liked?(beispiel)) ? ("you and " + ((beispiel.get_likes.size - 1).to_s + " others")) : beispiel.get_likes.size.to_s), remote: true
|
||||||
else
|
else
|
||||||
"liked by " + beispiel.get_likes.size.to_s
|
"liked by " + beispiel.get_likes.size.to_s
|
||||||
end
|
end
|
||||||
@@ -15,7 +15,7 @@ end
|
|||||||
|
|
||||||
<%=
|
<%=
|
||||||
if can?(:dislike, beispiel)
|
if can?(:dislike, beispiel)
|
||||||
link_to ffi1_icon("dislike")+" dislike" + "("+beispiel.get_dislikes.size.to_s+")", dislike_beispiel_path(beispiel),title:"disliked by " + ((current_user.disliked?(beispiel)) ? ("you and " + ((beispiel.get_dislikes.size - 1).to_s + " others")) : beispiel.get_dislikes.size.to_s)
|
link_to ffi1_icon("dislike")+" dislike" + "("+beispiel.get_dislikes.size.to_s+")", dislike_beispiel_path(beispiel),title:"disliked by " + ((current_user.disliked?(beispiel)) ? ("you and " + ((beispiel.get_dislikes.size - 1).to_s + " others")) : beispiel.get_dislikes.size.to_s) , remote: true
|
||||||
else
|
else
|
||||||
"disliked by " + beispiel.get_dislikes.size.to_s
|
"disliked by " + beispiel.get_dislikes.size.to_s
|
||||||
end
|
end
|
||||||
@@ -23,10 +23,17 @@ end
|
|||||||
%>
|
%>
|
||||||
|
|
||||||
<%= link_to ff_icon("icon-pencil")+" edit", edit_beispiel_path(beispiel) if can? :edit, beispiel%>
|
<%= link_to ff_icon("icon-pencil")+" edit", edit_beispiel_path(beispiel) if can? :edit, beispiel%>
|
||||||
<%= link_to ff_icon("icon-remove")+" delete", beispiel_path(beispiel), :method=>:delete, :data=>{:confirm=>I18n.t('beispiel.sure')} if can? :delete, beispiel %></br>
|
<%= link_to ff_icon("icon-remove")+" delete", beispiel_path(beispiel), :method=>:delete, :data=>{:confirm=>I18n.t('beispiel.sure')} if can? :delete, beispiel %>
|
||||||
|
<%= link_to "Refresh", beispiel, remote: true %></br>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="span4">
|
<div class="span4">
|
||||||
<%= beispiel.desc %>
|
<%= beispiel.desc %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="row-fluid"><div class="span12"><%= render partial:"comments/comments", object: beispiel.comments.roots %></div></div>
|
||||||
|
<%= link_to "comment" , new_comment_path( commentable_type: "Beispiel", commentable_id: beispiel.id), remote:true %>
|
||||||
|
<div id="<%= Comment.formid_for(beispiel) %>">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|||||||
1
app/views/beispiele/refresh.js.erb
Normal file
1
app/views/beispiele/refresh.js.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
$("#<%= @beispiel.divid %>").replaceWith("<%= escape_javascript ( render @beispiel ) %>")
|
||||||
1
app/views/beispiele/show.js.erb
Normal file
1
app/views/beispiele/show.js.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
$("<%= '#' + @beispiel.divid %>").replaceWith("<%= escape_javascript render @beispiel %>")
|
||||||
12
app/views/comments/_comment.html.erb
Normal file
12
app/views/comments/_comment.html.erb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<div id="<%= comment.divid %>">
|
||||||
|
<a class="pull-left media-object" href="#"><%= image_tag comment.thumb_url %></a>
|
||||||
|
<div class="media-body">
|
||||||
|
<b><%= comment.user.try(:email) %></b> (<%= I18n.l(comment.created_at) %>): <%= comment.text %>
|
||||||
|
<%= link_to "comment" , new_comment_path( commentable_type: "Comment", commentable_id: comment.id), remote:true %>
|
||||||
|
<%= render partial:"comments/comments", object: comment.children if comment.children.size >0 %>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="<%= Comment.formid_for(comment) %>">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
7
app/views/comments/_comments.html.erb
Normal file
7
app/views/comments/_comments.html.erb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<div class="contentbox">
|
||||||
|
<ul class="unstyled media-list">
|
||||||
|
<% comments.each do |c| %>
|
||||||
|
<li class="media"><%= render c %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
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 %>
|
||||||
21
app/views/comments/index.html.erb
Normal file
21
app/views/comments/index.html.erb
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<h1>Listing comments</h1>
|
||||||
|
|
||||||
|
<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 %>
|
||||||
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 %>
|
||||||
@@ -2,6 +2,8 @@ require File.expand_path('../boot', __FILE__)
|
|||||||
|
|
||||||
require 'rails/all'
|
require 'rails/all'
|
||||||
require File.expand_path('lib/like_voteable.rb')
|
require File.expand_path('lib/like_voteable.rb')
|
||||||
|
require File.expand_path('lib/is_commentable.rb')
|
||||||
|
|
||||||
if defined?(Bundler)
|
if defined?(Bundler)
|
||||||
# If you precompile assets before deploying to production, use this line
|
# If you precompile assets before deploying to production, use this line
|
||||||
Bundler.require(*Rails.groups(:assets => %w(development test)))
|
Bundler.require(*Rails.groups(:assets => %w(development test)))
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
Fetsite::Application.routes.draw do
|
Fetsite::Application.routes.draw do
|
||||||
|
resources :comments
|
||||||
|
|
||||||
|
|
||||||
themes_for_rails
|
themes_for_rails
|
||||||
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
|
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
|
||||||
resources :home, :only=>[:index] do
|
resources :home, :only=>[:index] do
|
||||||
@@ -127,7 +130,7 @@ Fetsite::Application.routes.draw do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
resources :comments
|
||||||
resources :home, :only=>[:index] do
|
resources :home, :only=>[:index] do
|
||||||
get :search, :on => :collection
|
get :search, :on => :collection
|
||||||
collection do
|
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
|
||||||
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
|
||||||
@@ -13,7 +13,15 @@ module LikeVoteable
|
|||||||
else
|
else
|
||||||
@obj.liked_by current_user
|
@obj.liked_by current_user
|
||||||
end
|
end
|
||||||
redirect_to @obj
|
respond_to do |format|
|
||||||
|
format.html {
|
||||||
|
redirect_to @obj
|
||||||
|
}
|
||||||
|
format.js {
|
||||||
|
render :show
|
||||||
|
}
|
||||||
|
#
|
||||||
|
end
|
||||||
end
|
end
|
||||||
def dislike
|
def dislike
|
||||||
@obj=controller_name.classify.constantize.find(params[:id])
|
@obj=controller_name.classify.constantize.find(params[:id])
|
||||||
@@ -22,7 +30,17 @@ module LikeVoteable
|
|||||||
else
|
else
|
||||||
@obj.disliked_by current_user
|
@obj.disliked_by current_user
|
||||||
end
|
end
|
||||||
redirect_to @obj
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.html {
|
||||||
|
redirect_to @obj
|
||||||
|
}
|
||||||
|
format.js {
|
||||||
|
render :show
|
||||||
|
}
|
||||||
|
#
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
6
spec/factories/comments.rb
Normal file
6
spec/factories/comments.rb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# Read about factories at https://github.com/thoughtbot/factory_girl
|
||||||
|
|
||||||
|
FactoryGirl.define do
|
||||||
|
factory :comment do
|
||||||
|
end
|
||||||
|
end
|
||||||
15
spec/helpers/comments_helper_spec.rb
Normal file
15
spec/helpers/comments_helper_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
# Specs in this file have access to a helper object that includes
|
||||||
|
# the CommentsHelper. For example:
|
||||||
|
#
|
||||||
|
# describe CommentsHelper do
|
||||||
|
# describe "string concat" do
|
||||||
|
# it "concats two strings with spaces" do
|
||||||
|
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
describe CommentsHelper do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
5
spec/models/comment_spec.rb
Normal file
5
spec/models/comment_spec.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Comment do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
11
spec/requests/comments_spec.rb
Normal file
11
spec/requests/comments_spec.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe "Comments" do
|
||||||
|
describe "GET /comments" do
|
||||||
|
it "works! (now write some real specs)" do
|
||||||
|
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
|
||||||
|
get comments_path
|
||||||
|
response.status.should be(200)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
35
spec/routing/comments_routing_spec.rb
Normal file
35
spec/routing/comments_routing_spec.rb
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe CommentsController do
|
||||||
|
describe "routing" do
|
||||||
|
|
||||||
|
it "routes to #index" do
|
||||||
|
get("/comments").should route_to("comments#index")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes to #new" do
|
||||||
|
get("/comments/new").should route_to("comments#new")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes to #show" do
|
||||||
|
get("/comments/1").should route_to("comments#show", :id => "1")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes to #edit" do
|
||||||
|
get("/comments/1/edit").should route_to("comments#edit", :id => "1")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes to #create" do
|
||||||
|
post("/comments").should route_to("comments#create")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes to #update" do
|
||||||
|
put("/comments/1").should route_to("comments#update", :id => "1")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes to #destroy" do
|
||||||
|
delete("/comments/1").should route_to("comments#destroy", :id => "1")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
15
spec/views/comments/edit.html.erb_spec.rb
Normal file
15
spec/views/comments/edit.html.erb_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe "comments/edit" do
|
||||||
|
before(:each) do
|
||||||
|
@comment = assign(:comment, stub_model(Comment))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "renders the edit comment form" do
|
||||||
|
render
|
||||||
|
|
||||||
|
# Run the generator again with the --webrat flag if you want to use webrat matchers
|
||||||
|
assert_select "form[action=?][method=?]", comment_path(@comment), "post" do
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
15
spec/views/comments/index.html.erb_spec.rb
Normal file
15
spec/views/comments/index.html.erb_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe "comments/index" do
|
||||||
|
before(:each) do
|
||||||
|
assign(:comments, [
|
||||||
|
stub_model(Comment),
|
||||||
|
stub_model(Comment)
|
||||||
|
])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "renders a list of comments" do
|
||||||
|
render
|
||||||
|
# Run the generator again with the --webrat flag if you want to use webrat matchers
|
||||||
|
end
|
||||||
|
end
|
||||||
15
spec/views/comments/new.html.erb_spec.rb
Normal file
15
spec/views/comments/new.html.erb_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe "comments/new" do
|
||||||
|
before(:each) do
|
||||||
|
assign(:comment, stub_model(Comment).as_new_record)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "renders new comment form" do
|
||||||
|
render
|
||||||
|
|
||||||
|
# Run the generator again with the --webrat flag if you want to use webrat matchers
|
||||||
|
assert_select "form[action=?][method=?]", comments_path, "post" do
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
12
spec/views/comments/show.html.erb_spec.rb
Normal file
12
spec/views/comments/show.html.erb_spec.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe "comments/show" do
|
||||||
|
before(:each) do
|
||||||
|
@comment = assign(:comment, stub_model(Comment))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "renders attributes in <p>" do
|
||||||
|
render
|
||||||
|
# Run the generator again with the --webrat flag if you want to use webrat matchers
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user