activerecord 1.0.0 → 4.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +2102 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +213 -0
- data/examples/performance.rb +172 -0
- data/examples/simple.rb +14 -0
- data/lib/active_record/aggregations.rb +180 -84
- data/lib/active_record/associations/alias_tracker.rb +76 -0
- data/lib/active_record/associations/association.rb +248 -0
- data/lib/active_record/associations/association_scope.rb +135 -0
- data/lib/active_record/associations/belongs_to_association.rb +92 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +35 -0
- data/lib/active_record/associations/builder/association.rb +108 -0
- data/lib/active_record/associations/builder/belongs_to.rb +98 -0
- data/lib/active_record/associations/builder/collection_association.rb +89 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
- data/lib/active_record/associations/builder/has_many.rb +15 -0
- data/lib/active_record/associations/builder/has_one.rb +25 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +608 -0
- data/lib/active_record/associations/collection_proxy.rb +986 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +58 -39
- data/lib/active_record/associations/has_many_association.rb +116 -85
- data/lib/active_record/associations/has_many_through_association.rb +197 -0
- data/lib/active_record/associations/has_one_association.rb +102 -0
- data/lib/active_record/associations/has_one_through_association.rb +36 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_dependency.rb +235 -0
- data/lib/active_record/associations/join_helper.rb +45 -0
- data/lib/active_record/associations/preloader/association.rb +121 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +63 -0
- data/lib/active_record/associations/preloader.rb +178 -0
- data/lib/active_record/associations/singular_association.rb +64 -0
- data/lib/active_record/associations/through_association.rb +87 -0
- data/lib/active_record/associations.rb +1437 -431
- data/lib/active_record/attribute_assignment.rb +201 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +70 -0
- data/lib/active_record/attribute_methods/dirty.rb +118 -0
- data/lib/active_record/attribute_methods/primary_key.rb +122 -0
- data/lib/active_record/attribute_methods/query.rb +40 -0
- data/lib/active_record/attribute_methods/read.rb +107 -0
- data/lib/active_record/attribute_methods/serialization.rb +162 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +59 -0
- data/lib/active_record/attribute_methods/write.rb +63 -0
- data/lib/active_record/attribute_methods.rb +393 -0
- data/lib/active_record/autosave_association.rb +426 -0
- data/lib/active_record/base.rb +268 -930
- data/lib/active_record/callbacks.rb +203 -230
- data/lib/active_record/coders/yaml_column.rb +38 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +638 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +67 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +390 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +129 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +501 -0
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +873 -0
- data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +389 -275
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
- data/lib/active_record/connection_adapters/column.rb +318 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +517 -90
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +911 -138
- data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +624 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +463 -0
- data/lib/active_record/counter_cache.rb +122 -0
- data/lib/active_record/dynamic_matchers.rb +131 -0
- data/lib/active_record/errors.rb +213 -0
- data/lib/active_record/explain.rb +38 -0
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +29 -0
- data/lib/active_record/fixture_set/file.rb +55 -0
- data/lib/active_record/fixtures.rb +892 -138
- data/lib/active_record/inheritance.rb +200 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locale/en.yml +47 -0
- data/lib/active_record/locking/optimistic.rb +181 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/log_subscriber.rb +82 -0
- data/lib/active_record/migration/command_recorder.rb +164 -0
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +1015 -0
- data/lib/active_record/model_schema.rb +345 -0
- data/lib/active_record/nested_attributes.rb +546 -0
- data/lib/active_record/null_relation.rb +65 -0
- data/lib/active_record/persistence.rb +509 -0
- data/lib/active_record/query_cache.rb +56 -0
- data/lib/active_record/querying.rb +62 -0
- data/lib/active_record/railtie.rb +205 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -0
- data/lib/active_record/railties/controller_runtime.rb +50 -0
- data/lib/active_record/railties/databases.rake +402 -0
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +30 -0
- data/lib/active_record/reflection.rb +544 -87
- data/lib/active_record/relation/batches.rb +93 -0
- data/lib/active_record/relation/calculations.rb +399 -0
- data/lib/active_record/relation/delegation.rb +125 -0
- data/lib/active_record/relation/finder_methods.rb +349 -0
- data/lib/active_record/relation/merger.rb +161 -0
- data/lib/active_record/relation/predicate_builder.rb +106 -0
- data/lib/active_record/relation/query_methods.rb +1044 -0
- data/lib/active_record/relation/spawn_methods.rb +73 -0
- data/lib/active_record/relation.rb +655 -0
- data/lib/active_record/result.rb +67 -0
- data/lib/active_record/runtime_registry.rb +17 -0
- data/lib/active_record/sanitization.rb +168 -0
- data/lib/active_record/schema.rb +65 -0
- data/lib/active_record/schema_dumper.rb +204 -0
- data/lib/active_record/schema_migration.rb +39 -0
- data/lib/active_record/scoping/default.rb +146 -0
- data/lib/active_record/scoping/named.rb +175 -0
- data/lib/active_record/scoping.rb +82 -0
- data/lib/active_record/serialization.rb +22 -0
- data/lib/active_record/serializers/xml_serializer.rb +197 -0
- data/lib/active_record/statement_cache.rb +26 -0
- data/lib/active_record/store.rb +156 -0
- data/lib/active_record/tasks/database_tasks.rb +203 -0
- data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
- data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
- data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
- data/lib/active_record/test_case.rb +96 -0
- data/lib/active_record/timestamp.rb +119 -0
- data/lib/active_record/transactions.rb +366 -69
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +49 -0
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +225 -0
- data/lib/active_record/validations.rb +64 -185
- data/lib/active_record/version.rb +11 -0
- data/lib/active_record.rb +149 -24
- data/lib/rails/generators/active_record/migration/migration_generator.rb +62 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +39 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +10 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
- data/lib/rails/generators/active_record.rb +23 -0
- metadata +261 -161
- data/CHANGELOG +0 -581
- data/README +0 -361
- data/RUNNING_UNIT_TESTS +0 -36
- data/dev-utils/eval_debugger.rb +0 -9
- data/examples/associations.png +0 -0
- data/examples/associations.rb +0 -87
- data/examples/shared_setup.rb +0 -15
- data/examples/validation.rb +0 -88
- data/install.rb +0 -60
- data/lib/active_record/associations/association_collection.rb +0 -70
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -107
- data/lib/active_record/deprecated_associations.rb +0 -70
- data/lib/active_record/observer.rb +0 -71
- data/lib/active_record/support/class_attribute_accessors.rb +0 -43
- data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
- data/lib/active_record/support/clean_logger.rb +0 -10
- data/lib/active_record/support/inflector.rb +0 -70
- data/lib/active_record/vendor/mysql.rb +0 -1117
- data/lib/active_record/vendor/simple.rb +0 -702
- data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
- data/lib/active_record/wrappings.rb +0 -59
- data/rakefile +0 -122
- data/test/abstract_unit.rb +0 -16
- data/test/aggregations_test.rb +0 -34
- data/test/all.sh +0 -8
- data/test/associations_test.rb +0 -477
- data/test/base_test.rb +0 -513
- data/test/class_inheritable_attributes_test.rb +0 -33
- data/test/connections/native_mysql/connection.rb +0 -24
- data/test/connections/native_postgresql/connection.rb +0 -24
- data/test/connections/native_sqlite/connection.rb +0 -24
- data/test/deprecated_associations_test.rb +0 -336
- data/test/finder_test.rb +0 -67
- data/test/fixtures/accounts/signals37 +0 -3
- data/test/fixtures/accounts/unknown +0 -2
- data/test/fixtures/auto_id.rb +0 -4
- data/test/fixtures/column_name.rb +0 -3
- data/test/fixtures/companies/first_client +0 -6
- data/test/fixtures/companies/first_firm +0 -4
- data/test/fixtures/companies/second_client +0 -6
- data/test/fixtures/company.rb +0 -37
- data/test/fixtures/company_in_module.rb +0 -33
- data/test/fixtures/course.rb +0 -3
- data/test/fixtures/courses/java +0 -2
- data/test/fixtures/courses/ruby +0 -2
- data/test/fixtures/customer.rb +0 -30
- data/test/fixtures/customers/david +0 -6
- data/test/fixtures/db_definitions/mysql.sql +0 -96
- data/test/fixtures/db_definitions/mysql2.sql +0 -4
- data/test/fixtures/db_definitions/postgresql.sql +0 -113
- data/test/fixtures/db_definitions/postgresql2.sql +0 -4
- data/test/fixtures/db_definitions/sqlite.sql +0 -85
- data/test/fixtures/db_definitions/sqlite2.sql +0 -4
- data/test/fixtures/default.rb +0 -2
- data/test/fixtures/developer.rb +0 -8
- data/test/fixtures/developers/david +0 -2
- data/test/fixtures/developers/jamis +0 -2
- data/test/fixtures/developers_projects/david_action_controller +0 -2
- data/test/fixtures/developers_projects/david_active_record +0 -2
- data/test/fixtures/developers_projects/jamis_active_record +0 -2
- data/test/fixtures/entrant.rb +0 -3
- data/test/fixtures/entrants/first +0 -3
- data/test/fixtures/entrants/second +0 -3
- data/test/fixtures/entrants/third +0 -3
- data/test/fixtures/fixture_database.sqlite +0 -0
- data/test/fixtures/fixture_database_2.sqlite +0 -0
- data/test/fixtures/movie.rb +0 -5
- data/test/fixtures/movies/first +0 -2
- data/test/fixtures/movies/second +0 -2
- data/test/fixtures/project.rb +0 -3
- data/test/fixtures/projects/action_controller +0 -2
- data/test/fixtures/projects/active_record +0 -2
- data/test/fixtures/reply.rb +0 -21
- data/test/fixtures/subscriber.rb +0 -5
- data/test/fixtures/subscribers/first +0 -2
- data/test/fixtures/subscribers/second +0 -2
- data/test/fixtures/topic.rb +0 -20
- data/test/fixtures/topics/first +0 -9
- data/test/fixtures/topics/second +0 -8
- data/test/fixtures_test.rb +0 -20
- data/test/inflector_test.rb +0 -104
- data/test/inheritance_test.rb +0 -125
- data/test/lifecycle_test.rb +0 -110
- data/test/modules_test.rb +0 -21
- data/test/multiple_db_test.rb +0 -46
- data/test/pk_test.rb +0 -57
- data/test/reflection_test.rb +0 -78
- data/test/thread_safety_test.rb +0 -33
- data/test/transactions_test.rb +0 -83
- data/test/unconnected_test.rb +0 -24
- data/test/validations_test.rb +0 -126
@@ -1,336 +0,0 @@
|
|
1
|
-
require 'abstract_unit'
|
2
|
-
require 'fixtures/developer'
|
3
|
-
require 'fixtures/project'
|
4
|
-
require 'fixtures/company'
|
5
|
-
require 'fixtures/topic'
|
6
|
-
# require File.dirname(__FILE__) + '/../dev-utils/eval_debugger'
|
7
|
-
require 'fixtures/reply'
|
8
|
-
|
9
|
-
# Can't declare new classes in test case methods, so tests before that
|
10
|
-
bad_collection_keys = false
|
11
|
-
begin
|
12
|
-
class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
|
13
|
-
rescue ActiveRecord::ActiveRecordError
|
14
|
-
bad_collection_keys = true
|
15
|
-
end
|
16
|
-
raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys
|
17
|
-
|
18
|
-
|
19
|
-
class DeprecatedAssociationsTest < Test::Unit::TestCase
|
20
|
-
def setup
|
21
|
-
create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
|
22
|
-
@signals37 = Firm.find(1)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_has_many_find
|
26
|
-
assert_equal 2, Firm.find_first.clients.length
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_has_many_orders
|
30
|
-
assert_equal "Summit", Firm.find_first.clients.first.name
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_has_many_class_name
|
34
|
-
assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_has_many_foreign_key
|
38
|
-
assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_has_many_conditions
|
42
|
-
assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_has_many_sql
|
46
|
-
firm = Firm.find_first
|
47
|
-
assert_equal "Microsoft", firm.clients_using_sql.first.name
|
48
|
-
assert_equal 1, firm.clients_using_sql_count
|
49
|
-
assert_equal 1, Firm.find_first.clients_using_sql_count
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_has_many_queries
|
53
|
-
assert Firm.find_first.has_clients?
|
54
|
-
firm = Firm.find_first
|
55
|
-
assert_equal 2, firm.clients_count # tests using class count
|
56
|
-
firm.clients
|
57
|
-
assert firm.has_clients?
|
58
|
-
assert_equal 2, firm.clients_count # tests using collection length
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_has_many_dependence
|
62
|
-
assert_equal 2, Client.find_all.length
|
63
|
-
Firm.find_first.destroy
|
64
|
-
assert_equal 0, Client.find_all.length
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_has_many_dependence_with_transaction_support_on_failure
|
68
|
-
assert_equal 2, Client.find_all.length
|
69
|
-
|
70
|
-
firm = Firm.find_first
|
71
|
-
clients = firm.clients
|
72
|
-
clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
|
73
|
-
|
74
|
-
firm.destroy rescue "do nothing"
|
75
|
-
|
76
|
-
assert_equal 2, Client.find_all.length
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_has_one_dependence
|
80
|
-
firm = Firm.find(1)
|
81
|
-
assert firm.has_account?
|
82
|
-
firm.destroy
|
83
|
-
assert_equal 1, Account.find_all.length
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_has_one_dependence_with_missing_association
|
87
|
-
Account.destroy_all
|
88
|
-
firm = Firm.find(1)
|
89
|
-
assert !firm.has_account?
|
90
|
-
firm.destroy
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_belongs_to
|
94
|
-
assert_equal @signals37.name, Client.find(3).firm.name
|
95
|
-
assert Client.find(3).has_firm?, "Microsoft should have a firm"
|
96
|
-
# assert !Company.find(1).has_firm?, "37signals shouldn't have a firm"
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_belongs_to_with_different_class_name
|
100
|
-
assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
|
101
|
-
assert Company.find(3).has_firm_with_other_name?, "Microsoft should have a firm"
|
102
|
-
assert !Company.find(1).has_firm_with_other_name?, "37signals shouldn't have a firm"
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_belongs_to_with_condition
|
106
|
-
assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
|
107
|
-
assert Company.find(3).has_firm_with_condition?, "Microsoft should have a firm"
|
108
|
-
assert !Company.find(1).has_firm_with_condition?, "37signals shouldn't have a firm"
|
109
|
-
end
|
110
|
-
|
111
|
-
|
112
|
-
def test_belongs_to_equality
|
113
|
-
assert Company.find(3).firm?(Company.find(1)), "Microsoft should have 37signals as firm"
|
114
|
-
assert_raises(RuntimeError) { !Company.find(3).firm?(Company.find(3)) } # "Summit shouldn't have itself as firm"
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_has_one
|
118
|
-
assert @signals37.account?(Account.find(1))
|
119
|
-
assert_equal Account.find(1).credit_limit, @signals37.account.credit_limit
|
120
|
-
assert @signals37.has_account?, "37signals should have an account"
|
121
|
-
assert Account.find(1).firm?(@signals37), "37signals account should be able to backtrack"
|
122
|
-
assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
|
123
|
-
|
124
|
-
assert !Account.find(2).has_firm?, "Unknown isn't linked"
|
125
|
-
assert !Account.find(2).firm?(@signals37), "Unknown isn't linked"
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_has_many_dependence_on_account
|
129
|
-
assert_equal 2, Account.find_all.length
|
130
|
-
@signals37.destroy
|
131
|
-
assert_equal 1, Account.find_all.length
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_find_in
|
135
|
-
assert_equal Client.find(2).name, @signals37.find_in_clients(2).name
|
136
|
-
assert_raises(ActiveRecord::RecordNotFound) { @signals37.find_in_clients(6) }
|
137
|
-
end
|
138
|
-
|
139
|
-
def test_force_reload
|
140
|
-
firm = Firm.new
|
141
|
-
firm.save
|
142
|
-
firm.clients.each {|c|} # forcing to load all clients
|
143
|
-
assert firm.clients.empty?, "New firm shouldn't have client objects"
|
144
|
-
assert !firm.has_clients?, "New firm shouldn't have clients"
|
145
|
-
assert_equal 0, firm.clients_count, "New firm should have 0 clients"
|
146
|
-
|
147
|
-
client = Client.new("firm_id" => firm.id)
|
148
|
-
client.save
|
149
|
-
|
150
|
-
assert firm.clients.empty?, "New firm should have cached no client objects"
|
151
|
-
assert !firm.has_clients?, "New firm should have cached a no-clients response"
|
152
|
-
assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count"
|
153
|
-
|
154
|
-
assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
|
155
|
-
assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
|
156
|
-
assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_included_in_collection
|
160
|
-
assert @signals37.clients.include?(Client.find(2))
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_build_to_collection
|
164
|
-
assert_equal 1, @signals37.clients_of_firm_count
|
165
|
-
new_client = @signals37.build_to_clients_of_firm("name" => "Another Client")
|
166
|
-
assert_equal "Another Client", new_client.name
|
167
|
-
assert new_client.save
|
168
|
-
|
169
|
-
assert new_client.firm?(@signals37)
|
170
|
-
assert_equal 2, @signals37.clients_of_firm_count(true)
|
171
|
-
end
|
172
|
-
|
173
|
-
def test_create_in_collection
|
174
|
-
assert_equal @signals37.create_in_clients_of_firm("name" => "Another Client"), @signals37.clients_of_firm(true).last
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_succesful_build_association
|
178
|
-
firm = Firm.new("name" => "GlobalMegaCorp")
|
179
|
-
firm.save
|
180
|
-
|
181
|
-
account = firm.build_account("credit_limit" => 1000)
|
182
|
-
assert account.save
|
183
|
-
assert_equal account, firm.account
|
184
|
-
end
|
185
|
-
|
186
|
-
def test_failing_build_association
|
187
|
-
firm = Firm.new("name" => "GlobalMegaCorp")
|
188
|
-
firm.save
|
189
|
-
|
190
|
-
account = firm.build_account
|
191
|
-
assert !account.save
|
192
|
-
assert_equal "can't be empty", account.errors.on("credit_limit")
|
193
|
-
end
|
194
|
-
|
195
|
-
def test_create_association
|
196
|
-
firm = Firm.new("name" => "GlobalMegaCorp")
|
197
|
-
firm.save
|
198
|
-
assert_equal firm.create_account("credit_limit" => 1000), firm.account
|
199
|
-
end
|
200
|
-
|
201
|
-
def test_has_and_belongs_to_many
|
202
|
-
david = Developer.find(1)
|
203
|
-
assert david.has_projects?
|
204
|
-
assert_equal 2, david.projects_count
|
205
|
-
|
206
|
-
active_record = Project.find(1)
|
207
|
-
assert active_record.has_developers?
|
208
|
-
assert_equal 2, active_record.developers_count
|
209
|
-
assert_equal david.name, active_record.developers.first.name
|
210
|
-
end
|
211
|
-
|
212
|
-
def test_has_and_belongs_to_many_removing
|
213
|
-
david = Developer.find(1)
|
214
|
-
active_record = Project.find(1)
|
215
|
-
|
216
|
-
david.remove_projects(active_record)
|
217
|
-
|
218
|
-
assert_equal 1, david.projects_count
|
219
|
-
assert_equal 1, active_record.developers_count
|
220
|
-
end
|
221
|
-
|
222
|
-
def test_has_and_belongs_to_many_zero
|
223
|
-
david = Developer.find(1)
|
224
|
-
david.remove_projects(Project.find_all)
|
225
|
-
|
226
|
-
assert_equal 0, david.projects_count
|
227
|
-
assert !david.has_projects?
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_has_and_belongs_to_many_adding
|
231
|
-
jamis = Developer.find(2)
|
232
|
-
action_controller = Project.find(2)
|
233
|
-
|
234
|
-
jamis.add_projects(action_controller)
|
235
|
-
|
236
|
-
assert_equal 2, jamis.projects_count
|
237
|
-
assert_equal 2, action_controller.developers_count
|
238
|
-
end
|
239
|
-
|
240
|
-
def test_has_and_belongs_to_many_adding_from_the_project
|
241
|
-
jamis = Developer.find(2)
|
242
|
-
action_controller = Project.find(2)
|
243
|
-
|
244
|
-
action_controller.add_developers(jamis)
|
245
|
-
|
246
|
-
assert_equal 2, jamis.projects_count
|
247
|
-
assert_equal 2, action_controller.developers_count
|
248
|
-
end
|
249
|
-
|
250
|
-
def test_has_and_belongs_to_many_adding_a_collection
|
251
|
-
aridridel = Developer.new("name" => "Aridridel")
|
252
|
-
aridridel.save
|
253
|
-
|
254
|
-
aridridel.add_projects([ Project.find(1), Project.find(2) ])
|
255
|
-
assert_equal 2, aridridel.projects_count
|
256
|
-
end
|
257
|
-
|
258
|
-
def test_belongs_to_counter
|
259
|
-
topic = Topic.create("title" => "Apple", "content" => "hello world")
|
260
|
-
assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet"
|
261
|
-
|
262
|
-
reply = topic.create_in_replies("title" => "I'm saying no!", "content" => "over here")
|
263
|
-
assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply created"
|
264
|
-
|
265
|
-
reply.destroy
|
266
|
-
assert_equal 0, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply deleted"
|
267
|
-
end
|
268
|
-
|
269
|
-
def test_natural_assignment_of_has_one
|
270
|
-
apple = Firm.create("name" => "Apple")
|
271
|
-
citibank = Account.create("credit_limit" => 10)
|
272
|
-
apple.account = citibank
|
273
|
-
assert_equal apple.id, citibank.firm_id
|
274
|
-
end
|
275
|
-
|
276
|
-
def test_natural_assignment_of_belongs_to
|
277
|
-
apple = Firm.create("name" => "Apple")
|
278
|
-
citibank = Account.create("credit_limit" => 10)
|
279
|
-
citibank.firm = apple
|
280
|
-
assert_equal apple.id, citibank.firm_id
|
281
|
-
end
|
282
|
-
|
283
|
-
def test_natural_assignment_of_has_many
|
284
|
-
apple = Firm.create("name" => "Apple")
|
285
|
-
natural = Client.new("name" => "Natural Company")
|
286
|
-
apple.clients << natural
|
287
|
-
assert_equal apple.id, natural.firm_id
|
288
|
-
assert_equal Client.find(natural.id), Firm.find(apple.id).clients.find { |c| c.id == natural.id }
|
289
|
-
apple.clients.delete natural
|
290
|
-
assert_nil Firm.find(apple.id).clients.find { |c| c.id == natural.id }
|
291
|
-
end
|
292
|
-
|
293
|
-
|
294
|
-
def test_natural_adding_of_has_and_belongs_to_many
|
295
|
-
rails = Project.create("name" => "Rails")
|
296
|
-
ap = Project.create("name" => "Action Pack")
|
297
|
-
john = Developer.create("name" => "John")
|
298
|
-
mike = Developer.create("name" => "Mike")
|
299
|
-
rails.developers << john
|
300
|
-
rails.developers << mike
|
301
|
-
|
302
|
-
assert_equal Developer.find(john.id), Project.find(rails.id).developers.find { |d| d.id == john.id }
|
303
|
-
assert_equal Developer.find(mike.id), Project.find(rails.id).developers.find { |d| d.id == mike.id }
|
304
|
-
assert_equal Project.find(rails.id), Developer.find(mike.id).projects.find { |p| p.id == rails.id }
|
305
|
-
assert_equal Project.find(rails.id), Developer.find(john.id).projects.find { |p| p.id == rails.id }
|
306
|
-
ap.developers << john
|
307
|
-
assert_equal Developer.find(john.id), Project.find(ap.id).developers.find { |d| d.id == john.id }
|
308
|
-
assert_equal Project.find(ap.id), Developer.find(john.id).projects.find { |p| p.id == ap.id }
|
309
|
-
|
310
|
-
ap.developers.delete john
|
311
|
-
assert_nil Project.find(ap.id).developers.find { |d| d.id == john.id }
|
312
|
-
assert_nil Developer.find(john.id).projects.find { |p| p.id == ap.id }
|
313
|
-
end
|
314
|
-
|
315
|
-
def test_storing_in_pstore
|
316
|
-
require "pstore"
|
317
|
-
apple = Firm.create("name" => "Apple")
|
318
|
-
natural = Client.new("name" => "Natural Company")
|
319
|
-
apple.clients << natural
|
320
|
-
|
321
|
-
db = PStore.new("/tmp/ar-pstore-association-test")
|
322
|
-
db.transaction do
|
323
|
-
db["apple"] = apple
|
324
|
-
end
|
325
|
-
|
326
|
-
db = PStore.new("/tmp/ar-pstore-association-test")
|
327
|
-
db.transaction do
|
328
|
-
assert_equal "Natural Company", db["apple"].clients.first.name
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
def test_has_many_find_all
|
333
|
-
assert_equal 2, Firm.find_first.find_all_in_clients("type = 'Client'").length
|
334
|
-
assert_equal 1, Firm.find_first.find_all_in_clients("name = 'Summit'").length
|
335
|
-
end
|
336
|
-
end
|
data/test/finder_test.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'abstract_unit'
|
2
|
-
require 'fixtures/company'
|
3
|
-
require 'fixtures/topic'
|
4
|
-
|
5
|
-
class FinderTest < Test::Unit::TestCase
|
6
|
-
def setup
|
7
|
-
@company_fixtures = create_fixtures("companies")
|
8
|
-
@topic_fixtures = create_fixtures("topics")
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_find
|
12
|
-
assert_equal(@topic_fixtures["first"]["title"], Topic.find(1).title)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_find_by_ids
|
16
|
-
assert_equal(2, Topic.find(1, 2).length)
|
17
|
-
assert_equal(@topic_fixtures["second"]["title"], Topic.find([ 2 ]).title)
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_find_by_ids_missing_one
|
21
|
-
assert_raises(ActiveRecord::RecordNotFound) {
|
22
|
-
Topic.find(1, 2, 45)
|
23
|
-
}
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_find_with_entire_select_statement
|
27
|
-
topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
|
28
|
-
|
29
|
-
assert_equal(1, topics.size)
|
30
|
-
assert_equal(@topic_fixtures["second"]["title"], topics.first.title)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_find_first
|
34
|
-
first = Topic.find_first "title = 'The First Topic'"
|
35
|
-
assert_equal(@topic_fixtures["first"]["title"], first.title)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_find_first_failing
|
39
|
-
first = Topic.find_first "title = 'The First Topic!'"
|
40
|
-
assert_nil(first)
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_unexisting_record_exception_handling
|
44
|
-
assert_raises(ActiveRecord::RecordNotFound) {
|
45
|
-
Topic.find(1).parent
|
46
|
-
}
|
47
|
-
|
48
|
-
Topic.find(2).parent
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_find_on_conditions
|
52
|
-
assert Topic.find_on_conditions(1, "approved = 0")
|
53
|
-
assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, "approved = 1") }
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_condition_interpolation
|
57
|
-
assert_kind_of Firm, Company.find_first(["name = '%s'", "37signals"])
|
58
|
-
assert_nil Company.find_first(["name = '%s'", "37signals!"])
|
59
|
-
assert_nil Company.find_first(["name = '%s'", "37signals!' OR 1=1"])
|
60
|
-
assert_kind_of Time, Topic.find_first(["id = %d", 1]).written_on
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_string_sanitation
|
64
|
-
assert_equal "something '' 1=1", ActiveRecord::Base.sanitize("something ' 1=1")
|
65
|
-
assert_equal "something select table", ActiveRecord::Base.sanitize("something; select table")
|
66
|
-
end
|
67
|
-
end
|
data/test/fixtures/auto_id.rb
DELETED
data/test/fixtures/company.rb
DELETED
@@ -1,37 +0,0 @@
|
|
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
|
@@ -1,33 +0,0 @@
|
|
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
|
data/test/fixtures/course.rb
DELETED
data/test/fixtures/courses/java
DELETED
data/test/fixtures/courses/ruby
DELETED
data/test/fixtures/customer.rb
DELETED
@@ -1,30 +0,0 @@
|
|
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
|
@@ -1,96 +0,0 @@
|
|
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
|
-
);
|