2004-11-24 01:04:44 +00:00
|
|
|
class Topic < ActiveRecord::Base
|
2008-03-24 02:50:02 +00:00
|
|
|
named_scope :written_before, lambda { |time|
|
|
|
|
{ :conditions => ['written_on < ?', time] }
|
|
|
|
}
|
|
|
|
named_scope :approved, :conditions => {:approved => true}
|
|
|
|
named_scope :replied, :conditions => ['replies_count > 0']
|
|
|
|
named_scope :anonymous_extension do
|
|
|
|
def one
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module NamedExtension
|
|
|
|
def two
|
|
|
|
2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module MultipleExtensionOne
|
|
|
|
def extension_one
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module MultipleExtensionTwo
|
|
|
|
def extension_two
|
|
|
|
2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
named_scope :named_extension, :extend => NamedExtension
|
|
|
|
named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne]
|
2008-04-01 06:46:40 +00:00
|
|
|
|
2006-03-18 20:25:50 +00:00
|
|
|
has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
|
2004-11-24 01:04:44 +00:00
|
|
|
serialize :content
|
2008-01-18 07:27:03 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
before_create :default_written_on
|
2004-12-14 12:32:29 +00:00
|
|
|
before_destroy :destroy_children
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
def parent
|
2006-03-05 18:43:56 +00:00
|
|
|
Topic.find(parent_id)
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-18 07:27:03 +00:00
|
|
|
|
2006-09-02 21:00:09 +00:00
|
|
|
# trivial method for testing Array#to_xml with :methods
|
|
|
|
def topic_id
|
|
|
|
id
|
|
|
|
end
|
2008-01-18 07:27:03 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
protected
|
2007-09-17 09:29:02 +00:00
|
|
|
def approved=(val)
|
|
|
|
@custom_approved = val
|
|
|
|
write_attribute(:approved, val)
|
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
def default_written_on
|
|
|
|
self.written_on = Time.now unless attribute_present?("written_on")
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_children
|
|
|
|
self.class.delete_all "parent_id = #{id}"
|
|
|
|
end
|
2007-10-08 06:21:38 +00:00
|
|
|
|
|
|
|
def after_initialize
|
|
|
|
if self.new_record?
|
|
|
|
self.author_email_address = 'test@test.com'
|
|
|
|
end
|
|
|
|
end
|
2008-04-01 06:46:40 +00:00
|
|
|
end
|