From 6d444ac73749bd255892a888f3681c2d91405b53 Mon Sep 17 00:00:00 2001 From: Alexey Poimtsev Date: Tue, 17 Jan 2012 15:42:46 +0400 Subject: Added ability to attach discussion to any other model (discussable) --- app/models/discussion.rb | 2 ++ lib/generators/inboxes/templates/install.rb | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/models/discussion.rb b/app/models/discussion.rb index a5b12ca..675547d 100644 --- a/app/models/discussion.rb +++ b/app/models/discussion.rb @@ -7,6 +7,8 @@ class Discussion < ActiveRecord::Base # creater has_many :messages, :dependent => :destroy + belongs_to :discussable, :polymorphic => true + # participants of discussion (speakers) has_many :speakers, :dependent => :destroy has_many :users, :through => :speakers diff --git a/lib/generators/inboxes/templates/install.rb b/lib/generators/inboxes/templates/install.rb index 436ac98..e89c128 100644 --- a/lib/generators/inboxes/templates/install.rb +++ b/lib/generators/inboxes/templates/install.rb @@ -2,6 +2,7 @@ class InstallInboxes < ActiveRecord::Migration def self.up create_table :discussions do |t| t.integer :messages_count, :default => 0 # counter cache + t.references :discussable t.timestamps end @@ -26,4 +27,4 @@ class InstallInboxes < ActiveRecord::Migration drop_table :discussions drop_table :messages end -end \ No newline at end of file +end -- cgit v1.2.3 From 69127502f3a419beecd35dda687f5536524a4d7a Mon Sep 17 00:00:00 2001 From: Alexey Poimtsev Date: Tue, 17 Jan 2012 15:54:30 +0400 Subject: acts_as_discussable added acts_as_discussable method. this method can be used in any model to add ability on it --- lib/inboxes/active_record_extension.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/inboxes/active_record_extension.rb b/lib/inboxes/active_record_extension.rb index 2a5bac8..d6e7e36 100644 --- a/lib/inboxes/active_record_extension.rb +++ b/lib/inboxes/active_record_extension.rb @@ -7,5 +7,9 @@ module Inboxes has_many :speakers, :dependent => :destroy has_many :discussions, :through => :speakers end + + def acts_as_discussable + has_many :discussions, :as => :discussable + end end -end \ No newline at end of file +end -- cgit v1.2.3