/activerecord/test/models/author.rb

https://github.com/diminish7/rails · Ruby · 201 lines · 160 code · 41 blank · 0 comment · 0 complexity · 4a9bb11a820d67ce2518e29406e4427a MD5 · raw file

  1. class Author < ActiveRecord::Base
  2. has_many :posts
  3. has_one :post
  4. has_many :very_special_comments, :through => :posts
  5. has_many :posts_with_comments, -> { includes(:comments) }, :class_name => "Post"
  6. has_many :popular_grouped_posts, -> { includes(:comments).group("type").having("SUM(comments_count) > 1").select("type") }, :class_name => "Post"
  7. has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order('comments.id') }, :class_name => "Post"
  8. has_many :posts_sorted_by_id_limited, -> { order('posts.id').limit(1) }, :class_name => "Post"
  9. has_many :posts_with_categories, -> { includes(:categories) }, :class_name => "Post"
  10. has_many :posts_with_comments_and_categories, -> { includes(:comments, :categories).order("posts.id") }, :class_name => "Post"
  11. has_many :posts_containing_the_letter_a, :class_name => "Post"
  12. has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension
  13. def testing_proxy_owner
  14. proxy_owner
  15. end
  16. def testing_proxy_reflection
  17. proxy_reflection
  18. end
  19. def testing_proxy_target
  20. proxy_target
  21. end
  22. end
  23. has_one :post_about_thinking, -> { where("posts.title like '%thinking%'") }, :class_name => 'Post'
  24. has_one :post_about_thinking_with_last_comment, -> { where("posts.title like '%thinking%'").includes(:last_comment) }, :class_name => 'Post'
  25. has_many :comments, :through => :posts
  26. has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
  27. has_many :comments_with_order_and_conditions, -> { order('comments.body').where("comments.body like 'Thank%'") }, :through => :posts, :source => :comments
  28. has_many :comments_with_include, -> { includes(:post) }, :through => :posts, :source => :comments
  29. has_many :first_posts
  30. has_many :comments_on_first_posts, -> { order('posts.id desc, comments.id asc') }, :through => :first_posts, :source => :comments
  31. has_one :first_post
  32. has_one :comment_on_first_post, -> { order('posts.id desc, comments.id asc') }, :through => :first_post, :source => :comments
  33. has_many :thinking_posts, -> { where(:title => 'So I was thinking') }, :dependent => :delete_all, :class_name => 'Post'
  34. has_many :welcome_posts, -> { where(:title => 'Welcome to the weblog') }, :class_name => 'Post'
  35. has_many :comments_desc, -> { order('comments.id DESC') }, :through => :posts, :source => :comments
  36. has_many :limited_comments, -> { limit(1) }, :through => :posts, :source => :comments
  37. has_many :funky_comments, :through => :posts, :source => :comments
  38. has_many :ordered_uniq_comments, -> { uniq.order('comments.id') }, :through => :posts, :source => :comments
  39. has_many :ordered_uniq_comments_desc, -> { uniq.order('comments.id DESC') }, :through => :posts, :source => :comments
  40. has_many :readonly_comments, -> { readonly }, :through => :posts, :source => :comments
  41. has_many :special_posts
  42. has_many :special_post_comments, :through => :special_posts, :source => :comments
  43. has_many :sti_posts, :class_name => 'StiPost'
  44. has_many :sti_post_comments, :through => :sti_posts, :source => :comments
  45. has_many :special_nonexistant_posts, -> { where("posts.body = 'nonexistant'") }, :class_name => "SpecialPost"
  46. has_many :special_nonexistant_post_comments, -> { where('comments.post_id' => 0) }, :through => :special_nonexistant_posts, :source => :comments
  47. has_many :nonexistant_comments, :through => :posts
  48. has_many :hello_posts, -> { where "posts.body = 'hello'" }, :class_name => "Post"
  49. has_many :hello_post_comments, :through => :hello_posts, :source => :comments
  50. has_many :posts_with_no_comments, -> { where('comments.id' => nil).includes(:comments) }, :class_name => 'Post'
  51. has_many :hello_posts_with_hash_conditions, -> { where(:body => 'hello') }, :class_name => "Post"
  52. has_many :hello_post_comments_with_hash_conditions, :through =>
  53. :hello_posts_with_hash_conditions, :source => :comments
  54. has_many :other_posts, :class_name => "Post"
  55. has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
  56. :after_add => :log_after_adding,
  57. :before_remove => :log_before_removing,
  58. :after_remove => :log_after_removing
  59. has_many :posts_with_proc_callbacks, :class_name => "Post",
  60. :before_add => Proc.new {|o, r| o.post_log << "before_adding#{r.id || '<new>'}"},
  61. :after_add => Proc.new {|o, r| o.post_log << "after_adding#{r.id || '<new>'}"},
  62. :before_remove => Proc.new {|o, r| o.post_log << "before_removing#{r.id}"},
  63. :after_remove => Proc.new {|o, r| o.post_log << "after_removing#{r.id}"}
  64. has_many :posts_with_multiple_callbacks, :class_name => "Post",
  65. :before_add => [:log_before_adding, Proc.new {|o, r| o.post_log << "before_adding_proc#{r.id || '<new>'}"}],
  66. :after_add => [:log_after_adding, Proc.new {|o, r| o.post_log << "after_adding_proc#{r.id || '<new>'}"}]
  67. has_many :unchangable_posts, :class_name => "Post", :before_add => :raise_exception, :after_add => :log_after_adding
  68. has_many :categorizations
  69. has_many :categories, :through => :categorizations
  70. has_many :named_categories, :through => :categorizations
  71. has_many :special_categorizations
  72. has_many :special_categories, :through => :special_categorizations, :source => :category
  73. has_one :special_category, :through => :special_categorizations, :source => :category
  74. has_many :categories_like_general, -> { where(:name => 'General') }, :through => :categorizations, :source => :category, :class_name => 'Category'
  75. has_many :categorized_posts, :through => :categorizations, :source => :post
  76. has_many :unique_categorized_posts, -> { uniq }, :through => :categorizations, :source => :post
  77. has_many :nothings, :through => :kateggorisatons, :class_name => 'Category'
  78. has_many :author_favorites
  79. has_many :favorite_authors, -> { order('name') }, :through => :author_favorites
  80. has_many :taggings, :through => :posts
  81. has_many :taggings_2, :through => :posts, :source => :tagging
  82. has_many :tags, :through => :posts
  83. has_many :post_categories, :through => :posts, :source => :categories
  84. has_many :tagging_tags, :through => :taggings, :source => :tag
  85. has_many :similar_posts, -> { uniq }, :through => :tags, :source => :tagged_posts
  86. has_many :distinct_tags, -> { select("DISTINCT tags.*").order("tags.name") }, :through => :posts, :source => :tags
  87. has_many :tags_with_primary_key, :through => :posts
  88. has_many :books
  89. has_many :subscriptions, :through => :books
  90. has_many :subscribers, -> { order("subscribers.nick") }, :through => :subscriptions
  91. has_many :distinct_subscribers, -> { select("DISTINCT subscribers.*").order("subscribers.nick") }, :through => :subscriptions, :source => :subscriber
  92. has_one :essay, :primary_key => :name, :as => :writer
  93. has_one :essay_category, :through => :essay, :source => :category
  94. has_one :essay_owner, :through => :essay, :source => :owner
  95. has_one :essay_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
  96. has_one :essay_category_2, :through => :essay_2, :source => :category
  97. has_many :essays, :primary_key => :name, :as => :writer
  98. has_many :essay_categories, :through => :essays, :source => :category
  99. has_many :essay_owners, :through => :essays, :source => :owner
  100. has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
  101. has_many :essay_categories_2, :through => :essays_2, :source => :category
  102. belongs_to :owned_essay, :primary_key => :name, :class_name => 'Essay'
  103. has_one :owned_essay_category, :through => :owned_essay, :source => :category
  104. belongs_to :author_address, :dependent => :destroy
  105. belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
  106. has_many :category_post_comments, :through => :categories, :source => :post_comments
  107. has_many :misc_posts, -> { where(:posts => { :title => ['misc post by bob', 'misc post by mary'] }) }, :class_name => 'Post'
  108. has_many :misc_post_first_blue_tags, :through => :misc_posts, :source => :first_blue_tags
  109. has_many :misc_post_first_blue_tags_2, -> { where(:posts => { :title => ['misc post by bob', 'misc post by mary'] }) },
  110. :through => :posts, :source => :first_blue_tags_2
  111. has_many :posts_with_default_include, :class_name => 'PostWithDefaultInclude'
  112. has_many :comments_on_posts_with_default_include, :through => :posts_with_default_include, :source => :comments
  113. scope :relation_include_posts, -> { includes(:posts) }
  114. scope :relation_include_tags, -> { includes(:tags) }
  115. attr_accessor :post_log
  116. after_initialize :set_post_log
  117. def set_post_log
  118. @post_log = []
  119. end
  120. def label
  121. "#{id}-#{name}"
  122. end
  123. def social
  124. %w(twitter github)
  125. end
  126. validates_presence_of :name
  127. private
  128. def log_before_adding(object)
  129. @post_log << "before_adding#{object.id || '<new>'}"
  130. end
  131. def log_after_adding(object)
  132. @post_log << "after_adding#{object.id}"
  133. end
  134. def log_before_removing(object)
  135. @post_log << "before_removing#{object.id}"
  136. end
  137. def log_after_removing(object)
  138. @post_log << "after_removing#{object.id}"
  139. end
  140. def raise_exception(object)
  141. raise Exception.new("You can't add a post")
  142. end
  143. end
  144. class AuthorAddress < ActiveRecord::Base
  145. has_one :author
  146. def self.destroyed_author_address_ids
  147. @destroyed_author_address_ids ||= []
  148. end
  149. before_destroy do |author_address|
  150. AuthorAddress.destroyed_author_address_ids << author_address.id
  151. end
  152. end
  153. class AuthorFavorite < ActiveRecord::Base
  154. belongs_to :author
  155. belongs_to :favorite_author, :class_name => "Author"
  156. end