activerecord 3.1.10 → 4.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


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

Files changed (237) hide show
  1. checksums.yaml +6 -6
  2. data/CHANGELOG.md +1837 -338
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +39 -43
  5. data/examples/performance.rb +51 -20
  6. data/examples/simple.rb +4 -4
  7. data/lib/active_record/aggregations.rb +57 -43
  8. data/lib/active_record/association_relation.rb +35 -0
  9. data/lib/active_record/associations/alias_tracker.rb +47 -39
  10. data/lib/active_record/associations/association.rb +71 -85
  11. data/lib/active_record/associations/association_scope.rb +138 -89
  12. data/lib/active_record/associations/belongs_to_association.rb +65 -25
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +9 -3
  14. data/lib/active_record/associations/builder/association.rb +125 -29
  15. data/lib/active_record/associations/builder/belongs_to.rb +91 -60
  16. data/lib/active_record/associations/builder/collection_association.rb +69 -49
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +113 -42
  18. data/lib/active_record/associations/builder/has_many.rb +8 -64
  19. data/lib/active_record/associations/builder/has_one.rb +12 -52
  20. data/lib/active_record/associations/builder/singular_association.rb +22 -29
  21. data/lib/active_record/associations/collection_association.rb +294 -187
  22. data/lib/active_record/associations/collection_proxy.rb +961 -94
  23. data/lib/active_record/associations/foreign_association.rb +11 -0
  24. data/lib/active_record/associations/has_many_association.rb +118 -23
  25. data/lib/active_record/associations/has_many_through_association.rb +115 -45
  26. data/lib/active_record/associations/has_one_association.rb +57 -24
  27. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  28. data/lib/active_record/associations/join_dependency/join_association.rb +76 -102
  29. data/lib/active_record/associations/join_dependency/join_base.rb +7 -9
  30. data/lib/active_record/associations/join_dependency/join_part.rb +30 -37
  31. data/lib/active_record/associations/join_dependency.rb +230 -156
  32. data/lib/active_record/associations/preloader/association.rb +96 -55
  33. data/lib/active_record/associations/preloader/collection_association.rb +3 -3
  34. data/lib/active_record/associations/preloader/has_many_through.rb +7 -3
  35. data/lib/active_record/associations/preloader/has_one.rb +1 -1
  36. data/lib/active_record/associations/preloader/singular_association.rb +3 -3
  37. data/lib/active_record/associations/preloader/through_association.rb +61 -32
  38. data/lib/active_record/associations/preloader.rb +113 -87
  39. data/lib/active_record/associations/singular_association.rb +29 -13
  40. data/lib/active_record/associations/through_association.rb +37 -19
  41. data/lib/active_record/associations.rb +505 -371
  42. data/lib/active_record/attribute.rb +163 -0
  43. data/lib/active_record/attribute_assignment.rb +212 -0
  44. data/lib/active_record/attribute_decorators.rb +66 -0
  45. data/lib/active_record/attribute_methods/before_type_cast.rb +52 -7
  46. data/lib/active_record/attribute_methods/dirty.rb +141 -51
  47. data/lib/active_record/attribute_methods/primary_key.rb +87 -36
  48. data/lib/active_record/attribute_methods/query.rb +5 -4
  49. data/lib/active_record/attribute_methods/read.rb +74 -117
  50. data/lib/active_record/attribute_methods/serialization.rb +70 -0
  51. data/lib/active_record/attribute_methods/time_zone_conversion.rb +49 -47
  52. data/lib/active_record/attribute_methods/write.rb +60 -21
  53. data/lib/active_record/attribute_methods.rb +409 -48
  54. data/lib/active_record/attribute_set/builder.rb +106 -0
  55. data/lib/active_record/attribute_set.rb +81 -0
  56. data/lib/active_record/attributes.rb +147 -0
  57. data/lib/active_record/autosave_association.rb +279 -232
  58. data/lib/active_record/base.rb +84 -1969
  59. data/lib/active_record/callbacks.rb +66 -28
  60. data/lib/active_record/coders/json.rb +13 -0
  61. data/lib/active_record/coders/yaml_column.rb +18 -21
  62. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +422 -243
  63. data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
  64. data/lib/active_record/connection_adapters/abstract/database_statements.rb +170 -194
  65. data/lib/active_record/connection_adapters/abstract/query_cache.rb +32 -19
  66. data/lib/active_record/connection_adapters/abstract/quoting.rb +79 -57
  67. data/lib/active_record/connection_adapters/abstract/savepoints.rb +21 -0
  68. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +125 -0
  69. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +273 -170
  70. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +50 -0
  71. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +731 -254
  72. data/lib/active_record/connection_adapters/abstract/transaction.rb +215 -0
  73. data/lib/active_record/connection_adapters/abstract_adapter.rb +339 -95
  74. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +946 -0
  75. data/lib/active_record/connection_adapters/column.rb +33 -221
  76. data/lib/active_record/connection_adapters/connection_specification.rb +275 -0
  77. data/lib/active_record/connection_adapters/mysql2_adapter.rb +140 -602
  78. data/lib/active_record/connection_adapters/mysql_adapter.rb +254 -756
  79. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +93 -0
  80. data/lib/active_record/connection_adapters/postgresql/column.rb +20 -0
  81. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +232 -0
  82. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +100 -0
  83. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +15 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +46 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +11 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +36 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +19 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/float.rb +21 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +13 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +11 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +35 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +43 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +79 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +19 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/time.rb +11 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +109 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid.rb +36 -0
  108. data/lib/active_record/connection_adapters/postgresql/quoting.rb +108 -0
  109. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +152 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +596 -0
  112. data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
  113. data/lib/active_record/connection_adapters/postgresql_adapter.rb +445 -902
  114. data/lib/active_record/connection_adapters/schema_cache.rb +94 -0
  115. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +578 -25
  116. data/lib/active_record/connection_handling.rb +132 -0
  117. data/lib/active_record/core.rb +579 -0
  118. data/lib/active_record/counter_cache.rb +159 -102
  119. data/lib/active_record/dynamic_matchers.rb +140 -0
  120. data/lib/active_record/enum.rb +197 -0
  121. data/lib/active_record/errors.rb +102 -34
  122. data/lib/active_record/explain.rb +38 -0
  123. data/lib/active_record/explain_registry.rb +30 -0
  124. data/lib/active_record/explain_subscriber.rb +29 -0
  125. data/lib/active_record/fixture_set/file.rb +56 -0
  126. data/lib/active_record/fixtures.rb +318 -260
  127. data/lib/active_record/gem_version.rb +15 -0
  128. data/lib/active_record/inheritance.rb +247 -0
  129. data/lib/active_record/integration.rb +113 -0
  130. data/lib/active_record/legacy_yaml_adapter.rb +30 -0
  131. data/lib/active_record/locale/en.yml +8 -1
  132. data/lib/active_record/locking/optimistic.rb +80 -52
  133. data/lib/active_record/locking/pessimistic.rb +27 -5
  134. data/lib/active_record/log_subscriber.rb +25 -18
  135. data/lib/active_record/migration/command_recorder.rb +130 -38
  136. data/lib/active_record/migration/join_table.rb +15 -0
  137. data/lib/active_record/migration.rb +532 -201
  138. data/lib/active_record/model_schema.rb +342 -0
  139. data/lib/active_record/nested_attributes.rb +229 -139
  140. data/lib/active_record/no_touching.rb +52 -0
  141. data/lib/active_record/null_relation.rb +81 -0
  142. data/lib/active_record/persistence.rb +304 -99
  143. data/lib/active_record/query_cache.rb +25 -43
  144. data/lib/active_record/querying.rb +68 -0
  145. data/lib/active_record/railtie.rb +86 -45
  146. data/lib/active_record/railties/console_sandbox.rb +3 -4
  147. data/lib/active_record/railties/controller_runtime.rb +7 -4
  148. data/lib/active_record/railties/databases.rake +198 -377
  149. data/lib/active_record/railties/jdbcmysql_error.rb +2 -2
  150. data/lib/active_record/readonly_attributes.rb +23 -0
  151. data/lib/active_record/reflection.rb +516 -165
  152. data/lib/active_record/relation/batches.rb +96 -45
  153. data/lib/active_record/relation/calculations.rb +221 -144
  154. data/lib/active_record/relation/delegation.rb +140 -0
  155. data/lib/active_record/relation/finder_methods.rb +362 -243
  156. data/lib/active_record/relation/merger.rb +193 -0
  157. data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
  158. data/lib/active_record/relation/predicate_builder/relation_handler.rb +13 -0
  159. data/lib/active_record/relation/predicate_builder.rb +135 -41
  160. data/lib/active_record/relation/query_methods.rb +982 -155
  161. data/lib/active_record/relation/spawn_methods.rb +50 -110
  162. data/lib/active_record/relation.rb +371 -180
  163. data/lib/active_record/result.rb +109 -12
  164. data/lib/active_record/runtime_registry.rb +22 -0
  165. data/lib/active_record/sanitization.rb +191 -0
  166. data/lib/active_record/schema.rb +19 -13
  167. data/lib/active_record/schema_dumper.rb +111 -61
  168. data/lib/active_record/schema_migration.rb +53 -0
  169. data/lib/active_record/scoping/default.rb +135 -0
  170. data/lib/active_record/scoping/named.rb +164 -0
  171. data/lib/active_record/scoping.rb +87 -0
  172. data/lib/active_record/serialization.rb +7 -45
  173. data/lib/active_record/serializers/xml_serializer.rb +14 -65
  174. data/lib/active_record/statement_cache.rb +111 -0
  175. data/lib/active_record/store.rb +205 -0
  176. data/lib/active_record/tasks/database_tasks.rb +299 -0
  177. data/lib/active_record/tasks/mysql_database_tasks.rb +159 -0
  178. data/lib/active_record/tasks/postgresql_database_tasks.rb +101 -0
  179. data/lib/active_record/tasks/sqlite_database_tasks.rb +55 -0
  180. data/lib/active_record/timestamp.rb +35 -14
  181. data/lib/active_record/transactions.rb +141 -74
  182. data/lib/active_record/translation.rb +22 -0
  183. data/lib/active_record/type/big_integer.rb +13 -0
  184. data/lib/active_record/type/binary.rb +50 -0
  185. data/lib/active_record/type/boolean.rb +31 -0
  186. data/lib/active_record/type/date.rb +50 -0
  187. data/lib/active_record/type/date_time.rb +54 -0
  188. data/lib/active_record/type/decimal.rb +64 -0
  189. data/lib/active_record/type/decimal_without_scale.rb +11 -0
  190. data/lib/active_record/type/decorator.rb +14 -0
  191. data/lib/active_record/type/float.rb +19 -0
  192. data/lib/active_record/type/hash_lookup_type_map.rb +23 -0
  193. data/lib/active_record/type/integer.rb +59 -0
  194. data/lib/active_record/type/mutable.rb +16 -0
  195. data/lib/active_record/type/numeric.rb +36 -0
  196. data/lib/active_record/type/serialized.rb +62 -0
  197. data/lib/active_record/type/string.rb +40 -0
  198. data/lib/active_record/type/text.rb +11 -0
  199. data/lib/active_record/type/time.rb +26 -0
  200. data/lib/active_record/type/time_value.rb +38 -0
  201. data/lib/active_record/type/type_map.rb +64 -0
  202. data/lib/active_record/type/unsigned_integer.rb +15 -0
  203. data/lib/active_record/type/value.rb +110 -0
  204. data/lib/active_record/type.rb +23 -0
  205. data/lib/active_record/validations/associated.rb +27 -18
  206. data/lib/active_record/validations/presence.rb +67 -0
  207. data/lib/active_record/validations/uniqueness.rb +125 -66
  208. data/lib/active_record/validations.rb +37 -30
  209. data/lib/active_record/version.rb +5 -7
  210. data/lib/active_record.rb +80 -25
  211. data/lib/rails/generators/active_record/migration/migration_generator.rb +54 -9
  212. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
  213. data/lib/rails/generators/active_record/migration/templates/migration.rb +25 -11
  214. data/lib/rails/generators/active_record/migration.rb +11 -8
  215. data/lib/rails/generators/active_record/model/model_generator.rb +17 -4
  216. data/lib/rails/generators/active_record/model/templates/model.rb +5 -2
  217. data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
  218. data/lib/rails/generators/active_record.rb +3 -11
  219. metadata +132 -53
  220. data/examples/associations.png +0 -0
  221. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -62
  222. data/lib/active_record/associations/join_helper.rb +0 -55
  223. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
  224. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -135
  225. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -556
  226. data/lib/active_record/dynamic_finder_match.rb +0 -56
  227. data/lib/active_record/dynamic_scope_match.rb +0 -23
  228. data/lib/active_record/identity_map.rb +0 -163
  229. data/lib/active_record/named_scope.rb +0 -200
  230. data/lib/active_record/observer.rb +0 -121
  231. data/lib/active_record/session_store.rb +0 -358
  232. data/lib/active_record/test_case.rb +0 -69
  233. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -17
  234. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  235. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
  236. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
  237. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -16
