diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 12ad5562a..f74dc5f00 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -62,7 +62,7 @@ class LikesController < ApplicationController current_user.find_visible_post_by_id(params[:post_id]) else 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 end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 77eade82c..71cfae25f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -23,7 +23,8 @@ class Comment < ActiveRecord::Base xml_attr :text xml_attr :diaspora_handle - belongs_to :post + belongs_to :commentable, :touch => true, :polymorphic => true + alias_attribute :post, :commentable belongs_to :author, :class_name => 'Person' validates :text, :presence => true, :length => { :maximum => 2500 } diff --git a/app/models/post.rb b/app/models/post.rb index 048990f7c..39b662504 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -10,14 +10,13 @@ class Post < ActiveRecord::Base include Diaspora::Guid include Diaspora::Likeable + include Diaspora::Commentable xml_attr :diaspora_handle xml_attr :provider_display_name xml_attr :public xml_attr :created_at - has_many :comments, :order => 'created_at', :dependent => :destroy - has_many :aspect_visibilities has_many :aspects, :through => :aspect_visibilities diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index e0955bde3..342a6bce0 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -5,7 +5,7 @@ %li.comment.posted{:id => comment.guid, :class => ("hidden" if(defined? hidden))} - if current_user && (current_user.owns?(comment) || current_user.owns?(post)) .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) .content %span.from diff --git a/db/migrate/20110823212706_comment_anything.rb b/db/migrate/20110823212706_comment_anything.rb new file mode 100644 index 000000000..1a59862b1 --- /dev/null +++ b/db/migrate/20110823212706_comment_anything.rb @@ -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 diff --git a/lib/diaspora/commentable.rb b/lib/diaspora/commentable.rb new file mode 100644 index 000000000..924662392 --- /dev/null +++ b/lib/diaspora/commentable.rb @@ -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] + 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