topic.rb 3.1 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
class Topic < ActiveRecord::Base
2
  scope :base, -> { all }
3 4
  scope :written_before, lambda { |time|
    if time
J
Jon Leighton 已提交
5
      where 'written_on < ?', time
6 7
    end
  }
J
Jon Leighton 已提交
8 9
  scope :approved, -> { where(:approved => true) }
  scope :rejected, -> { where(:approved => false) }
10

11
  scope :scope_with_lambda, lambda { all }
12

J
Jon Leighton 已提交
13
  scope :by_lifo, -> { where(:author_name => 'lifo') }
J
Jon Leighton 已提交
14
  scope :replied, -> { where 'replies_count > 0' }
J
Jon Leighton 已提交
15 16

  scope 'approved_as_string', -> { where(:approved => true) }
17
  scope :anonymous_extension, -> { all } do
18 19 20 21
    def one
      1
    end
  end
22

23 24 25 26 27 28
  scope :with_object, Class.new(Struct.new(:klass)) {
    def call
      klass.where(:approved => true)
    end
  }.new(self)

29 30 31 32 33 34
  module NamedExtension
    def two
      2
    end
  end

35
  has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
36
  has_many :approved_replies, -> { approved }, class_name: 'Reply', foreign_key: "parent_id", counter_cache: 'replies_count'
37
  has_many :replies_with_primary_key, :class_name => "Reply", :dependent => :destroy, :primary_key => "title", :foreign_key => "parent_title"
38 39 40 41

  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 已提交
42
  serialize :content
J
Jeremy Kemper 已提交
43

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

47 48 49 50 51 52
  # 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 已提交
53
  def parent
54
    Topic.find(parent_id)
D
Initial  
David Heinemeier Hansson 已提交
55
  end
J
Jeremy Kemper 已提交
56

57 58 59 60
  # trivial method for testing Array#to_xml with :methods
  def topic_id
    id
  end
J
Jeremy Kemper 已提交
61

62 63
  alias_attribute :heading, :title

64 65 66 67 68 69 70 71 72
  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

73 74 75 76 77
  class_attribute :after_initialize_called
  after_initialize do
    self.class.after_initialize_called = true
  end

78 79 80 81 82
  def approved=(val)
    @custom_approved = val
    write_attribute(:approved, val)
  end

D
Initial  
David Heinemeier Hansson 已提交
83
  protected
84

D
Initial  
David Heinemeier Hansson 已提交
85 86 87 88 89 90 91
    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
92

93
    def set_email_address
94
      unless self.persisted?
95 96 97
        self.author_email_address = 'test@test.com'
      end
    end
98 99 100 101 102 103

    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
104
end
105

A
Alvaro Bautista 已提交
106 107 108 109
class ImportantTopic < Topic
  serialize :important, Hash
end

N
Nikita Afanasenko 已提交
110
class BlankTopic < Topic
111
  # declared here to make sure that dynamic finder with a bang can find a model that responds to `blank?`
112 113 114
  def blank?
    true
  end
N
Nikita Afanasenko 已提交
115 116
end

117 118 119 120
module Web
  class Topic < ActiveRecord::Base
    has_many :replies, :dependent => :destroy, :foreign_key => "parent_id", :class_name => 'Web::Reply'
  end
121
end