activerecord 3.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 +35 -44
- data/examples/performance.rb +110 -100
- data/lib/active_record/aggregations.rb +59 -75
- 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 +60 -59
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +16 -59
- 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 +40 -112
- data/lib/active_record/associations/has_many_association.rb +83 -76
- data/lib/active_record/associations/has_many_through_association.rb +147 -66
- data/lib/active_record/associations/has_one_association.rb +67 -108
- data/lib/active_record/associations/has_one_through_association.rb +21 -25
- 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 +512 -1224
- data/lib/active_record/attribute_assignment.rb +201 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +49 -12
- data/lib/active_record/attribute_methods/dirty.rb +51 -28
- data/lib/active_record/attribute_methods/primary_key.rb +94 -22
- data/lib/active_record/attribute_methods/query.rb +5 -4
- data/lib/active_record/attribute_methods/read.rb +63 -72
- data/lib/active_record/attribute_methods/serialization.rb +162 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -41
- data/lib/active_record/attribute_methods/write.rb +39 -13
- data/lib/active_record/attribute_methods.rb +362 -29
- data/lib/active_record/autosave_association.rb +132 -75
- data/lib/active_record/base.rb +83 -1627
- data/lib/active_record/callbacks.rb +69 -47
- data/lib/active_record/coders/yaml_column.rb +38 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +411 -138
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +21 -11
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +234 -173
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +36 -22
- data/lib/active_record/connection_adapters/abstract/quoting.rb +82 -25
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +176 -414
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +562 -232
- data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +281 -53
- 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 +365 -450
- 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 +672 -752
- data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +588 -17
- 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 +108 -101
- data/lib/active_record/dynamic_matchers.rb +131 -0
- data/lib/active_record/errors.rb +54 -13
- 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 +703 -785
- data/lib/active_record/inheritance.rb +200 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locale/en.yml +8 -1
- data/lib/active_record/locking/optimistic.rb +69 -60
- data/lib/active_record/locking/pessimistic.rb +34 -12
- data/lib/active_record/log_subscriber.rb +40 -6
- 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 +614 -216
- data/lib/active_record/model_schema.rb +345 -0
- data/lib/active_record/nested_attributes.rb +248 -119
- data/lib/active_record/null_relation.rb +65 -0
- data/lib/active_record/persistence.rb +275 -57
- data/lib/active_record/query_cache.rb +29 -9
- data/lib/active_record/querying.rb +62 -0
- data/lib/active_record/railtie.rb +135 -21
- data/lib/active_record/railties/console_sandbox.rb +5 -0
- data/lib/active_record/railties/controller_runtime.rb +17 -5
- data/lib/active_record/railties/databases.rake +249 -359
- 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 +283 -103
- data/lib/active_record/relation/batches.rb +38 -34
- data/lib/active_record/relation/calculations.rb +252 -139
- data/lib/active_record/relation/delegation.rb +125 -0
- data/lib/active_record/relation/finder_methods.rb +182 -188
- data/lib/active_record/relation/merger.rb +161 -0
- data/lib/active_record/relation/predicate_builder.rb +86 -21
- data/lib/active_record/relation/query_methods.rb +917 -134
- data/lib/active_record/relation/spawn_methods.rb +53 -92
- data/lib/active_record/relation.rb +405 -143
- 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 +20 -14
- data/lib/active_record/schema_dumper.rb +55 -46
- 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 +8 -46
- data/lib/active_record/serializers/xml_serializer.rb +21 -68
- 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 +57 -28
- data/lib/active_record/timestamp.rb +49 -18
- data/lib/active_record/transactions.rb +106 -63
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +25 -24
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +123 -83
- data/lib/active_record/validations.rb +29 -29
- data/lib/active_record/version.rb +7 -5
- data/lib/active_record.rb +83 -34
- data/lib/rails/generators/active_record/migration/migration_generator.rb +46 -9
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +30 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -5
- data/lib/rails/generators/active_record/model/templates/model.rb +7 -2
- data/lib/rails/generators/active_record/model/templates/module.rb +3 -1
- data/lib/rails/generators/active_record.rb +4 -8
- metadata +163 -121
- data/CHANGELOG +0 -6023
- data/examples/associations.png +0 -0
- data/lib/active_record/association_preload.rb +0 -403
- data/lib/active_record/associations/association_collection.rb +0 -562
- data/lib/active_record/associations/association_proxy.rb +0 -295
- data/lib/active_record/associations/through_association_scope.rb +0 -154
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -113
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -401
- data/lib/active_record/dynamic_finder_match.rb +0 -53
- data/lib/active_record/dynamic_scope_match.rb +0 -32
- data/lib/active_record/named_scope.rb +0 -138
- data/lib/active_record/observer.rb +0 -140
- data/lib/active_record/session_store.rb +0 -340
- data/lib/rails/generators/active_record/model/templates/migration.rb +0 -16
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -2
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -24
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -16
@@ -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
|