activerecord 4.2.0

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

Potentially problematic release.


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

Files changed (221) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1372 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +218 -0
  5. data/examples/performance.rb +184 -0
  6. data/examples/simple.rb +14 -0
  7. data/lib/active_record.rb +173 -0
  8. data/lib/active_record/aggregations.rb +266 -0
  9. data/lib/active_record/association_relation.rb +22 -0
  10. data/lib/active_record/associations.rb +1724 -0
  11. data/lib/active_record/associations/alias_tracker.rb +87 -0
  12. data/lib/active_record/associations/association.rb +253 -0
  13. data/lib/active_record/associations/association_scope.rb +194 -0
  14. data/lib/active_record/associations/belongs_to_association.rb +111 -0
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +40 -0
  16. data/lib/active_record/associations/builder/association.rb +149 -0
  17. data/lib/active_record/associations/builder/belongs_to.rb +116 -0
  18. data/lib/active_record/associations/builder/collection_association.rb +91 -0
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +124 -0
  20. data/lib/active_record/associations/builder/has_many.rb +15 -0
  21. data/lib/active_record/associations/builder/has_one.rb +23 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +38 -0
  23. data/lib/active_record/associations/collection_association.rb +634 -0
  24. data/lib/active_record/associations/collection_proxy.rb +1027 -0
  25. data/lib/active_record/associations/has_many_association.rb +184 -0
  26. data/lib/active_record/associations/has_many_through_association.rb +238 -0
  27. data/lib/active_record/associations/has_one_association.rb +105 -0
  28. data/lib/active_record/associations/has_one_through_association.rb +36 -0
  29. data/lib/active_record/associations/join_dependency.rb +282 -0
  30. data/lib/active_record/associations/join_dependency/join_association.rb +122 -0
  31. data/lib/active_record/associations/join_dependency/join_base.rb +22 -0
  32. data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
  33. data/lib/active_record/associations/preloader.rb +203 -0
  34. data/lib/active_record/associations/preloader/association.rb +162 -0
  35. data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
  36. data/lib/active_record/associations/preloader/collection_association.rb +24 -0
  37. data/lib/active_record/associations/preloader/has_many.rb +17 -0
  38. data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
  39. data/lib/active_record/associations/preloader/has_one.rb +23 -0
  40. data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
  41. data/lib/active_record/associations/preloader/singular_association.rb +21 -0
  42. data/lib/active_record/associations/preloader/through_association.rb +96 -0
  43. data/lib/active_record/associations/singular_association.rb +86 -0
  44. data/lib/active_record/associations/through_association.rb +96 -0
  45. data/lib/active_record/attribute.rb +149 -0
  46. data/lib/active_record/attribute_assignment.rb +212 -0
  47. data/lib/active_record/attribute_decorators.rb +66 -0
  48. data/lib/active_record/attribute_methods.rb +439 -0
  49. data/lib/active_record/attribute_methods/before_type_cast.rb +71 -0
  50. data/lib/active_record/attribute_methods/dirty.rb +181 -0
  51. data/lib/active_record/attribute_methods/primary_key.rb +128 -0
  52. data/lib/active_record/attribute_methods/query.rb +40 -0
  53. data/lib/active_record/attribute_methods/read.rb +103 -0
  54. data/lib/active_record/attribute_methods/serialization.rb +70 -0
  55. data/lib/active_record/attribute_methods/time_zone_conversion.rb +65 -0
  56. data/lib/active_record/attribute_methods/write.rb +83 -0
  57. data/lib/active_record/attribute_set.rb +77 -0
  58. data/lib/active_record/attribute_set/builder.rb +86 -0
  59. data/lib/active_record/attributes.rb +139 -0
  60. data/lib/active_record/autosave_association.rb +439 -0
  61. data/lib/active_record/base.rb +317 -0
  62. data/lib/active_record/callbacks.rb +313 -0
  63. data/lib/active_record/coders/json.rb +13 -0
  64. data/lib/active_record/coders/yaml_column.rb +38 -0
  65. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +659 -0
  66. data/lib/active_record/connection_adapters/abstract/database_limits.rb +67 -0
  67. data/lib/active_record/connection_adapters/abstract/database_statements.rb +373 -0
  68. data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -0
  69. data/lib/active_record/connection_adapters/abstract/quoting.rb +133 -0
  70. data/lib/active_record/connection_adapters/abstract/savepoints.rb +21 -0
  71. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +125 -0
  72. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +574 -0
  73. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +50 -0
  74. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +991 -0
  75. data/lib/active_record/connection_adapters/abstract/transaction.rb +219 -0
  76. data/lib/active_record/connection_adapters/abstract_adapter.rb +487 -0
  77. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +883 -0
  78. data/lib/active_record/connection_adapters/column.rb +82 -0
  79. data/lib/active_record/connection_adapters/connection_specification.rb +275 -0
  80. data/lib/active_record/connection_adapters/mysql2_adapter.rb +282 -0
  81. data/lib/active_record/connection_adapters/mysql_adapter.rb +491 -0
  82. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +93 -0
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +20 -0
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +232 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid.rb +36 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +99 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +14 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +46 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +11 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +27 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +17 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/float.rb +21 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +13 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +11 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +35 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +43 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +79 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +15 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/time.rb +11 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +97 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
  109. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
  110. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
  111. data/lib/active_record/connection_adapters/postgresql/quoting.rb +108 -0
  112. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +152 -0
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +588 -0
  115. data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
  116. data/lib/active_record/connection_adapters/postgresql_adapter.rb +754 -0
  117. data/lib/active_record/connection_adapters/schema_cache.rb +94 -0
  118. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +628 -0
  119. data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
  120. data/lib/active_record/connection_handling.rb +132 -0
  121. data/lib/active_record/core.rb +566 -0
  122. data/lib/active_record/counter_cache.rb +175 -0
  123. data/lib/active_record/dynamic_matchers.rb +140 -0
  124. data/lib/active_record/enum.rb +198 -0
  125. data/lib/active_record/errors.rb +252 -0
  126. data/lib/active_record/explain.rb +38 -0
  127. data/lib/active_record/explain_registry.rb +30 -0
  128. data/lib/active_record/explain_subscriber.rb +29 -0
  129. data/lib/active_record/fixture_set/file.rb +56 -0
  130. data/lib/active_record/fixtures.rb +1007 -0
  131. data/lib/active_record/gem_version.rb +15 -0
  132. data/lib/active_record/inheritance.rb +247 -0
  133. data/lib/active_record/integration.rb +113 -0
  134. data/lib/active_record/locale/en.yml +47 -0
  135. data/lib/active_record/locking/optimistic.rb +204 -0
  136. data/lib/active_record/locking/pessimistic.rb +77 -0
  137. data/lib/active_record/log_subscriber.rb +75 -0
  138. data/lib/active_record/migration.rb +1051 -0
  139. data/lib/active_record/migration/command_recorder.rb +197 -0
  140. data/lib/active_record/migration/join_table.rb +15 -0
  141. data/lib/active_record/model_schema.rb +340 -0
  142. data/lib/active_record/nested_attributes.rb +548 -0
  143. data/lib/active_record/no_touching.rb +52 -0
  144. data/lib/active_record/null_relation.rb +81 -0
  145. data/lib/active_record/persistence.rb +532 -0
  146. data/lib/active_record/query_cache.rb +56 -0
  147. data/lib/active_record/querying.rb +68 -0
  148. data/lib/active_record/railtie.rb +162 -0
  149. data/lib/active_record/railties/console_sandbox.rb +5 -0
  150. data/lib/active_record/railties/controller_runtime.rb +50 -0
  151. data/lib/active_record/railties/databases.rake +391 -0
  152. data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
  153. data/lib/active_record/readonly_attributes.rb +23 -0
  154. data/lib/active_record/reflection.rb +881 -0
  155. data/lib/active_record/relation.rb +681 -0
  156. data/lib/active_record/relation/batches.rb +138 -0
  157. data/lib/active_record/relation/calculations.rb +403 -0
  158. data/lib/active_record/relation/delegation.rb +140 -0
  159. data/lib/active_record/relation/finder_methods.rb +528 -0
  160. data/lib/active_record/relation/merger.rb +170 -0
  161. data/lib/active_record/relation/predicate_builder.rb +126 -0
  162. data/lib/active_record/relation/predicate_builder/array_handler.rb +47 -0
  163. data/lib/active_record/relation/predicate_builder/relation_handler.rb +13 -0
  164. data/lib/active_record/relation/query_methods.rb +1176 -0
  165. data/lib/active_record/relation/spawn_methods.rb +75 -0
  166. data/lib/active_record/result.rb +131 -0
  167. data/lib/active_record/runtime_registry.rb +22 -0
  168. data/lib/active_record/sanitization.rb +191 -0
  169. data/lib/active_record/schema.rb +64 -0
  170. data/lib/active_record/schema_dumper.rb +251 -0
  171. data/lib/active_record/schema_migration.rb +56 -0
  172. data/lib/active_record/scoping.rb +87 -0
  173. data/lib/active_record/scoping/default.rb +134 -0
  174. data/lib/active_record/scoping/named.rb +164 -0
  175. data/lib/active_record/serialization.rb +22 -0
  176. data/lib/active_record/serializers/xml_serializer.rb +193 -0
  177. data/lib/active_record/statement_cache.rb +111 -0
  178. data/lib/active_record/store.rb +205 -0
  179. data/lib/active_record/tasks/database_tasks.rb +296 -0
  180. data/lib/active_record/tasks/mysql_database_tasks.rb +145 -0
  181. data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
  182. data/lib/active_record/tasks/sqlite_database_tasks.rb +55 -0
  183. data/lib/active_record/timestamp.rb +121 -0
  184. data/lib/active_record/transactions.rb +417 -0
  185. data/lib/active_record/translation.rb +22 -0
  186. data/lib/active_record/type.rb +23 -0
  187. data/lib/active_record/type/big_integer.rb +13 -0
  188. data/lib/active_record/type/binary.rb +50 -0
  189. data/lib/active_record/type/boolean.rb +30 -0
  190. data/lib/active_record/type/date.rb +46 -0
  191. data/lib/active_record/type/date_time.rb +43 -0
  192. data/lib/active_record/type/decimal.rb +40 -0
  193. data/lib/active_record/type/decimal_without_scale.rb +11 -0
  194. data/lib/active_record/type/decorator.rb +14 -0
  195. data/lib/active_record/type/float.rb +19 -0
  196. data/lib/active_record/type/hash_lookup_type_map.rb +17 -0
  197. data/lib/active_record/type/integer.rb +55 -0
  198. data/lib/active_record/type/mutable.rb +16 -0
  199. data/lib/active_record/type/numeric.rb +36 -0
  200. data/lib/active_record/type/serialized.rb +56 -0
  201. data/lib/active_record/type/string.rb +36 -0
  202. data/lib/active_record/type/text.rb +11 -0
  203. data/lib/active_record/type/time.rb +26 -0
  204. data/lib/active_record/type/time_value.rb +38 -0
  205. data/lib/active_record/type/type_map.rb +64 -0
  206. data/lib/active_record/type/unsigned_integer.rb +15 -0
  207. data/lib/active_record/type/value.rb +101 -0
  208. data/lib/active_record/validations.rb +90 -0
  209. data/lib/active_record/validations/associated.rb +51 -0
  210. data/lib/active_record/validations/presence.rb +67 -0
  211. data/lib/active_record/validations/uniqueness.rb +229 -0
  212. data/lib/active_record/version.rb +8 -0
  213. data/lib/rails/generators/active_record.rb +17 -0
  214. data/lib/rails/generators/active_record/migration.rb +18 -0
  215. data/lib/rails/generators/active_record/migration/migration_generator.rb +70 -0
  216. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +22 -0
  217. data/lib/rails/generators/active_record/migration/templates/migration.rb +45 -0
  218. data/lib/rails/generators/active_record/model/model_generator.rb +52 -0
  219. data/lib/rails/generators/active_record/model/templates/model.rb +10 -0
  220. data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
  221. metadata +309 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2004-2014 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,218 @@
