Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activerecord / test / models / topic.rb
1 class Topic < ActiveRecord::Base
2 named_scope :base
3 named_scope :written_before, lambda { |time|
4 if time
5 { :conditions => ['written_on < ?', time] }
6 end
7 }
8 named_scope :approved, :conditions => {:approved => true}
9 named_scope :rejected, :conditions => {:approved => false}
10
11 named_scope :by_lifo, :conditions => {:author_name => 'lifo'}
12
13 named_scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}
14 named_scope 'approved_as_string', :conditions => {:approved => true}
15 named_scope :replied, :conditions => ['replies_count > 0']
16 named_scope :anonymous_extension do
17 def one
18 1
19 end
20 end
21 module NamedExtension
22 def two
23 2
24 end
25 end
26 module MultipleExtensionOne
27 def extension_one
28 1
29 end
30 end
31 module MultipleExtensionTwo
32 def extension_two
33 2
34 end
35 end
36 named_scope :named_extension, :extend => NamedExtension
37 named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne]
38
39 named_scope :by_rejected_ids, lambda {{ :conditions => { :id => all(:conditions => {:approved => false}).map(&:id) } }}
40
41 has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
42 serialize :content
43
44 before_create :default_written_on
45 before_destroy :destroy_children
46
47 def parent
48 Topic.find(parent_id)
49 end
50
51 # trivial method for testing Array#to_xml with :methods
52 def topic_id
53 id
54 end
55
56 protected
57 def approved=(val)
58 @custom_approved = val
59 write_attribute(:approved, val)
60 end
61
62 def default_written_on
63 self.written_on = Time.now unless attribute_present?("written_on")
64 end
65
66 def destroy_children
67 self.class.delete_all "parent_id = #{id}"
68 end
69
70 def after_initialize
71 if self.new_record?
72 self.author_email_address = 'test@test.com'
73 end
74 end
75 end
76
77 module Web
78 class Topic < ActiveRecord::Base
79 has_many :replies, :dependent => :destroy, :foreign_key => "parent_id", :class_name => 'Web::Reply'
80 end
81 end