activerecord 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (106) hide show
  1. data/CHANGELOG +581 -0
  2. data/README +361 -0
  3. data/RUNNING_UNIT_TESTS +36 -0
  4. data/dev-utils/eval_debugger.rb +9 -0
  5. data/examples/associations.png +0 -0
  6. data/examples/associations.rb +87 -0
  7. data/examples/shared_setup.rb +15 -0
  8. data/examples/validation.rb +88 -0
  9. data/install.rb +60 -0
  10. data/lib/active_record.rb +48 -0
  11. data/lib/active_record/aggregations.rb +165 -0
  12. data/lib/active_record/associations.rb +536 -0
  13. data/lib/active_record/associations/association_collection.rb +70 -0
  14. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +46 -0
  15. data/lib/active_record/associations/has_many_association.rb +104 -0
  16. data/lib/active_record/base.rb +985 -0
  17. data/lib/active_record/callbacks.rb +337 -0
  18. data/lib/active_record/connection_adapters/abstract_adapter.rb +326 -0
  19. data/lib/active_record/connection_adapters/mysql_adapter.rb +131 -0
  20. data/lib/active_record/connection_adapters/postgresql_adapter.rb +177 -0
  21. data/lib/active_record/connection_adapters/sqlite_adapter.rb +107 -0
  22. data/lib/active_record/deprecated_associations.rb +70 -0
  23. data/lib/active_record/fixtures.rb +172 -0
  24. data/lib/active_record/observer.rb +71 -0
  25. data/lib/active_record/reflection.rb +126 -0
  26. data/lib/active_record/support/class_attribute_accessors.rb +43 -0
  27. data/lib/active_record/support/class_inheritable_attributes.rb +37 -0
  28. data/lib/active_record/support/clean_logger.rb +10 -0
  29. data/lib/active_record/support/inflector.rb +70 -0
  30. data/lib/active_record/transactions.rb +102 -0
  31. data/lib/active_record/validations.rb +205 -0
  32. data/lib/active_record/vendor/mysql.rb +1117 -0
  33. data/lib/active_record/vendor/simple.rb +702 -0
  34. data/lib/active_record/wrappers/yaml_wrapper.rb +15 -0
  35. data/lib/active_record/wrappings.rb +59 -0
  36. data/rakefile +122 -0
  37. data/test/abstract_unit.rb +16 -0
  38. data/test/aggregations_test.rb +34 -0
  39. data/test/all.sh +8 -0
  40. data/test/associations_test.rb +477 -0
  41. data/test/base_test.rb +513 -0
  42. data/test/class_inheritable_attributes_test.rb +33 -0
  43. data/test/connections/native_mysql/connection.rb +24 -0
  44. data/test/connections/native_postgresql/connection.rb +24 -0
  45. data/test/connections/native_sqlite/connection.rb +24 -0
  46. data/test/deprecated_associations_test.rb +336 -0
  47. data/test/finder_test.rb +67 -0
  48. data/test/fixtures/accounts/signals37 +3 -0
  49. data/test/fixtures/accounts/unknown +2 -0
  50. data/test/fixtures/auto_id.rb +4 -0
  51. data/test/fixtures/column_name.rb +3 -0
  52. data/test/fixtures/companies/first_client +6 -0
  53. data/test/fixtures/companies/first_firm +4 -0
  54. data/test/fixtures/companies/second_client +6 -0
  55. data/test/fixtures/company.rb +37 -0
  56. data/test/fixtures/company_in_module.rb +33 -0
  57. data/test/fixtures/course.rb +3 -0
  58. data/test/fixtures/courses/java +2 -0
  59. data/test/fixtures/courses/ruby +2 -0
  60. data/test/fixtures/customer.rb +30 -0
  61. data/test/fixtures/customers/david +6 -0
  62. data/test/fixtures/db_definitions/mysql.sql +96 -0
  63. data/test/fixtures/db_definitions/mysql2.sql +4 -0
  64. data/test/fixtures/db_definitions/postgresql.sql +113 -0
  65. data/test/fixtures/db_definitions/postgresql2.sql +4 -0
  66. data/test/fixtures/db_definitions/sqlite.sql +85 -0
  67. data/test/fixtures/db_definitions/sqlite2.sql +4 -0
  68. data/test/fixtures/default.rb +2 -0
  69. data/test/fixtures/developer.rb +8 -0
  70. data/test/fixtures/developers/david +2 -0
  71. data/test/fixtures/developers/jamis +2 -0
  72. data/test/fixtures/developers_projects/david_action_controller +2 -0
  73. data/test/fixtures/developers_projects/david_active_record +2 -0
  74. data/test/fixtures/developers_projects/jamis_active_record +2 -0
  75. data/test/fixtures/entrant.rb +3 -0
  76. data/test/fixtures/entrants/first +3 -0
  77. data/test/fixtures/entrants/second +3 -0
  78. data/test/fixtures/entrants/third +3 -0
  79. data/test/fixtures/fixture_database.sqlite +0 -0
  80. data/test/fixtures/fixture_database_2.sqlite +0 -0
  81. data/test/fixtures/movie.rb +5 -0
  82. data/test/fixtures/movies/first +2 -0
  83. data/test/fixtures/movies/second +2 -0
  84. data/test/fixtures/project.rb +3 -0
  85. data/test/fixtures/projects/action_controller +2 -0
  86. data/test/fixtures/projects/active_record +2 -0
  87. data/test/fixtures/reply.rb +21 -0
  88. data/test/fixtures/subscriber.rb +5 -0
  89. data/test/fixtures/subscribers/first +2 -0
  90. data/test/fixtures/subscribers/second +2 -0
  91. data/test/fixtures/topic.rb +20 -0
  92. data/test/fixtures/topics/first +9 -0
  93. data/test/fixtures/topics/second +8 -0
  94. data/test/fixtures_test.rb +20 -0
  95. data/test/inflector_test.rb +104 -0
  96. data/test/inheritance_test.rb +125 -0
  97. data/test/lifecycle_test.rb +110 -0
  98. data/test/modules_test.rb +21 -0
  99. data/test/multiple_db_test.rb +46 -0
  100. data/test/pk_test.rb +57 -0
  101. data/test/reflection_test.rb +78 -0
  102. data/test/thread_safety_test.rb +33 -0
  103. data/test/transactions_test.rb +83 -0
  104. data/test/unconnected_test.rb +24 -0
  105. data/test/validations_test.rb +126 -0
  106. metadata +166 -0
