Module: Commontator::ActsAsCommontable::ClassMethods

Defined in:
lib/commontator/acts_as_commontable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_commontable(options = {}) ⇒ Object Also known as: acts_as_commentable



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/commontator/acts_as_commontable.rb', line 12

def acts_as_commontable(options = {})
  class_eval do
    cattr_accessor :commontable_config
    self.commontable_config = Commontator::CommontableConfig.new(options)
    self.is_commontable = true

    has_one :thread, :as => :commontable, :class_name => 'Commontator::Thread'
    has_many :comments, :class_name => 'Commontator::Comment', :through => :thread

    has_many :subscriptions, :class_name => 'Commontator::Subscription', :through => :thread
    
    validates_presence_of :thread
    
    alias_method :thread_raw, :thread
    
    def thread
      raw = thread_raw
      return raw unless raw.nil?
      return Commontator::Thread.find_or_create_by_commontable_type_and_commontable_id(self.class.name, id) \
        unless id.nil?
      self.thread = Commontator::Thread.new
      self.thread.commontable = self
      self.thread
    end
  end
end