2004-11-23 20:04:44 -05:00
|
|
|
class Topic < ActiveRecord::Base
|
2008-04-30 01:25:52 -04:00
|
|
|
named_scope :base
|
2008-03-23 22:50:02 -04:00
|
|
|
named_scope :written_before, lambda { |time|
|
|
|
|
{ :conditions => ['written_on < ?', time] }
|
|
|
|
}
|
|
|
|
named_scope :approved, :conditions => {:approved => true}
|
2008-07-09 09:08:04 -04:00
|
|
|
named_scope :by_lifo, :conditions => {:author_name => 'lifo'}
|
|
|
|
|
2008-06-27 20:57:32 -04:00
|
|
|
named_scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}
|
2008-06-13 16:39:10 -04:00
|
|
|
named_scope 'approved_as_string', :conditions => {:approved => true}
|
2008-03-23 22:50:02 -04:00
|
|
|
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 02:46:40 -04:00
|
|
|
|
2006-03-18 15:25:50 -05:00
|
|
|
has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
|
2004-11-23 20:04:44 -05:00
|
|
|
serialize :content
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
before_create :default_written_on
|
2004-12-14 07:32:29 -05:00
|
|
|
before_destroy :destroy_children
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def parent
|
2006-03-05 13:43:56 -05:00
|
|
|
Topic.find(parent_id)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2006-09-02 17:00:09 -04:00
|
|
|
# trivial method for testing Array#to_xml with :methods
|
|
|
|
def topic_id
|
|
|
|
id
|
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
protected
|
2007-09-17 05:29:02 -04:00
|
|
|
def approved=(val)
|
|
|
|
@custom_approved = val
|
|
|
|
write_attribute(:approved, val)
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05: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 02:21:38 -04:00
|
|
|
|
|
|
|
def after_initialize
|
|
|
|
if self.new_record?
|
|
|
|
self.author_email_address = 'test@test.com'
|
|
|
|
end
|
|
|
|
end
|
2008-04-01 02:46:40 -04:00
|
|
|
end
|