@@ -0,0 +1,342 @@
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
+ #
33
+ # If you are organising your models within modules, you can add a suffix to the models within
34
+ # a namespace by defining a singleton method in the parent module called table_name_suffix which
35
+ # returns your chosen suffix.
36
+ class_attribute :table_name_suffix, instance_writer: false
37
+ self.table_name_suffix = ""
38
+
39
+ ##
40
+ # :singleton-method:
41
+ # Accessor for the name of the schema migrations table. By default, the value is "schema_migrations"
42
+ class_attribute :schema_migrations_table_name, instance_accessor: false
43
+ self.schema_migrations_table_name = "schema_migrations"
44
+
45
+ ##
46
+ # :singleton-method:
47
+ # Indicates whether table names should be the pluralized versions of the corresponding class names.
48
+ # If true, the default table name for a Product class will be +products+. If false, it would just be +product+.
49
+ # See table_name for the full rules on table/class naming. This is true, by default.
50
+ class_attribute :pluralize_table_names, instance_writer: false
51
+ self.pluralize_table_names = true
52
+
53
+ self.inheritance_column = 'type'
54
+
55
+ delegate :type_for_attribute, to: :class
56
+ end
57
+
58
+ # Derives the join table name for +first_table+ and +second_table+. The
59
+ # table names appear in alphabetical order. A common prefix is removed
60
+ # (useful for namespaced models like Music::Artist and Music::Record):
61
+ #
62
+ # artists, records => artists_records
63
+ # records, artists => artists_records
64
+ # music_artists, music_records => music_artists_records
65
+ def self.derive_join_table_name(first_table, second_table) # :nodoc:
66
+ [first_table.to_s, second_table.to_s].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').tr("\0", "_")
67
+ end
68
+
69
+ module ClassMethods
70
+ # Guesses the table name (in forced lower-case) based on the name of the class in the
71
+ # inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy
72
+ # looks like: Reply < Message < ActiveRecord::Base, then Message is used
73
+ # to guess the table name even when called on Reply. The rules used to do the guess
74
+ # are handled by the Inflector class in Active Support, which knows almost all common
75
+ # English inflections. You can add new inflections in config/initializers/inflections.rb.
76
+ #
77
+ # Nested classes are given table names prefixed by the singular form of
78
+ # the parent's table name. Enclosing modules are not considered.
79
+ #
80
+ # ==== Examples
81
+ #
82
+ # class Invoice < ActiveRecord::Base
83
+ # end
84
+ #
85
+ # file class table_name
86
+ # invoice.rb Invoice invoices
87
+ #
88
+ # class Invoice < ActiveRecord::Base
89
+ # class Lineitem < ActiveRecord::Base
90
+ # end
91
+ # end
92
+ #
93
+ # file class table_name
94
+ # invoice.rb Invoice::Lineitem invoice_lineitems
95
+ #
96
+ # module Invoice
97
+ # class Lineitem < ActiveRecord::Base
98
+ # end
99
+ # end
100
+ #
101
+ # file class table_name
102
+ # invoice/lineitem.rb Invoice::Lineitem lineitems
103
+ #
104
+ # Additionally, the class-level +table_name_prefix+ is prepended and the
105
+ # +table_name_suffix+ is appended. So if you have "myapp_" as a prefix,
106
+ # the table name guess for an Invoice class becomes "myapp_invoices".
107
+ # Invoice::Lineitem becomes "myapp_invoice_lineitems".
108
+ #
109
+ # You can also set your own table name explicitly:
110
+ #
111
+ # class Mouse < ActiveRecord::Base
112
+ # self.table_name = "mice"
113
+ # end
114
+ #
115
+ # Alternatively, you can override the table_name method to define your
116
+ # own computation. (Possibly using <tt>super</tt> to manipulate the default
117
+ # table name.) Example:
118
+ #
119
+ # class Post < ActiveRecord::Base
120
+ # def self.table_name
121
+ # "special_" + super
122
+ # end
123
+ # end
124
+ # Post.table_name # => "special_posts"
125
+ def table_name
126
+ reset_table_name unless defined?(@table_name)
127
+ @table_name
128
+ end
129
+
130
+ # Sets the table name explicitly. Example:
131
+ #
132
+ # class Project < ActiveRecord::Base
133
+ # self.table_name = "project"
134
+ # end
135
+ #
136
+ # You can also just define your own <tt>self.table_name</tt> method; see
137
+ # the documentation for ActiveRecord::Base#table_name.
138
+ def table_name=(value)
139
+ value = value && value.to_s
140
+
141
+ if defined?(@table_name)
142
+ return if value == @table_name
143
+ reset_column_information if connected?
144
+ end
145
+
146
+ @table_name = value
147
+ @quoted_table_name = nil
148
+ @arel_table = nil
149
+ @sequence_name = nil unless defined?(@explicit_sequence_name) && @explicit_sequence_name
150
+ @relation = Relation.create(self, arel_table)
151
+ end
152
+
153
+ # Returns a quoted version of the table name, used to construct SQL statements.
154
+ def quoted_table_name
155
+ @quoted_table_name ||= connection.quote_table_name(table_name)
156
+ end
157
+
158
+ # Computes the table name, (re)sets it internally, and returns it.
159
+ def reset_table_name #:nodoc:
160
+ self.table_name = if abstract_class?
161
+ superclass == Base ? nil : superclass.table_name
162
+ elsif superclass.abstract_class?
163
+ superclass.table_name || compute_table_name
164
+ else
165
+ compute_table_name
166
+ end
167
+ end
168
+
169
+ def full_table_name_prefix #:nodoc:
170
+ (parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
171
+ end
172
+
173
+ def full_table_name_suffix #:nodoc:
174
+ (parents.detect {|p| p.respond_to?(:table_name_suffix) } || self).table_name_suffix
175
+ end
176
+
177
+ # Defines the name of the table column which will store the class name on single-table
178
+ # inheritance situations.
179
+ #
180
+ # The default inheritance column name is +type+, which means it's a
181
+ # reserved word inside Active Record. To be able to use single-table
182
+ # inheritance with another column name, or to use the column +type+ in
183
+ # your own model for something else, you can set +inheritance_column+:
184
+ #
185
+ # self.inheritance_column = 'zoink'
186
+ def inheritance_column
187
+ (@inheritance_column ||= nil) || superclass.inheritance_column
188
+ end
189
+
190
+ # Sets the value of inheritance_column
191
+ def inheritance_column=(value)
192
+ @inheritance_column = value.to_s
193
+ @explicit_inheritance_column = true
194
+ end
195
+
196
+ def sequence_name
197
+ if base_class == self
198
+ @sequence_name ||= reset_sequence_name
199
+ else
200
+ (@sequence_name ||= nil) || base_class.sequence_name
201
+ end
202
+ end
203
+
204
+ def reset_sequence_name #:nodoc:
205
+ @explicit_sequence_name = false
206
+ @sequence_name = connection.default_sequence_name(table_name, primary_key)
207
+ end
208
+
209
+ # Sets the name of the sequence to use when generating ids to the given
210
+ # value, or (if the value is nil or false) to the value returned by the
211
+ # given block. This is required for Oracle and is useful for any
212
+ # database which relies on sequences for primary key generation.
213
+ #
214
+ # If a sequence name is not explicitly set when using Oracle,
215
+ # it will default to the commonly used pattern of: #{table_name}_seq
216
+ #
217
+ # If a sequence name is not explicitly set when using PostgreSQL, it
218
+ # will discover the sequence corresponding to your primary key for you.
219
+ #
220
+ # class Project < ActiveRecord::Base
221
+ # self.sequence_name = "projectseq" # default would have been "project_seq"
222
+ # end
223
+ def sequence_name=(value)
224
+ @sequence_name = value.to_s
225
+ @explicit_sequence_name = true
226
+ end
227
+
228
+ # Indicates whether the table associated with this class exists
229
+ def table_exists?
230
+ connection.schema_cache.table_exists?(table_name)
231
+ end
232
+
233
+ def attributes_builder # :nodoc:
234
+ @attributes_builder ||= AttributeSet::Builder.new(column_types, primary_key)
235
+ end
236
+
237
+ def column_types # :nodoc:
238
+ @column_types ||= columns_hash.transform_values(&:cast_type).tap do |h|
239
+ h.default = Type::Value.new
240
+ end
241
+ end
242
+
243
+ def type_for_attribute(attr_name) # :nodoc:
244
+ column_types[attr_name]
245
+ end
246
+
247
+ # Returns a hash where the keys are column names and the values are
248
+ # default values when instantiating the AR object for this table.
249
+ def column_defaults
250
+ _default_attributes.dup.to_hash
251
+ end
252
+
253
+ def _default_attributes # :nodoc:
254
+ @default_attributes ||= attributes_builder.build_from_database(
255
+ raw_default_values)
256
+ end
257
+
258
+ # Returns an array of column names as strings.
259
+ def column_names
260
+ @column_names ||= columns.map { |column| column.name }
261
+ end
262
+
263
+ # Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
264
+ # and columns used for single table inheritance have been removed.
265
+ def content_columns
266
+ @content_columns ||= columns.reject { |c| c.name == primary_key || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
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)
299
+
300
+ @arel_engine = nil
301
+ @column_names = nil
302
+ @column_types = nil
303
+ @content_columns = nil
304
+ @default_attributes = nil
305
+ @inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
306
+ @relation = nil
307
+
308
+ initialize_find_by_cache
309
+ end
310
+
311
+ private
312
+
313
+ # Guesses the table name, but does not decorate it with prefix and suffix information.
314
+ def undecorated_table_name(class_name = base_class.name)
315
+ table_name = class_name.to_s.demodulize.underscore
316
+ pluralize_table_names ? table_name.pluralize : table_name
317
+ end
318
+
319
+ # Computes and returns a table name according to default conventions.
320
+ def compute_table_name
321
+ base = base_class
322
+ if self == base
323
+ # Nested classes are prefixed with singular parent table name.
324
+ if parent < Base && !parent.abstract_class?
325
+ contained = parent.table_name
326
+ contained = contained.singularize if parent.pluralize_table_names
327
+ contained += '_'
328
+ end
329
+
330
+ "#{full_table_name_prefix}#{contained}#{undecorated_table_name(name)}#{full_table_name_suffix}"
331
+ else
332
+ # STI subclasses always use their superclass' table.
333
+ base.table_name
334
+ end
335
+ end
336
+
337
+ def raw_default_values
338
+ columns_hash.transform_values(&:default)
339
+ end
340
+ end
341
+ end
342
+ end