This commit is contained in:
2014-07-30 10:15:40 +05:30
parent f5beed1264
commit 1871c64e6c
35 changed files with 429 additions and 12 deletions

View File

@@ -43,7 +43,9 @@ gem 'jquery-rails'
# Formbuilder for easier form generation
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
gem "tinymce-rails" , '~>4.1.0'

View 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/

View File

@@ -115,3 +115,4 @@ $box-background: white;
@import 'layout';
@import 'calendars';
@import 'formtastic-bootstrap'

View File

@@ -17,7 +17,10 @@ class BeispieleController < ApplicationController
def show
# @lva = params([:lva]) unless params([:lva]).nil?
@beispiel = Beispiel.find(params[:id])
redirect_to @beispiel.lva
respond_to do |format|
format.html { redirect_to @beispiel.lva }
format.js
end
end
# GET /beispiele/new

View 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

View File

@@ -0,0 +1,2 @@
module CommentsHelper
end

View File

@@ -4,6 +4,8 @@ class Ability
def initialize(user)
loggedin=!(user.nil?)
user ||= User.new # guest user (not logged in)
can :manage, Comment
#-----------------------------------------------------
# Rechteverwaltung fuer Studien Modul
can [:show, :index], Studium
@@ -97,6 +99,8 @@ class Ability
if user.has_role?("fetadmin")
can :addfetuser, User
can :addfetadmin, User
can :edit, User
can :manage, User
end
if user.has_role?("newsadmin") || user.has_role?( "fetadmin") || user.has_role?( "fetuser")

View File

@@ -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
@@ -35,4 +35,7 @@ class Beispiel < ActiveRecord::Base
"delete_type" => "DELETE"
}
end
def divid
"beispiel_"+id.to_s
end
end

44
app/models/comment.rb Normal file
View 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

View File

@@ -16,7 +16,7 @@
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
has_many :memberships, dependent: :delete_all
has_many :memberships, dependent: :destroy
has_many :gremien, :through=> :membership
mount_uploader :picture, PictureUploader
has_paper_trail

View File

@@ -1,5 +1,5 @@
<div class="row-fluid contentbox">
<div id="<%= beispiel.divid %>" class="contentbox">
<div class="row-fluid">
<div class="span8">
<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>
<span class="linklist"><%=
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
"liked by " + beispiel.get_likes.size.to_s
end
@@ -15,7 +15,7 @@ end
<%=
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
"disliked by " + beispiel.get_dislikes.size.to_s
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-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>
</div>
<div class="span4">
<%= beispiel.desc %>
</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>

View File

@@ -0,0 +1 @@
$("#<%= @beispiel.divid %>").replaceWith("<%= escape_javascript ( render @beispiel ) %>")

View File

@@ -0,0 +1 @@
$("<%= '#' + @beispiel.divid %>").replaceWith("<%= escape_javascript render @beispiel %>")

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

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

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

View File

@@ -0,0 +1,6 @@
<h1>Editing comment</h1>
<%= render 'form', object: @comment %>
<%= link_to 'Show', @comment %> |
<%= link_to 'Back', comments_path %>

View 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 %>

View File

@@ -0,0 +1,5 @@
<h1>New comment</h1>
<%= render partial: 'form', object: @comment %>
<%= link_to 'Back', comments_path %>

View File

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

View File

@@ -0,0 +1,6 @@
<p id="notice"><%= notice %></p>
<%= render @comment %>
<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>

View File

@@ -2,6 +2,8 @@ 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
Bundler.require(*Rails.groups(:assets => %w(development test)))

View File

@@ -1,4 +1,7 @@
Fetsite::Application.routes.draw do
resources :comments
themes_for_rails
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
resources :home, :only=>[:index] do
@@ -127,7 +130,7 @@ Fetsite::Application.routes.draw do
end
end
end
resources :comments
resources :home, :only=>[:index] do
get :search, :on => :collection
collection do

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

View File

@@ -13,7 +13,15 @@ module LikeVoteable
else
@obj.liked_by current_user
end
redirect_to @obj
respond_to do |format|
format.html {
redirect_to @obj
}
format.js {
render :show
}
#
end
end
def dislike
@obj=controller_name.classify.constantize.find(params[:id])
@@ -22,7 +30,17 @@ module LikeVoteable
else
@obj.disliked_by current_user
end
redirect_to @obj
respond_to do |format|
format.html {
redirect_to @obj
}
format.js {
render :show
}
#
end
end
end

View File

@@ -0,0 +1,6 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :comment do
end
end

View 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

View File

@@ -0,0 +1,5 @@
require 'spec_helper'
describe Comment do
pending "add some examples to (or delete) #{__FILE__}"
end

View 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

View 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

View 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

View 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

View 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

View 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