1
+ = Active Record -- Object-relational mapping in Rails
2
+
3
+ Active Record connects classes to relational database tables to establish an
4
+ almost zero-configuration persistence layer for applications. The library
5
+ provides a base class that, when subclassed, sets up a mapping between the new
6
+ class and an existing table in the database. In the context of an application,
7
+ these classes are commonly referred to as *models*. Models can also be
8
+ connected to other models; this is done by defining *associations*.
9
+
10
+ Active Record relies heavily on naming in that it uses class and association
11
+ names to establish mappings between respective database tables and foreign key
12
+ columns. Although these mappings can be defined explicitly, it's recommended
13
+ to follow naming conventions, especially when getting started with the
14
+ library.
15
+
16
+ A short rundown of some of the major features:
17
+
18
+ * Automated mapping between classes and tables, attributes and columns.
19
+
20
+ class Product < ActiveRecord::Base
21
+ end
22
+
23
+ {Learn more}[link:classes/ActiveRecord/Base.html]
24
+
25
+ The Product class is automatically mapped to the table named "products",
26
+ which might look like this:
27
+
28
+ CREATE TABLE products (
29
+ id int(11) NOT NULL auto_increment,
30
+ name varchar(255),
31
+ PRIMARY KEY (id)
32
+ );
33
+
34
+ This would also define the following accessors: `Product#name` and
35
+ `Product#name=(new_name)`.
36
+
37
+
38
+ * Associations between objects defined by simple class methods.
39
+
40
+ class Firm < ActiveRecord::Base
41
+ has_many :clients
42
+ has_one :account
43
+ belongs_to :conglomerate
44
+ end
45
+
46
+ {Learn more}[link:classes/ActiveRecord/Associations/ClassMethods.html]
47
+
48
+
49
+ * Aggregations of value objects.
50
+
51
+ class Account < ActiveRecord::Base
52
+ composed_of :balance, class_name: 'Money',
53
+ mapping: %w(balance amount)
54
+ composed_of :address,
55
+ mapping: [%w(address_street street), %w(address_city city)]
56
+ end
57
+
58
+ {Learn more}[link:classes/ActiveRecord/Aggregations/ClassMethods.html]
59
+
60
+
61
+ * Validation rules that can differ for new or existing objects.
62
+
63
+ class Account < ActiveRecord::Base
64
+ validates :subdomain, :name, :email_address, :password, presence: true
65
+ validates :subdomain, uniqueness: true
66
+ validates :terms_of_service, acceptance: true, on: :create
67
+ validates :password, :email_address, confirmation: true, on: :create
68
+ end
69
+
70
+ {Learn more}[link:classes/ActiveRecord/Validations.html]
71
+
72
+
73
+ * Callbacks available for the entire life cycle (instantiation, saving, destroying, validating, etc.).
74
+
75
+ class Person < ActiveRecord::Base
76
+ before_destroy :invalidate_payment_plan
77
+ # the `invalidate_payment_plan` method gets called just before Person#destroy
78
+ end
79
+
80
+ {Learn more}[link:classes/ActiveRecord/Callbacks.html]
81
+
82
+
83
+ * Inheritance hierarchies.
84
+
85
+ class Company < ActiveRecord::Base; end
86
+ class Firm < Company; end
87
+ class Client < Company; end
88
+ class PriorityClient < Client; end
89
+
90
+ {Learn more}[link:classes/ActiveRecord/Base.html]
91
+
92
+
93
+ * Transactions.
94
+
95
+ # Database transaction
96
+ Account.transaction do
97
+ david.withdrawal(100)
98
+ mary.deposit(100)
99
+ end
100
+
101
+ {Learn more}[link:classes/ActiveRecord/Transactions/ClassMethods.html]
102
+
103
+
104
+ * Reflections on columns, associations, and aggregations.
105
+
106
+ reflection = Firm.reflect_on_association(:clients)
107
+ reflection.klass # => Client (class)
108
+ Firm.columns # Returns an array of column descriptors for the firms table
109
+
110
+ {Learn more}[link:classes/ActiveRecord/Reflection/ClassMethods.html]
111
+
112
+
113
+ * Database abstraction through simple adapters.
114
+
115
+ # connect to SQLite3
116
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'dbfile.sqlite3')
117
+
118
+ # connect to MySQL with authentication
119
+ ActiveRecord::Base.establish_connection(
120
+ adapter: 'mysql2',
121
+ host: 'localhost',
122
+ username: 'me',
123
+ password: 'secret',
124
+ database: 'activerecord'
125
+ )
126
+
127
+ {Learn more}[link:classes/ActiveRecord/Base.html] and read about the built-in support for
128
+ MySQL[link:classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html],
129
+ PostgreSQL[link:classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html], and
130
+ SQLite3[link:classes/ActiveRecord/ConnectionAdapters/SQLite3Adapter.html].
131
+
132
+
133
+ * Logging support for Log4r[https://github.com/colbygk/log4r] and Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc].
134
+
135
+ ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
136
+ ActiveRecord::Base.logger = Log4r::Logger.new('Application Log')
137
+
138
+
139
+ * Database agnostic schema management with Migrations.
140
+
141
+ class AddSystemSettings < ActiveRecord::Migration
142
+ def up
143
+ create_table :system_settings do |t|
144
+ t.string :name
145
+ t.string :label
146
+ t.text :value
147
+ t.string :type
148
+ t.integer :position
149
+ end
150
+
151
+ SystemSetting.create name: 'notice', label: 'Use notice?', value: 1
152
+ end
153
+
154
+ def down
155
+ drop_table :system_settings
156
+ end
157
+ end
158
+
159
+ {Learn more}[link:classes/ActiveRecord/Migration.html]
160
+
161
+
162
+ == Philosophy
163
+
164
+ Active Record is an implementation of the object-relational mapping (ORM)
165
+ pattern[http://www.martinfowler.com/eaaCatalog/activeRecord.html] by the same
166
+ name described by Martin Fowler:
167
+
168
+ "An object that wraps a row in a database table or view,
169
+ encapsulates the database access, and adds domain logic on that data."
170
+
171
+ Active Record attempts to provide a coherent wrapper as a solution for the inconvenience that is
172
+ object-relational mapping. The prime directive for this mapping has been to minimize
173
+ the amount of code needed to build a real-world domain model. This is made possible
174
+ by relying on a number of conventions that make it easy for Active Record to infer
175
+ complex relations and structures from a minimal amount of explicit direction.
176
+
177
+ Convention over Configuration:
178
+ * No XML files!
179
+ * Lots of reflection and run-time extension
180
+ * Magic is not inherently a bad word
181
+
182
+ Admit the Database:
183
+ * Lets you drop down to SQL for odd cases and performance
184
+ * Doesn't attempt to duplicate or replace data definitions
185
+
186
+
187
+ == Download and installation
188
+
189
+ The latest version of Active Record can be installed with RubyGems:
190
+
191
+ % [sudo] gem install activerecord
192
+
193
+ Source code can be downloaded as part of the Rails project on GitHub:
194
+
195
+ * https://github.com/rails/rails/tree/4-2-stable/activerecord
196
+
197
+
198
+ == License
199
+
200
+ Active Record is released under the MIT license:
201
+
202
+ * http://www.opensource.org/licenses/MIT
203
+
204
+
205
+ == Support
206
+
207
+ API documentation is at:
208
+
209
+ * http://api.rubyonrails.org
210
+
211
+ Bug reports can be filed for the Ruby on Rails project here:
212
+
213
+ * https://github.com/rails/rails/issues
214
+
215
+ Feature requests should be discussed on the rails-core mailing list here:
216
+
217
+ * https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-core
218
+
@@ -0,0 +1,184 @@
1
+ require File.expand_path('../../../load_paths', __FILE__)
2
+ require "active_record"
3
+ require 'benchmark/ips'
4
+
5
+ TIME = (ENV['BENCHMARK_TIME'] || 20).to_i
6
+ RECORDS = (ENV['BENCHMARK_RECORDS'] || TIME*1000).to_i
7
+
8
+ conn = { adapter: 'sqlite3', database: ':memory:' }
9
+
10
+ ActiveRecord::Base.establish_connection(conn)
11
+
12
+ class User < ActiveRecord::Base
13
+ connection.create_table :users, force: true do |t|
14
+ t.string :name, :email
15
+ t.timestamps
16
+ end
17
+
18
+ has_many :exhibits
19
+ end
20
+
21
+ class Exhibit < ActiveRecord::Base
22
+ connection.create_table :exhibits, force: true do |t|
23
+ t.belongs_to :user
24
+ t.string :name
25
+ t.text :notes
26
+ t.timestamps
27
+ end
28
+
29
+ belongs_to :user
30
+
31
+ def look; attributes end
32
+ def feel; look; user.name end
33
+
34
+ def self.with_name
35
+ where("name IS NOT NULL")
36
+ end
37
+
38
+ def self.with_notes
39
+ where("notes IS NOT NULL")
40
+ end
41
+
42
+ def self.look(exhibits) exhibits.each { |e| e.look } end
43
+ def self.feel(exhibits) exhibits.each { |e| e.feel } end
44
+ end
45
+
46
+ def progress_bar(int); print "." if (int%100).zero? ; end
47
+
48
+ puts 'Generating data...'
49
+
50
+ module ActiveRecord
51
+ class Faker
52
+ LOREM = %Q{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse non aliquet diam. Curabitur vel urna metus, quis malesuada elit.
53
+ Integer consequat tincidunt felis. Etiam non erat dolor. Vivamus imperdiet nibh sit amet diam eleifend id posuere diam malesuada. Mauris at accumsan sem.
54
+ Donec id lorem neque. Fusce erat lorem, ornare eu congue vitae, malesuada quis neque. Maecenas vel urna a velit pretium fermentum. Donec tortor enim,
55
+ tempor venenatis egestas a, tempor sed ipsum. Ut arcu justo, faucibus non imperdiet ac, interdum at diam. Pellentesque ipsum enim, venenatis ut iaculis vitae,
56
+ varius vitae sem. Sed rutrum quam ac elit euismod bibendum. Donec ultricies ultricies magna, at lacinia libero mollis aliquam. Sed ac arcu in tortor elementum
57
+ tincidunt vel interdum sem. Curabitur eget erat arcu. Praesent eget eros leo. Nam magna enim, sollicitudin vehicula scelerisque in, vulputate ut libero.
58
+ Praesent varius tincidunt commodo}.split
59
+
60
+ def self.name
61
+ LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join ' '
62
+ end
63
+
64
+ def self.email
65
+ LOREM.grep(/^\w*$/).sort_by { rand }.first(2).join('@') + ".com"
66
+ end
67
+ end
68
+ end
69
+
70
+ # pre-compute the insert statements and fake data compilation,
71
+ # so the benchmarks below show the actual runtime for the execute
72
+ # method, minus the setup steps
73
+
74
+ # Using the same paragraph for all exhibits because it is very slow
75
+ # to generate unique paragraphs for all exhibits.
76
+ notes = ActiveRecord::Faker::LOREM.join ' '
77
+ today = Date.today
78
+
79
+ puts "Inserting #{RECORDS} users and exhibits..."
80
+ RECORDS.times do |record|
81
+ user = User.create(
82
+ created_at: today,
83
+ name: ActiveRecord::Faker.name,
84
+ email: ActiveRecord::Faker.email
85
+ )
86
+
87
+ Exhibit.create(
88
+ created_at: today,
89
+ name: ActiveRecord::Faker.name,
90
+ user: user,
91
+ notes: notes
92
+ )
93
+ progress_bar(record)
94
+ end
95
+ puts "Done!\n"
96
+
97
+ Benchmark.ips(TIME) do |x|
98
+ ar_obj = Exhibit.find(1)
99
+ attrs = { name: 'sam' }
100
+ attrs_first = { name: 'sam' }
101
+ attrs_second = { name: 'tom' }
102
+ exhibit = {
103
+ name: ActiveRecord::Faker.name,
104
+ notes: notes,
105
+ created_at: Date.today
106
+ }
107
+
108
+ x.report("Model#id") do
109
+ ar_obj.id
110
+ end
111
+
112
+ x.report 'Model.new (instantiation)' do
113
+ Exhibit.new
114
+ end
115
+
116
+ x.report 'Model.new (setting attributes)' do
117
+ Exhibit.new(attrs)
118
+ end
119
+
120
+ x.report 'Model.first' do
121
+ Exhibit.first.look
122
+ end
123
+
124
+ x.report 'Model.take' do
125
+ Exhibit.take
126
+ end
127
+
128
+ x.report("Model.all limit(100)") do
129
+ Exhibit.look Exhibit.limit(100)
130
+ end
131
+
132
+ x.report("Model.all take(100)") do
133
+ Exhibit.look Exhibit.take(100)
134
+ end
135
+
136
+ x.report "Model.all limit(100) with relationship" do
137
+ Exhibit.feel Exhibit.limit(100).includes(:user)
138
+ end
139
+
140
+ x.report "Model.all limit(10,000)" do
141
+ Exhibit.look Exhibit.limit(10000)
142
+ end
143
+
144
+ x.report 'Model.named_scope' do
145
+ Exhibit.limit(10).with_name.with_notes
146
+ end
147
+
148
+ x.report 'Model.create' do
149
+ Exhibit.create(exhibit)
150
+ end
151
+
152
+ x.report 'Resource#attributes=' do
153
+ e = Exhibit.new(attrs_first)
154
+ e.attributes = attrs_second
155
+ end
156
+
157
+ x.report 'Resource#update' do
158
+ Exhibit.first.update(name: 'bob')
159
+ end
160
+
161
+ x.report 'Resource#destroy' do
162
+ Exhibit.first.destroy
163
+ end
164
+
165
+ x.report 'Model.transaction' do
166
+ Exhibit.transaction { Exhibit.new }
167
+ end
168
+
169
+ x.report 'Model.find(id)' do
170
+ User.find(1)
171
+ end
172
+
173
+ x.report 'Model.find_by_sql' do
174
+ Exhibit.find_by_sql("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}").first
175
+ end
176
+
177
+ x.report "Model.log" do
178
+ Exhibit.connection.send(:log, "hello", "world") {}
179
+ end
180
+
181
+ x.report "AR.execute(query)" do
182
+ ActiveRecord::Base.connection.execute("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}")
183
+ end
184
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../../../load_paths', __FILE__)
2
+ require 'active_record'
3
+
4
+ class Person < ActiveRecord::Base
5
+ establish_connection adapter: 'sqlite3', database: 'foobar.db'
6
+ connection.create_table table_name, force: true do |t|
7
+ t.string :name
8
+ end
9
+ end
10
+
11
+ bob = Person.create!(name: 'bob')
12
+ puts Person.all.inspect
13
+ bob.destroy
14
+ puts Person.all.inspect
@@ -0,0 +1,173 @@
1
+ #--
2
+ # Copyright (c) 2004-2014 David Heinemeier Hansson
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'active_support'
25
+ require 'active_support/rails'
26
+ require 'active_model'
27
+ require 'arel'
28
+
29
+ require 'active_record/version'
30
+ require 'active_record/attribute_set'
31
+
32
+ module ActiveRecord
33
+ extend ActiveSupport::Autoload
34
+
35
+ autoload :Attribute
36
+ autoload :Base
37
+ autoload :Callbacks
38
+ autoload :Core
39
+ autoload :ConnectionHandling
40
+ autoload :CounterCache
41
+ autoload :DynamicMatchers
42
+ autoload :Enum
43
+ autoload :Explain
44
+ autoload :Inheritance
45
+ autoload :Integration
46
+ autoload :Migration
47
+ autoload :Migrator, 'active_record/migration'
48
+ autoload :ModelSchema
49
+ autoload :NestedAttributes
50
+ autoload :NoTouching
51
+ autoload :Persistence
52
+ autoload :QueryCache
53
+ autoload :Querying
54
+ autoload :ReadonlyAttributes
55
+ autoload :Reflection
56
+ autoload :RuntimeRegistry
57
+ autoload :Sanitization
58
+ autoload :Schema
59
+ autoload :SchemaDumper
60
+ autoload :SchemaMigration
61
+ autoload :Scoping
62
+ autoload :Serialization
63
+ autoload :StatementCache
64
+ autoload :Store
65
+ autoload :Timestamp
66
+ autoload :Transactions
67
+ autoload :Translation
68
+ autoload :Validations
69
+
70
+ eager_autoload do
71
+ autoload :ActiveRecordError, 'active_record/errors'
72
+ autoload :ConnectionNotEstablished, 'active_record/errors'
73
+ autoload :ConnectionAdapters, 'active_record/connection_adapters/abstract_adapter'
74
+
75
+ autoload :Aggregations
76
+ autoload :Associations
77
+ autoload :AttributeAssignment
78
+ autoload :AttributeMethods
79
+ autoload :AutosaveAssociation
80
+
81
+ autoload :Relation
82
+ autoload :AssociationRelation
83
+ autoload :NullRelation
84
+
85
+ autoload_under 'relation' do
86
+ autoload :QueryMethods
87
+ autoload :FinderMethods
88
+ autoload :Calculations
89
+ autoload :PredicateBuilder
90
+ autoload :SpawnMethods
91
+ autoload :Batches
92
+ autoload :Delegation
93
+ end
94
+
95
+ autoload :Result
96
+ end
97
+
98
+ module Coders
99
+ autoload :YAMLColumn, 'active_record/coders/yaml_column'
100
+ autoload :JSON, 'active_record/coders/json'
101
+ end
102
+
103
+ module AttributeMethods
104
+ extend ActiveSupport::Autoload
105
+
106
+ eager_autoload do
107
+ autoload :BeforeTypeCast
108
+ autoload :Dirty
109
+ autoload :PrimaryKey
110
+ autoload :Query
111
+ autoload :Read
112
+ autoload :TimeZoneConversion
113
+ autoload :Write
114
+ autoload :Serialization
115
+ end
116
+ end
117
+
118
+ module Locking
119
+ extend ActiveSupport::Autoload
120
+
121
+ eager_autoload do
122
+ autoload :Optimistic
123
+ autoload :Pessimistic
124
+ end
125
+ end
126
+
127
+ module ConnectionAdapters
128
+ extend ActiveSupport::Autoload
129
+
130
+ eager_autoload do
131
+ autoload :AbstractAdapter
132
+ autoload :ConnectionManagement, "active_record/connection_adapters/abstract/connection_pool"
133
+ end
134
+ end
135
+
136
+ module Scoping
137
+ extend ActiveSupport::Autoload
138
+
139
+ eager_autoload do
140
+ autoload :Named
141
+ autoload :Default
142
+ end
143
+ end
144
+
145
+ module Tasks
146
+ extend ActiveSupport::Autoload
147
+
148
+ autoload :DatabaseTasks
149
+ autoload :SQLiteDatabaseTasks, 'active_record/tasks/sqlite_database_tasks'
150
+ autoload :MySQLDatabaseTasks, 'active_record/tasks/mysql_database_tasks'
151
+ autoload :PostgreSQLDatabaseTasks,
152
+ 'active_record/tasks/postgresql_database_tasks'
153
+ end
154
+
155
+ autoload :TestFixtures, 'active_record/fixtures'
156
+
157
+ def self.eager_load!
158
+ super
159
+ ActiveRecord::Locking.eager_load!
160
+ ActiveRecord::Scoping.eager_load!
161
+ ActiveRecord::Associations.eager_load!
162
+ ActiveRecord::AttributeMethods.eager_load!
163
+ ActiveRecord::ConnectionAdapters.eager_load!
164
+ end
165
+ end
166
+
167
+ ActiveSupport.on_load(:active_record) do
168
+ Arel::Table.engine = self
169
+ end
170
+
171
+ ActiveSupport.on_load(:i18n) do
172
+ I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'
173
+ end