Froze rails gems
[depot.git] / vendor / rails / activerecord / test / models / company.rb
1 class AbstractCompany < ActiveRecord::Base
2 self.abstract_class = true
3 end
4
5 class Company < AbstractCompany
6 attr_protected :rating
7 set_sequence_name :companies_nonstd_seq
8
9 validates_presence_of :name
10
11 has_one :dummy_account, :foreign_key => "firm_id", :class_name => "Account"
12
13 def arbitrary_method
14 "I am Jack's profound disappointment"
15 end
16
17 private
18
19 def private_method
20 "I am Jack's innermost fears and aspirations"
21 end
22 end
23
24 module Namespaced
25 class Company < ::Company
26 end
27
28 class Firm < ::Company
29 has_many :clients, :class_name => 'Namespaced::Client'
30 end
31
32 class Client < ::Company
33 end
34 end
35
36 class Firm < Company
37 has_many :clients, :order => "id", :dependent => :destroy, :counter_sql =>
38 "SELECT COUNT(*) FROM companies WHERE firm_id = 1 " +
39 "AND (#{QUOTED_TYPE} = 'Client' OR #{QUOTED_TYPE} = 'SpecialClient' OR #{QUOTED_TYPE} = 'VerySpecialClient' )"
40 has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
41 has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
42 has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false
43 has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :destroy
44 has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all
45 has_many :limited_clients, :class_name => "Client", :order => "id", :limit => 1
46 has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
47 has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => 'rating > #{rating}'
48 has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id"
49 has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
50 has_many :clients_using_counter_sql, :class_name => "Client",
51 :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
52 :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}'
53 has_many :clients_using_zero_counter_sql, :class_name => "Client",
54 :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
55 :counter_sql => 'SELECT 0 FROM companies WHERE client_of = #{id}'
56 has_many :no_clients_using_counter_sql, :class_name => "Client",
57 :finder_sql => 'SELECT * FROM companies WHERE client_of = 1000',
58 :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
59 has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1'
60 has_many :plain_clients, :class_name => 'Client'
61 has_many :readonly_clients, :class_name => 'Client', :readonly => true
62 has_many :clients_using_primary_key, :class_name => 'Client',
63 :primary_key => 'name', :foreign_key => 'firm_name'
64 has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id", :select => "firm_id"
65 has_many :clients_grouped_by_name, :class_name => "Client", :group => "name", :select => "name"
66
67 has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true
68 has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false
69 has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id", :class_name=>'Account'
70 has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
71 has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account"
72 end
73
74 class DependentFirm < Company
75 has_one :account, :foreign_key => "firm_id", :dependent => :nullify
76 has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify
77 end
78
79 class ExclusivelyDependentFirm < Company
80 has_one :account, :foreign_key => "firm_id", :dependent => :delete
81 has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'"
82 has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.']
83 end
84
85 class Client < Company
86 belongs_to :firm, :foreign_key => "client_of"
87 belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
88 belongs_to :firm_with_select, :class_name => "Firm", :foreign_key => "firm_id", :select => "id"
89 belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
90 belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1]
91 belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true
92
93 # Record destruction so we can test whether firm.clients.clear has
94 # is calling client.destroy, deleting from the database, or setting
95 # foreign keys to NULL.
96 def self.destroyed_client_ids
97 @destroyed_client_ids ||= Hash.new { |h,k| h[k] = [] }
98 end
99
100 before_destroy do |client|
101 if client.firm
102 Client.destroyed_client_ids[client.firm.id] << client.id
103 end
104 true
105 end
106
107 # Used to test that read and question methods are not generated for these attributes
108 def ruby_type
109 read_attribute :ruby_type
110 end
111
112 def rating?
113 query_attribute :rating
114 end
115
116 class << self
117 private
118
119 def private_method
120 "darkness"
121 end
122 end
123 end
124
125
126 class SpecialClient < Client
127 end
128
129 class VerySpecialClient < SpecialClient
130 end
131
132 class Account < ActiveRecord::Base
133 belongs_to :firm
134
135 def self.destroyed_account_ids
136 @destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] }
137 end
138
139 before_destroy do |account|
140 if account.firm
141 Account.destroyed_account_ids[account.firm.id] << account.id
142 end
143 true
144 end
145
146 protected
147 def validate
148 errors.add_on_empty "credit_limit"
149 end
150
151 private
152
153 def private_method
154 "Sir, yes sir!"
155 end
156 end