PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/activerecord/test/models/company.rb

https://github.com/wrozka/rails
Ruby | 218 lines | 174 code | 38 blank | 6 comment | 4 complexity | 0a26f736cb39405ca6c6b69563c527d4 MD5 | raw file
  1. class AbstractCompany < ActiveRecord::Base
  2. self.abstract_class = true
  3. end
  4. class Company < AbstractCompany
  5. attr_protected :rating
  6. self.sequence_name = :companies_nonstd_seq
  7. validates_presence_of :name
  8. has_one :dummy_account, :foreign_key => "firm_id", :class_name => "Account"
  9. has_many :contracts
  10. has_many :developers, :through => :contracts
  11. def arbitrary_method
  12. "I am Jack's profound disappointment"
  13. end
  14. private
  15. def private_method
  16. "I am Jack's innermost fears and aspirations"
  17. end
  18. end
  19. module Namespaced
  20. class Company < ::Company
  21. end
  22. class Firm < ::Company
  23. has_many :clients, :class_name => 'Namespaced::Client'
  24. end
  25. class Client < ::Company
  26. end
  27. end
  28. class Firm < Company
  29. has_many :clients, :order => "id", :dependent => :destroy, :counter_sql =>
  30. "SELECT COUNT(*) FROM companies WHERE firm_id = 1 " +
  31. "AND (#{QUOTED_TYPE} = 'Client' OR #{QUOTED_TYPE} = 'SpecialClient' OR #{QUOTED_TYPE} = 'VerySpecialClient' )",
  32. :before_remove => :log_before_remove,
  33. :after_remove => :log_after_remove
  34. has_many :unsorted_clients, :class_name => "Client"
  35. has_many :unsorted_clients_with_symbol, :class_name => :Client
  36. has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
  37. has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
  38. has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false
  39. has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :destroy
  40. has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all
  41. has_many :limited_clients, :class_name => "Client", :limit => 1
  42. has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => proc { "rating > #{rating}" }
  43. has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
  44. has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id"
  45. has_many :clients_using_sql, :class_name => "Client", :finder_sql => proc { "SELECT * FROM companies WHERE client_of = #{id}" }
  46. has_many :clients_using_counter_sql, :class_name => "Client",
  47. :finder_sql => proc { "SELECT * FROM companies WHERE client_of = #{id} " },
  48. :counter_sql => proc { "SELECT COUNT(*) FROM companies WHERE client_of = #{id}" }
  49. has_many :clients_using_zero_counter_sql, :class_name => "Client",
  50. :finder_sql => proc { "SELECT * FROM companies WHERE client_of = #{id}" },
  51. :counter_sql => proc { "SELECT 0 FROM companies WHERE client_of = #{id}" }
  52. has_many :no_clients_using_counter_sql, :class_name => "Client",
  53. :finder_sql => 'SELECT * FROM companies WHERE client_of = 1000',
  54. :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
  55. has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1'
  56. has_many :plain_clients, :class_name => 'Client'
  57. has_many :readonly_clients, :class_name => 'Client', :readonly => true
  58. has_many :clients_using_primary_key, :class_name => 'Client',
  59. :primary_key => 'name', :foreign_key => 'firm_name'
  60. has_many :clients_using_primary_key_with_delete_all, :class_name => 'Client',
  61. :primary_key => 'name', :foreign_key => 'firm_name', :dependent => :delete_all
  62. has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id", :select => "firm_id"
  63. has_many :clients_grouped_by_name, :class_name => "Client", :group => "name", :select => "name"
  64. has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true
  65. has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false
  66. has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id", :class_name=>'Account'
  67. has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
  68. # added order by id as in fixtures there are two accounts for Rails Core
  69. # Oracle tests were failing because of that as the second fixture was selected
  70. has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account", :order => "id"
  71. has_one :account_using_foreign_and_primary_keys, :foreign_key => "firm_name", :primary_key => "name", :class_name => "Account"
  72. has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete
  73. has_one :account_limit_500_with_hash_conditions, :foreign_key => "firm_id", :class_name => "Account", :conditions => { :credit_limit => 500 }
  74. has_one :unautosaved_account, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
  75. has_many :accounts
  76. has_many :unautosaved_accounts, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
  77. def log
  78. @log ||= []
  79. end
  80. private
  81. def log_before_remove(record)
  82. log << "before_remove#{record.id}"
  83. end
  84. def log_after_remove(record)
  85. log << "after_remove#{record.id}"
  86. end
  87. end
  88. class DependentFirm < Company
  89. has_one :account, :foreign_key => "firm_id", :dependent => :nullify
  90. has_many :companies, :foreign_key => 'client_of', :dependent => :nullify
  91. end
  92. class RestrictedFirm < Company
  93. has_one :account, :foreign_key => "firm_id", :dependent => :restrict, :order => "id"
  94. has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :restrict
  95. end
  96. class Client < Company
  97. belongs_to :firm, :foreign_key => "client_of"
  98. belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
  99. belongs_to :firm_with_select, :class_name => "Firm", :foreign_key => "firm_id", :select => "id"
  100. belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
  101. belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1]
  102. belongs_to :firm_with_primary_key, :class_name => "Firm", :primary_key => "name", :foreign_key => "firm_name"
  103. belongs_to :firm_with_primary_key_symbols, :class_name => "Firm", :primary_key => :name, :foreign_key => :firm_name
  104. belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true
  105. belongs_to :bob_firm, :class_name => "Firm", :foreign_key => "client_of", :conditions => { :name => "Bob" }
  106. has_many :accounts, :through => :firm
  107. belongs_to :account
  108. class RaisedOnSave < RuntimeError; end
  109. attr_accessor :raise_on_save
  110. before_save do
  111. raise RaisedOnSave if raise_on_save
  112. end
  113. class RaisedOnDestroy < RuntimeError; end
  114. attr_accessor :raise_on_destroy
  115. before_destroy do
  116. raise RaisedOnDestroy if raise_on_destroy
  117. end
  118. # Record destruction so we can test whether firm.clients.clear has
  119. # is calling client.destroy, deleting from the database, or setting
  120. # foreign keys to NULL.
  121. def self.destroyed_client_ids
  122. @destroyed_client_ids ||= Hash.new { |h,k| h[k] = [] }
  123. end
  124. before_destroy do |client|
  125. if client.firm
  126. Client.destroyed_client_ids[client.firm.id] << client.id
  127. end
  128. true
  129. end
  130. before_destroy :overwrite_to_raise
  131. # Used to test that read and question methods are not generated for these attributes
  132. def ruby_type
  133. read_attribute :ruby_type
  134. end
  135. def rating?
  136. query_attribute :rating
  137. end
  138. def overwrite_to_raise
  139. end
  140. class << self
  141. private
  142. def private_method
  143. "darkness"
  144. end
  145. end
  146. end
  147. class ExclusivelyDependentFirm < Company
  148. has_one :account, :foreign_key => "firm_id", :dependent => :delete
  149. has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'"
  150. has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.']
  151. has_many :dependent_hash_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => {:name => 'BigShot Inc.'}
  152. end
  153. class SpecialClient < Client
  154. end
  155. class VerySpecialClient < SpecialClient
  156. end
  157. class Account < ActiveRecord::Base
  158. belongs_to :firm, :class_name => 'Company'
  159. belongs_to :unautosaved_firm, :foreign_key => "firm_id", :class_name => "Firm", :autosave => false
  160. def self.destroyed_account_ids
  161. @destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] }
  162. end
  163. before_destroy do |account|
  164. if account.firm
  165. Account.destroyed_account_ids[account.firm.id] << account.id
  166. end
  167. true
  168. end
  169. validate :check_empty_credit_limit
  170. protected
  171. def check_empty_credit_limit
  172. errors.add_on_empty "credit_limit"
  173. end
  174. private
  175. def private_method
  176. "Sir, yes sir!"
  177. end
  178. end