PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/activerecord/test/models/company.rb

https://gitlab.com/mpivaa/rails
Ruby | 225 lines | 174 code | 44 blank | 7 comment | 5 complexity | 59c2569e13ec85ce9aafb75cc77d16db MD5 | raw file
  1. class AbstractCompany < ActiveRecord::Base
  2. self.abstract_class = true
  3. end
  4. class Company < AbstractCompany
  5. self.sequence_name = :companies_nonstd_seq
  6. validates_presence_of :name
  7. has_one :dummy_account, :foreign_key => "firm_id", :class_name => "Account"
  8. has_many :contracts
  9. has_many :developers, :through => :contracts
  10. has_many :accounts
  11. scope :of_first_firm, lambda {
  12. joins(:account => :firm).
  13. where('firms.id' => 1)
  14. }
  15. def arbitrary_method
  16. "I am Jack's profound disappointment"
  17. end
  18. private
  19. def private_method
  20. "I am Jack's innermost fears and aspirations"
  21. end
  22. end
  23. module Namespaced
  24. class Company < ::Company
  25. end
  26. class Firm < ::Company
  27. has_many :clients, :class_name => 'Namespaced::Client'
  28. end
  29. class Client < ::Company
  30. end
  31. end
  32. class Firm < Company
  33. to_param :name
  34. has_many :clients, -> { order "id" }, :dependent => :destroy, :before_remove => :log_before_remove, :after_remove => :log_after_remove
  35. has_many :unsorted_clients, :class_name => "Client"
  36. has_many :unsorted_clients_with_symbol, :class_name => :Client
  37. has_many :clients_sorted_desc, -> { order "id DESC" }, :class_name => "Client"
  38. has_many :clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client", :inverse_of => :firm
  39. has_many :clients_ordered_by_name, -> { order "name" }, :class_name => "Client"
  40. has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false
  41. has_many :dependent_clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client", :dependent => :destroy
  42. has_many :exclusively_dependent_clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client", :dependent => :delete_all
  43. has_many :limited_clients, -> { limit 1 }, :class_name => "Client"
  44. has_many :clients_with_interpolated_conditions, ->(firm) { where "rating > #{firm.rating}" }, :class_name => "Client"
  45. has_many :clients_like_ms, -> { where("name = 'Microsoft'").order("id") }, :class_name => "Client"
  46. has_many :clients_like_ms_with_hash_conditions, -> { where(:name => 'Microsoft').order("id") }, :class_name => "Client"
  47. has_many :plain_clients, :class_name => 'Client'
  48. has_many :clients_using_primary_key, :class_name => 'Client',
  49. :primary_key => 'name', :foreign_key => 'firm_name'
  50. has_many :clients_using_primary_key_with_delete_all, :class_name => 'Client',
  51. :primary_key => 'name', :foreign_key => 'firm_name', :dependent => :delete_all
  52. has_many :clients_grouped_by_firm_id, -> { group("firm_id").select("firm_id") }, :class_name => "Client"
  53. has_many :clients_grouped_by_name, -> { group("name").select("name") }, :class_name => "Client"
  54. has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true
  55. has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false
  56. has_one :account_with_select, -> { select("id, firm_id") }, :foreign_key => "firm_id", :class_name=>'Account'
  57. has_one :readonly_account, -> { readonly }, :foreign_key => "firm_id", :class_name => "Account"
  58. # added order by id as in fixtures there are two accounts for Rails Core
  59. # Oracle tests were failing because of that as the second fixture was selected
  60. has_one :account_using_primary_key, -> { order('id') }, :primary_key => "firm_id", :class_name => "Account"
  61. has_one :account_using_foreign_and_primary_keys, :foreign_key => "firm_name", :primary_key => "name", :class_name => "Account"
  62. has_one :account_with_inexistent_foreign_key, class_name: 'Account', foreign_key: "inexistent"
  63. has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete
  64. has_one :account_limit_500_with_hash_conditions, -> { where :credit_limit => 500 }, :foreign_key => "firm_id", :class_name => "Account"
  65. has_one :unautosaved_account, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
  66. has_many :accounts
  67. has_many :unautosaved_accounts, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
  68. has_many :association_with_references, -> { references(:foo) }, :class_name => 'Client'
  69. def log
  70. @log ||= []
  71. end
  72. private
  73. def log_before_remove(record)
  74. log << "before_remove#{record.id}"
  75. end
  76. def log_after_remove(record)
  77. log << "after_remove#{record.id}"
  78. end
  79. end
  80. class DependentFirm < Company
  81. has_one :account, :foreign_key => "firm_id", :dependent => :nullify
  82. has_many :companies, :foreign_key => 'client_of', :dependent => :nullify
  83. has_one :company, :foreign_key => 'client_of', :dependent => :nullify
  84. end
  85. class RestrictedWithExceptionFirm < Company
  86. has_one :account, -> { order("id") }, :foreign_key => "firm_id", :dependent => :restrict_with_exception
  87. has_many :companies, -> { order("id") }, :foreign_key => 'client_of', :dependent => :restrict_with_exception
  88. end
  89. class RestrictedWithErrorFirm < Company
  90. has_one :account, -> { order("id") }, :foreign_key => "firm_id", :dependent => :restrict_with_error
  91. has_many :companies, -> { order("id") }, :foreign_key => 'client_of', :dependent => :restrict_with_error
  92. end
  93. class Client < Company
  94. belongs_to :firm, :foreign_key => "client_of"
  95. belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
  96. belongs_to :firm_with_select, -> { select("id") }, :class_name => "Firm", :foreign_key => "firm_id"
  97. belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
  98. belongs_to :firm_with_condition, -> { where "1 = ?", 1 }, :class_name => "Firm", :foreign_key => "client_of"
  99. belongs_to :firm_with_primary_key, :class_name => "Firm", :primary_key => "name", :foreign_key => "firm_name"
  100. belongs_to :firm_with_primary_key_symbols, :class_name => "Firm", :primary_key => :name, :foreign_key => :firm_name
  101. belongs_to :readonly_firm, -> { readonly }, :class_name => "Firm", :foreign_key => "firm_id"
  102. belongs_to :bob_firm, -> { where :name => "Bob" }, :class_name => "Firm", :foreign_key => "client_of"
  103. has_many :accounts, :through => :firm, :source => :accounts
  104. belongs_to :account
  105. validate do
  106. firm
  107. end
  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 rating?
  133. query_attribute :rating
  134. end
  135. def overwrite_to_raise
  136. end
  137. class << self
  138. private
  139. def private_method
  140. "darkness"
  141. end
  142. end
  143. end
  144. class ExclusivelyDependentFirm < Company
  145. has_one :account, :foreign_key => "firm_id", :dependent => :delete
  146. has_many :dependent_sanitized_conditional_clients_of_firm, -> { order("id").where("name = 'BigShot Inc.'") }, :foreign_key => "client_of", :class_name => "Client", :dependent => :delete_all
  147. has_many :dependent_conditional_clients_of_firm, -> { order("id").where("name = ?", 'BigShot Inc.') }, :foreign_key => "client_of", :class_name => "Client", :dependent => :delete_all
  148. end
  149. class SpecialClient < Client
  150. end
  151. class VerySpecialClient < SpecialClient
  152. end
  153. class Account < ActiveRecord::Base
  154. belongs_to :firm, :class_name => 'Company'
  155. belongs_to :unautosaved_firm, :foreign_key => "firm_id", :class_name => "Firm", :autosave => false
  156. alias_attribute :available_credit, :credit_limit
  157. def self.destroyed_account_ids
  158. @destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] }
  159. end
  160. # Test private kernel method through collection proxy using has_many.
  161. def self.open
  162. where('firm_name = ?', '37signals')
  163. end
  164. before_destroy do |account|
  165. if account.firm
  166. Account.destroyed_account_ids[account.firm.id] << account.id
  167. end
  168. true
  169. end
  170. validate :check_empty_credit_limit
  171. protected
  172. def check_empty_credit_limit
  173. errors.add("credit_limit", :blank) if credit_limit.blank?
  174. end
  175. private
  176. def private_method
  177. "Sir, yes sir!"
  178. end
  179. end