topic.rb 2.8 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
5
      where "written_on < ?", time
6 7
    end
  }
8 9
  scope :approved, -> { where(approved: true) }
  scope :rejected, -> { where(approved: false) }
10

11
  scope :scope_with_lambda, lambda { all }
12

13
  scope :by_lifo, -> { where(author_name: "lifo") }
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
  scope :with_object, Class.new(Struct.new(:klass)) {
    def call
25
      klass.where(approved: true)
26 27 28
    end
  }.new(self)

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

M
Mehmet Emin İNAÇ 已提交
35
  has_many :replies, dependent: :destroy, foreign_key: "parent_id", autosave: true
36
  has_many :approved_replies, -> { approved }, class_name: "Reply", foreign_key: "parent_id", counter_cache: "replies_count"
37

38 39
  has_many :unique_replies, dependent: :destroy, foreign_key: "parent_id"
  has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"
40

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
  alias_attribute :heading, :title

57 58 59 60 61 62 63 64 65
  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

66 67 68 69 70
  class_attribute :after_initialize_called
  after_initialize do
    self.class.after_initialize_called = true
  end

71 72 73 74 75
  def approved=(val)
    @custom_approved = val
    write_attribute(:approved, val)
  end

76
  private
77

D
Initial  
David Heinemeier Hansson 已提交
78 79 80 81 82
    def default_written_on
      self.written_on = Time.now unless attribute_present?("written_on")
    end

    def destroy_children
83
      self.class.where("parent_id = #{id}").delete_all
D
Initial  
David Heinemeier Hansson 已提交
84
    end
85

86
    def set_email_address
87
      unless persisted?
88
        self.author_email_address = "test@test.com"
89 90
      end
    end
91 92 93 94 95 96

    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
97
end
98

A
Alvaro Bautista 已提交
99 100 101 102
class ImportantTopic < Topic
  serialize :important, Hash
end

103 104 105 106
class DefaultRejectedTopic < Topic
  default_scope -> { where(approved: false) }
end

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

114 115
module Web
  class Topic < ActiveRecord::Base
116
    has_many :replies, dependent: :destroy, foreign_key: "parent_id", class_name: "Web::Reply"
117
  end
118
end