Introduced Commentable

This commit is contained in:
Manuel Schölling 2011-08-24 20:55:18 +02:00
parent 3179ebabc9
commit 6e325f8fd5
6 changed files with 48 additions and 5 deletions

View file

@ -62,7 +62,7 @@ class LikesController < ApplicationController
current_user.find_visible_post_by_id(params[:post_id]) current_user.find_visible_post_by_id(params[:post_id])
else else
comment = Comment.find(params[:comment_id]) comment = Comment.find(params[:comment_id])
comment = nil unless current_user.find_visible_post_by_id(comment.post_id) comment = nil unless current_user.find_visible_post_by_id(comment.commentable_id)
comment comment
end end
end end

View file

@ -23,7 +23,8 @@ class Comment < ActiveRecord::Base
xml_attr :text xml_attr :text
xml_attr :diaspora_handle xml_attr :diaspora_handle
belongs_to :post belongs_to :commentable, :touch => true, :polymorphic => true
alias_attribute :post, :commentable
belongs_to :author, :class_name => 'Person' belongs_to :author, :class_name => 'Person'
validates :text, :presence => true, :length => { :maximum => 2500 } validates :text, :presence => true, :length => { :maximum => 2500 }

View file

@ -10,14 +10,13 @@ class Post < ActiveRecord::Base
include Diaspora::Guid include Diaspora::Guid
include Diaspora::Likeable include Diaspora::Likeable
include Diaspora::Commentable
xml_attr :diaspora_handle xml_attr :diaspora_handle
xml_attr :provider_display_name xml_attr :provider_display_name
xml_attr :public xml_attr :public
xml_attr :created_at xml_attr :created_at
has_many :comments, :order => 'created_at', :dependent => :destroy
has_many :aspect_visibilities has_many :aspect_visibilities
has_many :aspects, :through => :aspect_visibilities has_many :aspects, :through => :aspect_visibilities

View file

@ -5,7 +5,7 @@
%li.comment.posted{:id => comment.guid, :class => ("hidden" if(defined? hidden))} %li.comment.posted{:id => comment.guid, :class => ("hidden" if(defined? hidden))}
- if current_user && (current_user.owns?(comment) || current_user.owns?(post)) - if current_user && (current_user.owns?(comment) || current_user.owns?(post))
.right.controls .right.controls
= link_to image_tag('deletelabel.png'), post_comment_path(comment.post_id, comment), :class => "delete comment_delete", :title => t('delete') = link_to image_tag('deletelabel.png'), post_comment_path(comment.commentable_id, comment), :class => "delete comment_delete", :title => t('delete')
= person_image_link(comment.author, :size => :thumb_small) = person_image_link(comment.author, :size => :thumb_small)
.content .content
%span.from %span.from

View file

@ -0,0 +1,18 @@
class CommentAnything < ActiveRecord::Migration
def self.up
remove_foreign_key :comments, :posts
remove_index :comments, :post_id
change_table :comments do |t|
t.rename :post_id, :commentable_id
t.string :commentable_type, :default => 'Post', :null => false
end
end
def self.down
rename_column :comments, :commentable_id, :post_id
add_foreign_key :comments, :posts
add_index :comments, :post_id
remove_column :comments, :commentable_type
end
end

View file

@ -0,0 +1,25 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module Diaspora
module Commentable
def self.included(model)
model.instance_eval do
has_many :comments, :as => :commentable, :order => 'created_at', :dependent => :destroy
end
end
# @return [Array<Comment>]
def last_three_comments
self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse
end
end
# @return [Integer]
def update_comments_counter
self.class.where(:id => self.id).
update_all(:comments_count => self.comments.count)
end
end