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
@@ -0,0 +1,345 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ModelSchema
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
##
|
7
|
+
# :singleton-method:
|
8
|
+
# Accessor for the prefix type that will be prepended to every primary key column name.
|
9
|
+
# The options are :table_name and :table_name_with_underscore. If the first is specified,
|
10
|
+
# the Product class will look for "productid" instead of "id" as the primary column. If the
|
11
|
+
# latter is specified, the Product class will look for "product_id" instead of "id". Remember
|
12
|
+
# that this is a global setting for all Active Records.
|
13
|
+
mattr_accessor :primary_key_prefix_type, instance_writer: false
|
14
|
+
|
15
|
+
##
|
16
|
+
# :singleton-method:
|
17
|
+
# Accessor for the name of the prefix string to prepend to every table name. So if set
|
18
|
+
# to "basecamp_", all table names will be named like "basecamp_projects", "basecamp_people",
|
19
|
+
# etc. This is a convenient way of creating a namespace for tables in a shared database.
|
20
|
+
# By default, the prefix is the empty string.
|
21
|
+
#
|
22
|
+
# If you are organising your models within modules you can add a prefix to the models within
|
23
|
+
# a namespace by defining a singleton method in the parent module called table_name_prefix which
|
24
|
+
# returns your chosen prefix.
|
25
|
+
class_attribute :table_name_prefix, instance_writer: false
|
26
|
+
self.table_name_prefix = ""
|
27
|
+
|
28
|
+
##
|
29
|
+
# :singleton-method:
|
30
|
+
# Works like +table_name_prefix+, but appends instead of prepends (set to "_basecamp" gives "projects_basecamp",
|
31
|
+
# "people_basecamp"). By default, the suffix is the empty string.
|
32
|
+
class_attribute :table_name_suffix, instance_writer: false
|
33
|
+
self.table_name_suffix = ""
|
34
|
+
|
35
|
+
##
|
36
|
+
# :singleton-method:
|
37
|
+
# Indicates whether table names should be the pluralized versions of the corresponding class names.
|
38
|
+
# If true, the default table name for a Product class will be +products+. If false, it would just be +product+.
|
39
|
+
# See table_name for the full rules on table/class naming. This is true, by default.
|
40
|
+
class_attribute :pluralize_table_names, instance_writer: false
|
41
|
+
self.pluralize_table_names = true
|
42
|
+
|
43
|
+
self.inheritance_column = 'type'
|
44
|
+
end
|
45
|
+
|
46
|
+
module ClassMethods
|
47
|
+
# Guesses the table name (in forced lower-case) based on the name of the class in the
|
48
|
+
# inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy
|
49
|
+
# looks like: Reply < Message < ActiveRecord::Base, then Message is used
|
50
|
+
# to guess the table name even when called on Reply. The rules used to do the guess
|
51
|
+
# are handled by the Inflector class in Active Support, which knows almost all common
|
52
|
+
# English inflections. You can add new inflections in config/initializers/inflections.rb.
|
53
|
+
#
|
54
|
+
# Nested classes are given table names prefixed by the singular form of
|
55
|
+
# the parent's table name. Enclosing modules are not considered.
|
56
|
+
#
|
57
|
+
# ==== Examples
|
58
|
+
#
|
59
|
+
# class Invoice < ActiveRecord::Base
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# file class table_name
|
63
|
+
# invoice.rb Invoice invoices
|
64
|
+
#
|
65
|
+
# class Invoice < ActiveRecord::Base
|
66
|
+
# class Lineitem < ActiveRecord::Base
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# file class table_name
|
71
|
+
# invoice.rb Invoice::Lineitem invoice_lineitems
|
72
|
+
#
|
73
|
+
# module Invoice
|
74
|
+
# class Lineitem < ActiveRecord::Base
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# file class table_name
|
79
|
+
# invoice/lineitem.rb Invoice::Lineitem lineitems
|
80
|
+
#
|
81
|
+
# Additionally, the class-level +table_name_prefix+ is prepended and the
|
82
|
+
# +table_name_suffix+ is appended. So if you have "myapp_" as a prefix,
|
83
|
+
# the table name guess for an Invoice class becomes "myapp_invoices".
|
84
|
+
# Invoice::Lineitem becomes "myapp_invoice_lineitems".
|
85
|
+
#
|
86
|
+
# You can also set your own table name explicitly:
|
87
|
+
#
|
88
|
+
# class Mouse < ActiveRecord::Base
|
89
|
+
# self.table_name = "mice"
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# Alternatively, you can override the table_name method to define your
|
93
|
+
# own computation. (Possibly using <tt>super</tt> to manipulate the default
|
94
|
+
# table name.) Example:
|
95
|
+
#
|
96
|
+
# class Post < ActiveRecord::Base
|
97
|
+
# def self.table_name
|
98
|
+
# "special_" + super
|
99
|
+
# end
|
100
|
+
# end
|
101
|
+
# Post.table_name # => "special_posts"
|
102
|
+
def table_name
|
103
|
+
reset_table_name unless defined?(@table_name)
|
104
|
+
@table_name
|
105
|
+
end
|
106
|
+
|
107
|
+
# Sets the table name explicitly. Example:
|
108
|
+
#
|
109
|
+
# class Project < ActiveRecord::Base
|
110
|
+
# self.table_name = "project"
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# You can also just define your own <tt>self.table_name</tt> method; see
|
114
|
+
# the documentation for ActiveRecord::Base#table_name.
|
115
|
+
def table_name=(value)
|
116
|
+
value = value && value.to_s
|
117
|
+
|
118
|
+
if defined?(@table_name)
|
119
|
+
return if value == @table_name
|
120
|
+
reset_column_information if connected?
|
121
|
+
end
|
122
|
+
|
123
|
+
@table_name = value
|
124
|
+
@quoted_table_name = nil
|
125
|
+
@arel_table = nil
|
126
|
+
@sequence_name = nil unless defined?(@explicit_sequence_name) && @explicit_sequence_name
|
127
|
+
@relation = Relation.new(self, arel_table)
|
128
|
+
end
|
129
|
+
|
130
|
+
# Returns a quoted version of the table name, used to construct SQL statements.
|
131
|
+
def quoted_table_name
|
132
|
+
@quoted_table_name ||= connection.quote_table_name(table_name)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Computes the table name, (re)sets it internally, and returns it.
|
136
|
+
def reset_table_name #:nodoc:
|
137
|
+
self.table_name = if abstract_class?
|
138
|
+
superclass == Base ? nil : superclass.table_name
|
139
|
+
elsif superclass.abstract_class?
|
140
|
+
superclass.table_name || compute_table_name
|
141
|
+
else
|
142
|
+
compute_table_name
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def full_table_name_prefix #:nodoc:
|
147
|
+
(parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
|
148
|
+
end
|
149
|
+
|
150
|
+
# Defines the name of the table column which will store the class name on single-table
|
151
|
+
# inheritance situations.
|
152
|
+
#
|
153
|
+
# The default inheritance column name is +type+, which means it's a
|
154
|
+
# reserved word inside Active Record. To be able to use single-table
|
155
|
+
# inheritance with another column name, or to use the column +type+ in
|
156
|
+
# your own model for something else, you can set +inheritance_column+:
|
157
|
+
#
|
158
|
+
# self.inheritance_column = 'zoink'
|
159
|
+
def inheritance_column
|
160
|
+
(@inheritance_column ||= nil) || superclass.inheritance_column
|
161
|
+
end
|
162
|
+
|
163
|
+
# Sets the value of inheritance_column
|
164
|
+
def inheritance_column=(value)
|
165
|
+
@inheritance_column = value.to_s
|
166
|
+
@explicit_inheritance_column = true
|
167
|
+
end
|
168
|
+
|
169
|
+
def sequence_name
|
170
|
+
if base_class == self
|
171
|
+
@sequence_name ||= reset_sequence_name
|
172
|
+
else
|
173
|
+
(@sequence_name ||= nil) || base_class.sequence_name
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def reset_sequence_name #:nodoc:
|
178
|
+
@explicit_sequence_name = false
|
179
|
+
@sequence_name = connection.default_sequence_name(table_name, primary_key)
|
180
|
+
end
|
181
|
+
|
182
|
+
# Sets the name of the sequence to use when generating ids to the given
|
183
|
+
# value, or (if the value is nil or false) to the value returned by the
|
184
|
+
# given block. This is required for Oracle and is useful for any
|
185
|
+
# database which relies on sequences for primary key generation.
|
186
|
+
#
|
187
|
+
# If a sequence name is not explicitly set when using Oracle or Firebird,
|
188
|
+
# it will default to the commonly used pattern of: #{table_name}_seq
|
189
|
+
#
|
190
|
+
# If a sequence name is not explicitly set when using PostgreSQL, it
|
191
|
+
# will discover the sequence corresponding to your primary key for you.
|
192
|
+
#
|
193
|
+
# class Project < ActiveRecord::Base
|
194
|
+
# self.sequence_name = "projectseq" # default would have been "project_seq"
|
195
|
+
# end
|
196
|
+
def sequence_name=(value)
|
197
|
+
@sequence_name = value.to_s
|
198
|
+
@explicit_sequence_name = true
|
199
|
+
end
|
200
|
+
|
201
|
+
# Indicates whether the table associated with this class exists
|
202
|
+
def table_exists?
|
203
|
+
connection.schema_cache.table_exists?(table_name)
|
204
|
+
end
|
205
|
+
|
206
|
+
# Returns an array of column objects for the table associated with this class.
|
207
|
+
def columns
|
208
|
+
@columns ||= connection.schema_cache.columns(table_name).map do |col|
|
209
|
+
col = col.dup
|
210
|
+
col.primary = (col.name == primary_key)
|
211
|
+
col
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
# Returns a hash of column objects for the table associated with this class.
|
216
|
+
def columns_hash
|
217
|
+
@columns_hash ||= Hash[columns.map { |c| [c.name, c] }]
|
218
|
+
end
|
219
|
+
|
220
|
+
def column_types # :nodoc:
|
221
|
+
@column_types ||= decorate_columns(columns_hash.dup)
|
222
|
+
end
|
223
|
+
|
224
|
+
def decorate_columns(columns_hash) # :nodoc:
|
225
|
+
return if columns_hash.empty?
|
226
|
+
|
227
|
+
columns_hash.each do |name, col|
|
228
|
+
if serialized_attributes.key?(name)
|
229
|
+
columns_hash[name] = AttributeMethods::Serialization::Type.new(col)
|
230
|
+
end
|
231
|
+
if create_time_zone_conversion_attribute?(name, col)
|
232
|
+
columns_hash[name] = AttributeMethods::TimeZoneConversion::Type.new(col)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
columns_hash
|
237
|
+
end
|
238
|
+
|
239
|
+
# Returns a hash where the keys are column names and the values are
|
240
|
+
# default values when instantiating the AR object for this table.
|
241
|
+
def column_defaults
|
242
|
+
@column_defaults ||= Hash[columns.map { |c| [c.name, c.default] }]
|
243
|
+
end
|
244
|
+
|
245
|
+
# Returns an array of column names as strings.
|
246
|
+
def column_names
|
247
|
+
@column_names ||= columns.map { |column| column.name }
|
248
|
+
end
|
249
|
+
|
250
|
+
# Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
|
251
|
+
# and columns used for single table inheritance have been removed.
|
252
|
+
def content_columns
|
253
|
+
@content_columns ||= columns.reject { |c| c.primary || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
|
254
|
+
end
|
255
|
+
|
256
|
+
# Returns a hash of all the methods added to query each of the columns in the table with the name of the method as the key
|
257
|
+
# and true as the value. This makes it possible to do O(1) lookups in respond_to? to check if a given method for attribute
|
258
|
+
# is available.
|
259
|
+
def column_methods_hash #:nodoc:
|
260
|
+
@dynamic_methods_hash ||= column_names.each_with_object(Hash.new(false)) do |attr, methods|
|
261
|
+
attr_name = attr.to_s
|
262
|
+
methods[attr.to_sym] = attr_name
|
263
|
+
methods["#{attr}=".to_sym] = attr_name
|
264
|
+
methods["#{attr}?".to_sym] = attr_name
|
265
|
+
methods["#{attr}_before_type_cast".to_sym] = attr_name
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
# Resets all the cached information about columns, which will cause them
|
270
|
+
# to be reloaded on the next request.
|
271
|
+
#
|
272
|
+
# The most common usage pattern for this method is probably in a migration,
|
273
|
+
# when just after creating a table you want to populate it with some default
|
274
|
+
# values, eg:
|
275
|
+
#
|
276
|
+
# class CreateJobLevels < ActiveRecord::Migration
|
277
|
+
# def up
|
278
|
+
# create_table :job_levels do |t|
|
279
|
+
# t.integer :id
|
280
|
+
# t.string :name
|
281
|
+
#
|
282
|
+
# t.timestamps
|
283
|
+
# end
|
284
|
+
#
|
285
|
+
# JobLevel.reset_column_information
|
286
|
+
# %w{assistant executive manager director}.each do |type|
|
287
|
+
# JobLevel.create(name: type)
|
288
|
+
# end
|
289
|
+
# end
|
290
|
+
#
|
291
|
+
# def down
|
292
|
+
# drop_table :job_levels
|
293
|
+
# end
|
294
|
+
# end
|
295
|
+
def reset_column_information
|
296
|
+
connection.clear_cache!
|
297
|
+
undefine_attribute_methods
|
298
|
+
connection.schema_cache.clear_table_cache!(table_name) if table_exists?
|
299
|
+
|
300
|
+
@arel_engine = nil
|
301
|
+
@column_defaults = nil
|
302
|
+
@column_names = nil
|
303
|
+
@columns = nil
|
304
|
+
@columns_hash = nil
|
305
|
+
@column_types = nil
|
306
|
+
@content_columns = nil
|
307
|
+
@dynamic_methods_hash = nil
|
308
|
+
@inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
|
309
|
+
@relation = nil
|
310
|
+
end
|
311
|
+
|
312
|
+
# This is a hook for use by modules that need to do extra stuff to
|
313
|
+
# attributes when they are initialized. (e.g. attribute
|
314
|
+
# serialization)
|
315
|
+
def initialize_attributes(attributes, options = {}) #:nodoc:
|
316
|
+
attributes
|
317
|
+
end
|
318
|
+
|
319
|
+
private
|
320
|
+
|
321
|
+
# Guesses the table name, but does not decorate it with prefix and suffix information.
|
322
|
+
def undecorated_table_name(class_name = base_class.name)
|
323
|
+
table_name = class_name.to_s.demodulize.underscore
|
324
|
+
pluralize_table_names ? table_name.pluralize : table_name
|
325
|
+
end
|
326
|
+
|
327
|
+
# Computes and returns a table name according to default conventions.
|
328
|
+
def compute_table_name
|
329
|
+
base = base_class
|
330
|
+
if self == base
|
331
|
+
# Nested classes are prefixed with singular parent table name.
|
332
|
+
if parent < Base && !parent.abstract_class?
|
333
|
+
contained = parent.table_name
|
334
|
+
contained = contained.singularize if parent.pluralize_table_names
|
335
|
+
contained += '_'
|
336
|
+
end
|
337
|
+
"#{full_table_name_prefix}#{contained}#{undecorated_table_name(name)}#{table_name_suffix}"
|
338
|
+
else
|
339
|
+
# STI subclasses always use their superclass' table.
|
340
|
+
base.table_name
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|