topic.rb 3.0 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
class Topic < ActiveRecord::Base
P
Pratik Naik 已提交
2
  scope :base
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

  ActiveSupport::Deprecation.silence do
    scope :written_before, lambda { |time|
      if time
        { :conditions => ['written_on < ?', time] }
      end
    }

    scope :with_object, Class.new(Struct.new(:klass)) {
      def call
        klass.where(:approved => true)
      end
    }.new(self)
  end

P
Pratik Naik 已提交
18 19
  scope :approved, :conditions => {:approved => true}
  scope :rejected, :conditions => {:approved => false}
20

P
Pratik Naik 已提交
21
  scope :by_lifo, :conditions => {:author_name => 'lifo'}
22

P
Pratik Naik 已提交
23 24 25 26
  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
27 28 29 30
    def one
      1
    end
  end
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  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 已提交
47 48
  scope :named_extension, :extend => NamedExtension
  scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne]
49

50
  has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
51
  has_many :replies_with_primary_key, :class_name => "Reply", :dependent => :destroy, :primary_key => "title", :foreign_key => "parent_title"
52 53 54 55

  has_many :unique_replies, :dependent => :destroy, :foreign_key => "parent_id"
  has_many :silly_unique_replies, :dependent => :destroy, :foreign_key => "parent_id"

D
Initial  
David Heinemeier Hansson 已提交
56
  serialize :content
J
Jeremy Kemper 已提交
57

D
Initial  
David Heinemeier Hansson 已提交
58
  before_create  :default_written_on
59
  before_destroy :destroy_children
D
Initial  
David Heinemeier Hansson 已提交
60

61 62 63 64 65 66
  # Explicitly define as :date column so that returned Oracle DATE values would be typecasted to Date and not Time.
  # Some tests depend on assumption that this attribute will have Date values.
  if current_adapter?(:OracleEnhancedAdapter)
    set_date_columns :last_read
  end

D
Initial  
David Heinemeier Hansson 已提交
67
  def parent
68
    Topic.find(parent_id)
D
Initial  
David Heinemeier Hansson 已提交
69
  end
J
Jeremy Kemper 已提交
70

71 72 73 74
  # trivial method for testing Array#to_xml with :methods
  def topic_id
    id
  end
J
Jeremy Kemper 已提交
75

76 77 78 79 80 81 82 83 84
  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 已提交
85
  protected
86 87 88 89 90
    def approved=(val)
      @custom_approved = val
      write_attribute(:approved, val)
    end

D
Initial  
David Heinemeier Hansson 已提交
91 92 93 94 95 96 97
    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
98

99
    def set_email_address
100
      unless self.persisted?
101 102 103
        self.author_email_address = 'test@test.com'
      end
    end
104 105 106 107 108 109

    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
110
end
111 112 113 114 115

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