aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/core.rb
Commit message (Collapse)AuthorAgeFilesLines
* Remove unneeded `@column_types` instance variableSean Griffin2014-06-221-2/+0
| | | | This was used more previously, but other uses have been removed.
* Return a null object from `AttributeSet#[]`Sean Griffin2014-06-201-1/+3
|
* docs, configurations method is listed in the docs. [ci skip]Yves Senn2014-06-201-1/+0
| | | | | The :singleton-method: directive is printed in the docs. This directive is not necessary as the method is documentable just fine.
* Introduce an object to aid in creation and management of `@attributes`Sean Griffin2014-06-191-4/+3
| | | | | Mostly delegation to start, but we can start moving a lot of behavior in bulk to this object.
* Remove unused column types overrideSean Griffin2014-06-131-3/+0
|
* Merge pull request #15689 from sgrif/sg-attribute-methodsMatthew Draper2014-06-141-0/+1
|\ | | | | Ensure we always define attribute methods
| * Ensure we always define attribute methodsSean Griffin2014-06-131-0/+1
| |
* | Merge pull request #15593 from sgrif/sg-attributeRafael Mendonça França2014-06-131-19/+18
|\ \ | |/ |/| Introduce an Attribute object to handle the type casting dance
| * Introduce an Attribute object to handle the type casting danceSean Griffin2014-06-131-19/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There's a lot more that can be moved to these, but this felt like a good place to introduce the object. Plans are: - Remove all knowledge of type casting from the columns, beyond a reference to the cast_type - Move type_cast_for_database to these objects - Potentially make them mutable, introduce a state machine, and have dirty checking handled here as well - Move `attribute`, `decorate_attribute`, and anything else that modifies types to mess with this object, not the columns hash - Introduce a collection object to manage these, reduce allocations, and not require serializing the types
* | Defer to super, rather than re-implementing Object#hashSean Griffin2014-06-121-1/+5
| |
* | Fix performance regression on preloading HABTM associationsSean Griffin2014-06-121-1/+1
|/ | | | | | | | | | | We'd spend a lot of time calling `hash` and `eql?` on the join model, which has no primary key. Calling `id` with no primary key is a really slow way to get back `nil`, so we can improve the performance there. However, even with the escape clause, we *still* weren't getting high enough performance, as we were checking the primary key too much. `hash` will always return `nil.hash` for records with no id, and `==` will always return `false`. We can optimize those cases in the HABTM join model.
* Keep the types of virtual columns after yaml serializationSean Griffin2014-06-101-0/+1
| | | | | On MySQL and PostgreSQL, the adapter does not type cast virtual columns for us.
* Move conditionals about freezing closer to the definition of `freeze`Sean Griffin2014-06-071-0/+6
| | | | | | | Reduces the number of places that care about the internals of how we store and type cast attributes. We do not need to go through the dup/freeze dance, as you couldn't have saved a frozen new record anyway, and that is the only time we would end up modifying the frozen hash.
* Bring type casting behavior of hstore/json in line with serializedSean Griffin2014-06-041-3/+6
| | | | | `@raw_attributes` should not contain the type-cast, mutable version of the value.
* Merge pull request #15172 from notEthan/active_record_pretty_printRafael Mendonça França2014-06-031-0/+23
|\ | | | | | | | | | | | | implement ActiveRecord::Base#pretty_print Conflicts: activerecord/CHANGELOG.md
| * implement ActiveRecord::Base#pretty_print + changelogEthan2014-05-291-0/+23
| |
* | Keep column defaults in type cast formSean Griffin2014-06-031-1/+1
| | | | | | | | | | | | | | | | | | | | The contract of `_field_changed?` assumes that the old value is always type cast. That is not the case for the value in `Column#default` as things are today. It appears there are other public methods that assume that `Column#default` is type cast, as well. The reason for this change originally was because the value gets put into `@raw_attributes` in initialize. This reverts to the old behavior on `Column`, and updates `initialize` to make sure that the values are in the right format.
* | Remove unused `initialize_attributes` methodSean Griffin2014-06-021-3/+2
| | | | | | | | | | This was previously a hook for a special case related to `serialize`, which has since been removed.
* | New records should remain new after yaml serializationSean Griffin2014-06-011-1/+2
| |
* | Remove most code related to serialized propertiesSean Griffin2014-06-011-1/+1
| | | | | | | | | | | | | | | | | | | | | | Nearly completely implemented in terms of custom properties. `_before_type_cast` now stores the raw serialized string consistently, which removes the need to keep track of "state". The following is now consistently true: - `model.serialized == model.reload.serialized` - A model can be dumped and loaded infinitely without changing - A model can be saved and reloaded infinitely without changing
* | Rename attribute related instance variables to better express intentSean Griffin2014-05-301-11/+11
|/ | | | | | | | | `@attributes` was actually used for `_before_type_cast` and friends, while `@attributes_cache` is the type cast version (and caching is the wrong word there, but I'm working on removing the conditionals around that). I opted for `@raw_attributes`, because `_before_type_cast` is also semantically misleading. The values in said hash are in the state given by the form builder or database, so raw seemed to be a good word.
* Fixed serialization for records with an attribute named `format`.Godfrey Chan2014-05-221-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * * * This bug can be triggered when serializing record R (the instance) of type C (the class), provided that the following conditions are met: 1. The name of one or more columns/attributes on C/R matches an existing private method on C (e.g. those defined by `Kernel`, such as `format`). 2. The attribute methods have not yet been generated on C. In this case, the matching private methods will be called by the serialization code (with no arguments) and their return values will be serialized instead. If the method requires one or more arguments, it will result in an `ArgumentError`. This regression is introduced in d1316bb. * * * Attribute methods (e.g. `#name` and `#format`, assuming the class has columns named `name` and `format` in its database table) are lazily defined. Instead of defining them when a the class is defined (e.g. in the `inherited` hook on `ActiveRecord::Base`), this operation is deferred until they are first accessed. The reason behind this is that is defining those methods requires knowing what columns are defined on the database table, which usually requires a round-trip to the database. Deferring their definition until the last-possible moment helps reducing unnessary work, especially in development mode where classes are redefined and throw away between requests. Typically, when an attribute is first accessed (e.g. `a_book.format`), it will fire the `method_missing` hook on the class, which triggers the definition of the attribute methods. This even works for methods like `format`, because calling a private method with an explicit receiver will also trigger that hook. Unfortunately, `read_attribute_for_serialization` is simply an alias to `send`, which does not respect method visibility. As a result, when serializing a record with those conflicting attributes, the `method_missing` is not fired, and as a result the attribute methods are not defined one would expected. Before d1316bb, this is negated by the fact that calling the `run_callbacks` method will also trigger a call to `respond_to?`, which is another trigger point for the class to define its attribute methods. Therefore, when Active Record tries to run the `after_find` callbacks, it will also define all the attribute methods thus masking the problem. * * * The proper fix for this problem is probably to restrict `read_attribute_for_serialization` to call public methods only (i.e. alias `read_attribute_for_serialization` to `public_send` instead of `send`). This however would be quite risky to change in a patch release and would probably require a full deprecation cycle. Another approach would be to override `read_attribute_for_serialization` inside Active Record to force the definition of attribute methods: def read_attribute_for_serialization(attribute) self.class.define_attribute_methods send(attribute) end Unfortunately, this is quite likely going to cause a performance degradation. This patch therefore restores the behaviour from the 4-0-stable branch by explicitly forcing the class to define its attribute methods in a similar spot (when records are initialized). This should not cause any extra roundtrips to the database because the `@columns` should already be cached on the class. Fixes #15188.
* Merge branch 'master' into adequaterecordAaron Patterson2014-04-201-0/+1
|\ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * master: (74 commits) [ci skip] builtin -> built-in Fix code indentation and improve formatting Grammar fix in Getting Started Guide Make URL escaping more consistent Optimize URI escaping Always escape string passed to url helper. Remove statement assuming coffee shop/public space wifi is inherently insecure Don't rely on Arel master in bug report template [ci skip] wrap methods in backticks [ci skip] "subhash" --> "sub-hash" multibyte_conformance.rb --> multibyte_conformance_test.rb Fix inconsistent behavior from String#first/#last `@destroyed` should always be set to `false` when an object is duped. remove warning `warning: ambiguous first argument; put parentheses or even spaces` :uglify -> :uglifier Regression test for irregular inflection on has_many Singularize association names before camelization Fix spelling and proper nouns Optimize select_value, select_values, select_rows and dry up checking whether to exec with cache for Postgresql adapter Include default rails protect_from_forgery with: :exception ... Conflicts: activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
| * `@destroyed` should always be set to `false` when an object is duped.Kuldeep Aggarwal2014-04-191-0/+1
| |
* | use an array for bind params to simplify substitutionAaron Patterson2014-04-121-4/+4
| |
* | decouple the factory method from the constructing modelAaron Patterson2014-04-101-2/+2
| | | | | | | | | | | | The factory method only requires the constructing model to get the connection object. Since the model is available when calling the factory method, we can just pass the appropriate connection in.
* | remove intelligence from StatementCache#initializeAaron Patterson2014-04-101-2/+2
| |
* | stop caching the class on the statement cache objectAaron Patterson2014-04-101-2/+2
| |
* | tests still pass when caching unprepared statementsAaron Patterson2014-04-091-1/+0
| |
* | Merge branch 'master' into adequaterecordAaron Patterson2014-04-071-1/+1
|\| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * master: (122 commits) Rails.application should be set inside before_configuration hook remove check for present? from delete_all Remove useless begin..end Build the reverse_order on its proper method. Use connection-specific bytea escaping Ignore order when doing count. make enums distinct per class Remove unused `subclass_controller_with_flash_type_bar` var from flash test. fix CollectionProxy delete_all documentation Added OS X specific commands to installation guide [ci skip] Recommended using homebrew for installing MySQL and PostgreSQL Fix setup of adding _flash_types test. Use SVG version of travis build status badge [skip ci] W3C CSP document moved to gihub.io URL [ci skip] sprockets-rails was released Fix the test defining the models in the right place Add CHANGELOG entry for #11650 [ci skip] Declare the assets dependency Use sass-rails 4.0.3 Make possible to use sprockets-rails 2.1 add missing parentheses to validates_with documentation [skip ci] ...
| * Ensure we are returning either `true` or `false` for `#==`Godfrey Chan2014-03-291-1/+1
| | | | | | | | | | | | 460eb83d cused `ActiveRecord::Base#==` to sometimes return `nil` in some cases, this ensures we always return a boolean value. Also fixed a similar problem in AR reflections.
* | add the deprecation to adequate record tooAaron Patterson2014-03-131-1/+5
| |
* | we can't cache when the arguments are a hashAaron Patterson2014-02-271-1/+3
| |
* | cache queries in the Model.find(id) pathAaron Patterson2014-02-181-0/+26
| |
* | add AST cache to the find_by methodAaron Patterson2014-02-171-0/+35
|/
* Add config to disable schema dump after migrationEmil Soman2014-02-061-0/+9
| | | | | | | | | * Add a config on Active Record named `dump_schema_after_migration` * Schema dump doesn't happen if the config is set to false * Set default value of the config to true * Set config in generated production environment file to false * Update configuration guide * Update CHANGELOG
* Make arel methods private APIRafael Mendonça França2014-02-011-2/+2
| | | | | | Since its conception arel was made to be private API of Active Record. If users want to use arel features directly we should provide a way using the Active Record API without exposing the arel implementation.
* Fixing issue with activerecord serialization not being able to dump a record ↵Mauricio Linhares2014-01-291-1/+1
| | | | after loading it from YAML - fixes #13861
* Move changed_attributes into dirty.rbKeenan Brock2014-01-221-14/+2
| | | Move serialization dirty into serialization.rb
* Restore ActiveRecord states after a rollback for models w/o callbacksGodfrey Chan2014-01-181-6/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes a regression (#13744) that was caused by 67d8bb9. In 67d8bb9, we introduced lazy rollback for records, such that the record's internal states and attributes are not restored immediately after a transaction rollback, but deferred until they are first accessed. This optimization is only performed when the model does not have any transactional callbacks (e.g. `after_commit` and `after_create`). Unfortunately, the models used to test the affected codepaths all comes with some sort of transactional callbacks. Therefore this codepath remains largely untested until now and as a result there are a few issues in the implementation that remains hidden until now. First, the `sync_with_transaction_state` (or more accurately, `update_attributes_from_transaction_state`) would perform the synchronization prematurely before a transaction is finalized (i.e. comitted or rolled back). As a result, when the actuall rollback happens, the record will incorrectly assumes that its internal states match the transaction state, and neglect to perform the restore. Second, `update_attributes_from_transaction_state` calls `committed!` in some cases. This in turns checks for the `destroyed?` state which also requires synchronization with the transaction stae, which causes an infnite recurrsion. This fix works by deferring the synchronization until the transaction has been finalized (addressing the first point), and also unrolled the `committed!` and `rolledback!` logic in-place (addressing the second point). It should be noted that the primary purpose of the `committed!` and `rolledback!` methods are to trigger the relevant transactional callbacks. Since this code path is only entered when there are no transactional callbacks on the model, this shouldn't be necessary. By unrolling the method calls, the intention here (to restore the states when necessary) becomes more clear.
* Ensure Active Record connection consistencyschneems2014-01-091-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently Active Record can be configured via the environment variable `DATABASE_URL` or by manually injecting a hash of values which is what Rails does, reading in `database.yml` and setting Active Record appropriately. Active Record expects to be able to use `DATABASE_URL` without the use of Rails, and we cannot rip out this functionality without deprecating. This presents a problem though when both config is set, and a `DATABASE_URL` is present. Currently the `DATABASE_URL` should "win" and none of the values in `database.yml` are used. This is somewhat unexpected to me if I were to set values such as `pool` in the `production:` group of `database.yml` they are ignored. There are many ways that active record initiates a connection today: - Stand Alone (without rails) - `rake db:<tasks>` - ActiveRecord.establish_connection - With Rails - `rake db:<tasks>` - `rails <server> | <console>` - `rails dbconsole` We should make all of these behave exactly the same way. The best way to do this is to put all of this logic in one place so it is guaranteed to be used. Here is my prosed matrix of how this behavior should work: ``` No database.yml No DATABASE_URL => Error ``` ``` database.yml present No DATABASE_URL => Use database.yml configuration ``` ``` No database.yml DATABASE_URL present => use DATABASE_URL configuration ``` ``` database.yml present DATABASE_URL present => Merged into `url` sub key. If both specify `url` sub key, the `database.yml` `url` sub key "wins". If other paramaters `adapter` or `database` are specified in YAML, they are discarded as the `url` sub key "wins". ``` ### Implementation Current implementation uses `ActiveRecord::Base.configurations` to resolve and merge all connection information before returning. This is achieved through a utility class: `ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig`. To understand the exact behavior of this class, it is best to review the behavior in activerecord/test/cases/connection_adapters/connection_handler_test.rb though it should match the above proposal.
* Automatically maintain test database schemaJon Leighton2014-01-021-0/+3
| | | | | | | | | | | | | | * Move check from generated helper to test_help.rb, so that all applications can benefit * Rather than just raising when the test schema has pending migrations, try to load in the schema and only raise if there are pending migrations afterwards * Opt out of the check by setting config.active_record.maintain_test_schema = false * Deprecate db:test:* tasks. The test helper is now fully responsible for maintaining the test schema, so we don't need rake tasks for this. This is also a speed improvement since we're no longer reloading the test database on every call to "rake test".
* [ci skip] used new syntax for scopesKuldeep Aggarwal2013-11-271-1/+1
|
* Changed message for Model.inspectArun Agrawal2013-11-091-1/+1
| | | | | | | (No database connection) sounds not be able to connect database. see more #12804
* Merge pull request #12390 from kennyj/rename_to_association_methodsRafael Mendonça França2013-11-051-4/+4
|\ | | | | | | | | | | | | | | | | Renamed generated_feature_methods to generated_association_methods. Conflicts: activerecord/lib/active_record/associations/builder/association.rb activerecord/lib/active_record/associations/builder/singular_association.rb activerecord/test/cases/base_test.rb
| * Renamed generated_feature_methods to generated_association_methods.kennyj2013-09-281-4/+4
| |
* | Revert "ActiveRecord::Base#<=> has been removed. Primary keys may not be in ↵David Heinemeier Hansson2013-11-021-0/+9
|/ | | | | | | | | order," -- will be replaced with a check to ensure that the keys used for comparison are integers, and only fail if they are not. This reverts commit 6256734e2d0bdd89f4b5d11da259d40afa0c95c7. Conflicts: activerecord/CHANGELOG.md
* Merge pull request #10816 from bogdan/less-dirty-dirtyRafael Mendonça França2013-09-231-3/+1
| | | | Make AM::Dirty less dirty to plugin into AR or other library
* Add back options argument in the ActiveRecord::Base.initialize methodRafael Mendonça França2013-09-211-2/+10
| | | | | | | This will make easier to hook protected_attributes gem in our code without making that gem fragile to change in Rails code base. Closes #12243
* support objects with blank string primary keysAaron Patterson2013-09-181-1/+1
|