topic.rb 2.4 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
class Topic < ActiveRecord::Base
P
Pratik Naik 已提交
2 3
  scope :base
  scope :written_before, lambda { |time|
4 5 6
    if time
      { :conditions => ['written_on < ?', time] }
    end
7
  }
P
Pratik Naik 已提交
8 9
  scope :approved, :conditions => {:approved => true}
  scope :rejected, :conditions => {:approved => false}
10

P
Pratik Naik 已提交
11
  scope :by_lifo, :conditions => {:author_name => 'lifo'}
12
  
P
Pratik Naik 已提交
13 14 15 16
  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
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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
P
Pratik Naik 已提交
36 37
  scope :named_extension, :extend => NamedExtension
  scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne]
38

39
  has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
40
  has_many :replies_with_primary_key, :class_name => "Reply", :dependent => :destroy, :primary_key => "title", :foreign_key => "parent_title"
D
Initial  
David Heinemeier Hansson 已提交
41
  serialize :content
J
Jeremy Kemper 已提交
42

D
Initial  
David Heinemeier Hansson 已提交
43
  before_create  :default_written_on
44
  before_destroy :destroy_children
D
Initial  
David Heinemeier Hansson 已提交
45 46

  def parent
47
    Topic.find(parent_id)
D
Initial  
David Heinemeier Hansson 已提交
48
  end
J
Jeremy Kemper 已提交
49

50 51 52 53
  # trivial method for testing Array#to_xml with :methods
  def topic_id
    id
  end
J
Jeremy Kemper 已提交
54

55 56 57 58 59 60 61 62 63
  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

D
Initial  
David Heinemeier Hansson 已提交
64
  protected
65 66 67 68 69
    def approved=(val)
      @custom_approved = val
      write_attribute(:approved, val)
    end

D
Initial  
David Heinemeier Hansson 已提交
70 71 72 73 74 75 76
    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
77

78
    def set_email_address
79 80 81 82
      if self.new_record?
        self.author_email_address = 'test@test.com'
      end
    end
83 84 85 86 87 88

    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
89
end
90 91 92 93 94

module Web
  class Topic < ActiveRecord::Base
    has_many :replies, :dependent => :destroy, :foreign_key => "parent_id", :class_name => 'Web::Reply'
  end
95
end