2004-11-24 01:04:44 +00:00
|
|
|
class Topic < ActiveRecord::Base
|
2010-01-17 23:08:19 +00:00
|
|
|
scope :base
|
|
|
|
scope :written_before, lambda { |time|
|
2009-03-12 15:06:19 +00:00
|
|
|
if time
|
|
|
|
{ :conditions => ['written_on < ?', time] }
|
|
|
|
end
|
2008-03-24 02:50:02 +00:00
|
|
|
}
|
2010-01-17 23:08:19 +00:00
|
|
|
scope :approved, :conditions => {:approved => true}
|
|
|
|
scope :rejected, :conditions => {:approved => false}
|
2009-01-24 17:54:10 +00:00
|
|
|
|
2010-01-17 23:08:19 +00:00
|
|
|
scope :by_lifo, :conditions => {:author_name => 'lifo'}
|
2008-07-09 13:08:04 +00:00
|
|
|
|
2010-01-17 23:08:19 +00:00
|
|
|
scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}
|
|
|
|
scope 'approved_as_string', :conditions => {:approved => true}
|
|
|
|
scope :replied, :conditions => ['replies_count > 0']
|
|
|
|
scope :anonymous_extension do
|
2008-03-24 02:50:02 +00:00
|
|
|
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
|
2010-01-17 23:08:19 +00:00
|
|
|
scope :named_extension, :extend => NamedExtension
|
|
|
|
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"
|
2009-07-15 20:22:25 +00:00
|
|
|
has_many :replies_with_primary_key, :class_name => "Reply", :dependent => :destroy, :primary_key => "title", :foreign_key => "parent_title"
|
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
|
|
|
|
2009-09-08 15:22:45 +00:00
|
|
|
before_validation :before_validation_for_transaction
|
|
|
|
before_save :before_save_for_transaction
|
|
|
|
before_destroy :before_destroy_for_transaction
|
|
|
|
|
|
|
|
after_save :after_save_for_transaction
|
|
|
|
after_create :after_create_for_transaction
|
|
|
|
|
|
|
|
after_initialize :set_email_address
|
|
|
|
|
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
|
|
|
|
2009-09-08 15:22:45 +00:00
|
|
|
def set_email_address
|
2007-10-08 06:21:38 +00:00
|
|
|
if self.new_record?
|
|
|
|
self.author_email_address = 'test@test.com'
|
|
|
|
end
|
|
|
|
end
|
2009-09-08 15:22:45 +00:00
|
|
|
|
|
|
|
def before_validation_for_transaction; end
|
|
|
|
def before_save_for_transaction; end
|
|
|
|
def before_destroy_for_transaction; end
|
|
|
|
def after_save_for_transaction; end
|
|
|
|
def after_create_for_transaction; end
|
2008-04-01 06:46:40 +00:00
|
|
|
end
|
2008-12-31 09:43:13 +00:00
|
|
|
|
|
|
|
module Web
|
|
|
|
class Topic < ActiveRecord::Base
|
|
|
|
has_many :replies, :dependent => :destroy, :foreign_key => "parent_id", :class_name => 'Web::Reply'
|
|
|
|
end
|
2009-09-08 15:10:14 +00:00
|
|
|
end
|