Merged updates from trunk into stable branch
[feedcatcher.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 :unsorted_clients, :class_name => "Client"
41 has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
42 has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
43 has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false
44 has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :destroy
45 has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all
46 has_many :limited_clients, :class_name => "Client", :order => "id", :limit => 1
47 has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
48 has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => 'rating > #{rating}'
49 has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id"
50 has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
51 has_many :clients_using_counter_sql, :class_name => "Client",
52 :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
53 :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}'
54 has_many :clients_using_zero_counter_sql, :class_name => "Client",
55 :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
56 :counter_sql => 'SELECT 0 FROM companies WHERE client_of = #{id}'
57 has_many :no_clients_using_counter_sql, :class_name => "Client",
58 :finder_sql => 'SELECT * FROM companies WHERE client_of = 1000',
59 :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
60 has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1'
61 has_many :plain_clients, :class_name => 'Client'
62 has_many :readonly_clients, :class_name => 'Client', :readonly => true
63 has_many :clients_using_primary_key, :class_name => 'Client',
64 :primary_key => 'name', :foreign_key => 'firm_name'
65 has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id", :select => "firm_id"
66 has_many :clients_grouped_by_name, :class_name => "Client", :group => "name", :select => "name"
67
68 has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true
69 has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false
70 has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id", :class_name=>'Account'
71 has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
72 has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account"
73 has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete
74 end
75
76 class DependentFirm < Company
77 has_one :account, :foreign_key => "firm_id", :dependent => :nullify
78 has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify
79 end
80
81 class ExclusivelyDependentFirm < Company
82 has_one :account, :foreign_key => "firm_id", :dependent => :delete
83 has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'"
84 has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.']
85 has_many :dependent_hash_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => {:name => 'BigShot Inc.'}
86 end
87
88 class Client < Company
89 belongs_to :firm, :foreign_key => "client_of"
90 belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
91 belongs_to :firm_with_select, :class_name => "Firm", :foreign_key => "firm_id", :select => "id"
92 belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
93 belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1]
94 belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true
95
96 # Record destruction so we can test whether firm.clients.clear has
97 # is calling client.destroy, deleting from the database, or setting
98 # foreign keys to NULL.
99 def self.destroyed_client_ids
100 @destroyed_client_ids ||= Hash.new { |h,k| h[k] = [] }
101 end
102
103 before_destroy do |client|
104 if client.firm
105 Client.destroyed_client_ids[client.firm.id] << client.id
106 end
107 true
108 end
109
110 # Used to test that read and question methods are not generated for these attributes
111 def ruby_type
112 read_attribute :ruby_type
113 end
114
115 def rating?
116 query_attribute :rating
117 end
118
119 class << self
120 private
121
122 def private_method
123 "darkness"
124 end
125 end
126 end
127
128
129 class SpecialClient < Client
130 end
131
132 class VerySpecialClient < SpecialClient
133 end
134
135 class Account < ActiveRecord::Base
136 belongs_to :firm
137
138 def self.destroyed_account_ids
139 @destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] }
140 end
141
142 before_destroy do |account|
143 if account.firm
144 Account.destroyed_account_ids[account.firm.id] << account.id
145 end
146 true
147 end
148
149 protected
150 def validate
151 errors.add_on_empty "credit_limit"
152 end
153
154 private
155
156 def private_method
157 "Sir, yes sir!"
158 end
159 end