@@ -0,0 +1,3 @@
1
+ id => 1
2
+ firm_id => 1
3
+ credit_limit => 50
@@ -0,0 +1,2 @@
1
+ id => 2
2
+ credit_limit => 50
@@ -0,0 +1,4 @@
1
+ class AutoId < ActiveRecord::Base
2
+ def self.table_name () "auto_id_tests" end
3
+ def self.primary_key () "auto_id" end
4
+ end
@@ -0,0 +1,3 @@
1
+ class ColumnName < ActiveRecord::Base
2
+ def self.table_name () "colnametests" end
3
+ end
@@ -0,0 +1,6 @@
1
+ id => 2
2
+ type => Client
3
+ firm_id => 1
4
+ client_of => 2
5
+ name => Summit
6
+ ruby_type => Client
@@ -0,0 +1,4 @@
1
+ id => 1
2
+ type => Firm
3
+ name => 37signals
4
+ ruby_type => Firm
@@ -0,0 +1,6 @@
1
+ id => 3
2
+ type => Client
3
+ firm_id => 1
4
+ client_of => 1
5
+ name => Microsoft
6
+ ruby_type => Client
@@ -0,0 +1,37 @@
1
+ class Company < ActiveRecord::Base
2
+ attr_protected :rating
3
+ end
4
+
5
+
6
+ class Firm < Company
7
+ has_many :clients, :order => "id", :dependent => true
8
+ has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
9
+ has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
10
+ has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
11
+ has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
12
+
13
+ has_one :account, :dependent => true
14
+ end
15
+
16
+ class Client < Company
17
+ belongs_to :firm, :foreign_key => "client_of"
18
+ belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
19
+ belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
20
+ belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => "1 = 1"
21
+ end
22
+
23
+
24
+ class SpecialClient < Client
25
+ end
26
+
27
+ class VerySpecialClient < SpecialClient
28
+ end
29
+
30
+ class Account < ActiveRecord::Base
31
+ belongs_to :firm
32
+
33
+ protected
34
+ def validate
35
+ errors.add_on_empty "credit_limit"
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ module MyApplication
2
+ module Business
3
+ class Company < ActiveRecord::Base
4
+ attr_protected :rating
5
+ end
6
+
7
+ class Firm < Company
8
+ has_many :clients, :order => "id", :dependent => true
9
+ has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
10
+ has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
11
+ has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
12
+ has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
13
+
14
+ has_one :account, :dependent => true
15
+ end
16
+
17
+ class Client < Company
18
+ belongs_to :firm, :foreign_key => "client_of"
19
+ belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
20
+ end
21
+ end
22
+
23
+ module Billing
24
+ class Account < ActiveRecord::Base
25
+ belongs_to :firm, :class_name => "MyApplication::Business::Firm"
26
+
27
+ protected
28
+ def validate
29
+ errors.add_on_empty "credit_limit"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ class Course < ActiveRecord::Base
2
+ has_many :entrants
3
+ end
@@ -0,0 +1,2 @@
1
+ id => 2
2
+ name => Java Development
@@ -0,0 +1,2 @@
1
+ id => 1
2
+ name => Ruby Development
@@ -0,0 +1,30 @@
1
+ class Customer < ActiveRecord::Base
2
+ composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ]
3
+ composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
4
+ end
5
+
6
+ class Address
7
+ attr_reader :street, :city, :country
8
+
9
+ def initialize(street, city, country)
10
+ @street, @city, @country = street, city, country
11
+ end
12
+
13
+ def close_to?(other_address)
14
+ city == other_address.city && country == other_address.country
15
+ end
16
+ end
17
+
18
+ class Money
19
+ attr_reader :amount, :currency
20
+
21
+ EXCHANGE_RATES = { "USD_TO_DKK" => 6, "DKK_TO_USD" => 0.6 }
22
+
23
+ def initialize(amount, currency = "USD")
24
+ @amount, @currency = amount, currency
25
+ end
26
+
27
+ def exchange_to(other_currency)
28
+ Money.new((amount * EXCHANGE_RATES["#{currency}_TO_#{other_currency}"]).floor, other_currency)
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ id => 1
2
+ name => David
3
+ balance => 50
4
+ address_street => Funny Street
5
+ address_city => Scary Town
6
+ address_country => Loony Land
@@ -0,0 +1,96 @@
1
+ CREATE TABLE `accounts` (
2
+ `id` int(11) NOT NULL auto_increment,
3
+ `firm_id` int(11) default NULL,
4
+ `credit_limit` int(5) default NULL,
5
+ PRIMARY KEY (`id`)
6
+ ) TYPE=InnoDB;
7
+
8
+ CREATE TABLE `companies` (
9
+ `id` int(11) NOT NULL auto_increment,
10
+ `type` varchar(50) default NULL,
11
+ `ruby_type` varchar(50) default NULL,
12
+ `firm_id` int(11) default NULL,
13
+ `name` varchar(50) default NULL,
14
+ `client_of` int(11) default NULL,
15
+ `rating` int(11) default NULL default 1,
16
+ PRIMARY KEY (`id`)
17
+ ) TYPE=InnoDB;
18
+
19
+
20
+ CREATE TABLE `topics` (
21
+ `id` int(11) NOT NULL auto_increment,
22
+ `title` varchar(255) default NULL,
23
+ `author_name` varchar(255) default NULL,
24
+ `author_email_address` varchar(255) default NULL,
25
+ `written_on` datetime default NULL,
26
+ `last_read` date default NULL,
27
+ `content` text,
28
+ `approved` tinyint(1) default 1,
29
+ `replies_count` int(11) default 0,
30
+ `parent_id` int(11) default NULL,
31
+ `type` varchar(50) default NULL,
32
+ PRIMARY KEY (`id`)
33
+ ) TYPE=InnoDB;
34
+
35
+ CREATE TABLE `developers` (
36
+ `id` int(11) NOT NULL auto_increment,
37
+ `name` varchar(100) default NULL,
38
+ PRIMARY KEY (`id`)
39
+ );
40
+
41
+ CREATE TABLE `projects` (
42
+ `id` int(11) NOT NULL auto_increment,
43
+ `name` varchar(100) default NULL,
44
+ PRIMARY KEY (`id`)
45
+ );
46
+
47
+ CREATE TABLE `developers_projects` (
48
+ `developer_id` int(11) NOT NULL,
49
+ `project_id` int(11) NOT NULL
50
+ );
51
+
52
+ CREATE TABLE `customers` (
53
+ `id` int(11) NOT NULL auto_increment,
54
+ `name` varchar(100) default NULL,
55
+ `balance` int(6) default 0,
56
+ `address_street` varchar(100) default NULL,
57
+ `address_city` varchar(100) default NULL,
58
+ `address_country` varchar(100) default NULL,
59
+ PRIMARY KEY (`id`)
60
+ );
61
+
62
+ CREATE TABLE `movies` (
63
+ `movieid` int(11) NOT NULL auto_increment,
64
+ `name` varchar(100) default NULL,
65
+ PRIMARY KEY (`movieid`)
66
+ );
67
+
68
+ CREATE TABLE `subscribers` (
69
+ `nick` varchar(100) NOT NULL,
70
+ `name` varchar(100) default NULL,
71
+ PRIMARY KEY (`nick`)
72
+ );
73
+
74
+ CREATE TABLE `booleantests` (
75
+ `id` int(11) NOT NULL auto_increment,
76
+ `value` integer default NULL,
77
+ PRIMARY KEY (`id`)
78
+ );
79
+
80
+ CREATE TABLE `auto_id_tests` (
81
+ `auto_id` int(11) NOT NULL auto_increment,
82
+ `value` integer default NULL,
83
+ PRIMARY KEY (`auto_id`)
84
+ );
85
+
86
+ CREATE TABLE `entrants` (
87
+ `id` INTEGER NOT NULL PRIMARY KEY,
88
+ `name` VARCHAR(255) NOT NULL,
89
+ `course_id` INTEGER NOT NULL
90
+ );
91
+
92
+ CREATE TABLE `colnametests` (
93
+ `id` int(11) NOT NULL auto_increment,
94
+ `references` int(11) NOT NULL,
95
+ PRIMARY KEY (`id`)
96
+ );
@@ -0,0 +1,4 @@
1
+ CREATE TABLE `courses` (
2
+ `id` INTEGER NOT NULL PRIMARY KEY,
3
+ `name` VARCHAR(255) NOT NULL
4
+ );
@@ -0,0 +1,113 @@
1
+ SET search_path = public, pg_catalog;
2
+
3
+ CREATE TABLE accounts (
4
+ id serial,
5
+ firm_id integer,
6
+ credit_limit integer,
7
+ PRIMARY KEY (id)
8
+ );
9
+ SELECT setval('accounts_id_seq', 100);
10
+
11
+ CREATE TABLE companies (
12
+ id serial,
13
+ "type" character varying(50),
14
+ "ruby_type" character varying(50),
15
+ firm_id integer,
16
+ name character varying(50),
17
+ client_of integer,
18
+ rating integer default 1,
19
+ PRIMARY KEY (id)
20
+ );
21
+ SELECT setval('companies_id_seq', 100);
22
+
23
+ CREATE TABLE developers_projects (
24
+ developer_id integer NOT NULL,
25
+ project_id integer NOT NULL
26
+ );
27
+
28
+ CREATE TABLE developers (
29
+ id serial,
30
+ name character varying(100),
31
+ PRIMARY KEY (id)
32
+ );
33
+ SELECT setval('developers_id_seq', 100);
34
+
35
+ CREATE TABLE projects (
36
+ id serial,
37
+ name character varying(100),
38
+ PRIMARY KEY (id)
39
+ );
40
+ SELECT setval('projects_id_seq', 100);
41
+
42
+ CREATE TABLE topics (
43
+ id serial,
44
+ title character varying(255),
45
+ author_name character varying(255),
46
+ author_email_address character varying(255),
47
+ written_on timestamp without time zone,
48
+ last_read date,
49
+ content text,
50
+ replies_count integer default 0,
51
+ parent_id integer,
52
+ "type" character varying(50),
53
+ approved smallint DEFAULT 1,
54
+ PRIMARY KEY (id)
55
+ );
56
+ SELECT setval('topics_id_seq', 100);
57
+
58
+ CREATE TABLE customers (
59
+ id serial,
60
+ name character varying,
61
+ balance integer default 0,
62
+ address_street character varying,
63
+ address_city character varying,
64
+ address_country character varying,
65
+ PRIMARY KEY (id)
66
+ );
67
+ SELECT setval('customers_id_seq', 100);
68
+
69
+ CREATE TABLE movies (
70
+ movieid serial,
71
+ name text,
72
+ PRIMARY KEY (movieid)
73
+ );
74
+
75
+ CREATE TABLE subscribers (
76
+ nick text NOT NULL,
77
+ name text,
78
+ PRIMARY KEY (nick)
79
+ );
80
+
81
+ CREATE TABLE booleantests (
82
+ id serial,
83
+ value boolean,
84
+ PRIMARY KEY (id)
85
+ );
86
+
87
+ CREATE TABLE defaults (
88
+ id serial,
89
+ modified_date date default CURRENT_DATE,
90
+ fixed_date date default '2004-01-01',
91
+ modified_time timestamp default CURRENT_TIMESTAMP,
92
+ fixed_time timestamp default '2004-01-01 00:00:00.000000-00',
93
+ char1 char(1) default 'Y',
94
+ char2 character varying(50) default 'a varchar field',
95
+ char3 text default 'a text field'
96
+ );
97
+
98
+ CREATE TABLE auto_id_tests (
99
+ auto_id serial,
100
+ value integer,
101
+ PRIMARY KEY (auto_id)
102
+ );
103
+
104
+ CREATE TABLE entrants (
105
+ id serial,
106
+ name text,
107
+ course_id integer
108
+ );
109
+
110
+ CREATE TABLE colnametests (
111
+ id serial,
112
+ "references" integer NOT NULL
113
+ );
@@ -0,0 +1,4 @@
1
+ CREATE TABLE courses (
2
+ id serial,
3
+ name text
4
+ );
@@ -0,0 +1,85 @@
1
+ CREATE TABLE 'accounts' (
2
+ 'id' INTEGER PRIMARY KEY NOT NULL,
3
+ 'firm_id' INTEGER DEFAULT NULL,
4
+ 'credit_limit' INTEGER DEFAULT NULL
5
+ );
6
+
7
+ CREATE TABLE 'companies' (
8
+ 'id' INTEGER PRIMARY KEY NOT NULL,
9
+ 'type' VARCHAR(255) DEFAULT NULL,
10
+ 'ruby_type' VARCHAR(255) DEFAULT NULL,
11
+ 'firm_id' INTEGER DEFAULT NULL,
12
+ 'name' TEXT DEFAULT NULL,
13
+ 'client_of' INTEGER DEFAULT NULL,
14
+ 'rating' INTEGER DEFAULT 1
15
+ );
16
+
17
+
18
+ CREATE TABLE 'topics' (
19
+ 'id' INTEGER PRIMARY KEY NOT NULL,
20
+ 'title' VARCHAR(255) DEFAULT NULL,
21
+ 'author_name' VARCHAR(255) DEFAULT NULL,
22
+ 'author_email_address' VARCHAR(255) DEFAULT NULL,
23
+ 'written_on' DATETIME DEFAULT NULL,
24
+ 'last_read' DATE DEFAULT NULL,
25
+ 'content' TEXT,
26
+ 'approved' INTEGER DEFAULT 1,
27
+ 'replies_count' INTEGER DEFAULT 0,
28
+ 'parent_id' INTEGER DEFAULT NULL,
29
+ 'type' VARCHAR(255) DEFAULT NULL
30
+ );
31
+
32
+ CREATE TABLE 'developers' (
33
+ 'id' INTEGER PRIMARY KEY NOT NULL,
34
+ 'name' TEXT DEFAULT NULL
35
+ );
36
+
37
+ CREATE TABLE 'projects' (
38
+ 'id' INTEGER PRIMARY KEY NOT NULL,
39
+ 'name' TEXT DEFAULT NULL
40
+ );
41
+
42
+ CREATE TABLE 'developers_projects' (
43
+ 'developer_id' INTEGER NOT NULL,
44
+ 'project_id' INTEGER NOT NULL
45
+ );
46
+
47
+ CREATE TABLE 'customers' (
48
+ 'id' INTEGER PRIMARY KEY NOT NULL,
49
+ 'name' VARCHAR(255) DEFAULT NULL,
50
+ 'balance' INTEGER DEFAULT 0,
51
+ 'address_street' TEXT DEFAULT NULL,
52
+ 'address_city' TEXT DEFAULT NULL,
53
+ 'address_country' TEXT DEFAULT NULL
54
+ );
55
+
56
+ CREATE TABLE 'movies' (
57
+ 'movieid' INTEGER PRIMARY KEY NOT NULL,
58
+ 'name' VARCHAR(255) DEFAULT NULL
59
+ );
60
+
61
+ CREATE TABLE subscribers (
62
+ 'nick' VARCHAR(255) PRIMARY KEY NOT NULL,
63
+ 'name' VARCHAR(255) DEFAULT NULL
64
+ );
65
+
66
+ CREATE TABLE 'booleantests' (
67
+ 'id' INTEGER PRIMARY KEY NOT NULL,
68
+ 'value' INTEGER DEFAULT NULL
69
+ );
70
+
71
+ CREATE TABLE 'auto_id_tests' (
72
+ 'auto_id' INTEGER PRIMARY KEY NOT NULL,
73
+ 'value' INTEGER DEFAULT NULL
74
+ );
75
+
76
+ CREATE TABLE 'entrants' (
77
+ 'id' INTEGER NOT NULL PRIMARY KEY,
78
+ 'name' VARCHAR(255) NOT NULL,
79
+ 'course_id' INTEGER NOT NULL
80
+ );
81
+
82
+ CREATE TABLE 'colnametests' (
83
+ 'id' INTEGER NOT NULL PRIMARY KEY,
84
+ 'references' INTEGER NOT NULL
85
+ );