aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
Commit message (Collapse)AuthorAgeFilesLines
* Stop using instance exec for type decoratorsSean Griffin & Sean Doyle2014-06-271-5/+15
| | | | | | | | We are moving this behavior out to an object that we would like to keep separated from `ActiveRecord::Base`, which means not passing the class object to it. As such, we need to stop using `instance_exec`, and instead close over the subclass on global type decorators that are applied in `Base`.
* Move writing unknown column exception to null attributeSean Griffin2014-06-262-9/+2
| | | | | | Making this change revealed several subtle bugs related to models with no primary key, and anonymous classes. These have been fixed as well, with regression tests added.
* Encapsulate the creation of `Attribute` objectsSean Griffin2014-06-261-3/+2
| | | | | | | | This will make it less painful to add additional properties, which should persist across writes, such as `name`. Conflicts: activerecord/lib/active_record/attribute_set.rb
* Merge pull request #15868 from sgrif/sg-uninitialized-attributesRafael Mendonça França2014-06-261-10/+3
|\ | | | | | | | | | | | | | | Move behavior of `read_attribute` to `AttributeSet` Conflicts: activerecord/lib/active_record/attribute_set.rb activerecord/test/cases/attribute_set_test.rb
| * Move behavior of `read_attribute` to `AttributeSet`Sean Griffin2014-06-251-10/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Moved `Builder` to its own file, as it started looking very weird once I added private methods to the `AttributeSet` class and the `Builder` class started to grow. Would like to refactor `fetch_value` to change to ```ruby self[name].value(&block) ``` But that requires the attributes to know about their name, which they currently do not.
* | Merge pull request #15846 from sgrif/sg-attributes-before-type-castRafael Mendonça França2014-06-261-1/+1
|\ \ | | | | | | | | | | | | | | | | | | | | | Move `attributes_before_type_cast` to `AttributeSet` Conflicts: activerecord/lib/active_record/attribute_set.rb activerecord/test/cases/attribute_set_test.rb
| * | Move `attributes_before_type_cast` to `AttributeSet`Sean Griffin2014-06-211-1/+1
| | |
* | | Silence warning in testSean Griffin2014-06-241-2/+2
| |/ |/| | | | | | | We still had one file using `column_for_attribute` when it could return nil, causing deprecation warnings in the tests.
* | Return a null object from `AttributeSet#[]`Sean Griffin2014-06-203-10/+4
|/
* Use `column_defaults` in dirty for checking changed defaultsSean Griffin2014-06-171-11/+3
| | | | | | We no longer need to "init changed attributes" from the initializer, either, as there is no longer a case where a given value would differ from the default, but would not already be marked as changed.
* Promote time zone aware attributes to a first class type decoratorSean Griffin2014-06-161-1/+6
| | | | | | | | | | | | | This refactoring revealed the need for another form of decoration, which takes a proc to select which it applies to (There's a *lot* of cases where this form can be used). To avoid duplication, we can re-implement the old decoration in terms of the proc-based decoration. The reason we're `instance_exec`ing the matcher is for cases such as time zone aware attributes, where a decorator is defined in a parent class, and a method called in the matcher is overridden by a child class. The matcher will close over the parent, and evaluate in its context, which is not the behavior we want.
* Refactor in-place dirty checking to use the attribute objectSean Griffin2014-06-162-9/+7
|
* Change the deprecation warning on `serialized_attributes`Sean Griffin2014-06-151-8/+2
| | | | to "without replacement"
* Deprecate `serialized_attributes` without replacementSean Griffin2014-06-141-16/+17
| | | | | We've stopped using it internally, in favor of polymorphism. So should you!
* Merge pull request #15674 from sgrif/sg-mutable-attributesMatthew Draper2014-06-142-23/+77
|\ | | | | Detect in-place changes on mutable AR attributes
| * Detect in-place changes on mutable AR attributesSean Griffin2014-06-132-23/+77
| | | | | | | | | | | | We have several mutable types on Active Record now. (Serialized, JSON, HStore). We need to be able to detect if these have been modified in place.
* | Merge pull request #15593 from sgrif/sg-attributeRafael Mendonça França2014-06-133-29/+19
|\ \ | | | | | | Introduce an Attribute object to handle the type casting dance
| * | Introduce an Attribute object to handle the type casting danceSean Griffin2014-06-133-29/+19
| |/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* | Use a conditional rather than early return in `id`Sean Griffin2014-06-121-3/+4
| |
* | Fix performance regression on preloading HABTM associationsSean Griffin2014-06-121-0/+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.
* rm cached attributesSean Griffin2014-06-111-38/+11
| | | | | | | | | | The original patch that added this concept can be found [here](https://web.archive.org/web/20090601022739/http://dev.rubyonrails.org/ticket/9767). The current default behavior is to cache everything except serialized columns, unless the user specified otherwise. If anyone were to specify otherwise, many types would actually be completely broken. Still, the method is left in place with a deprecation warning in case anyone is actually still calling this method.
* Rename `type_cast` to `type_cast_from_database`Sean Griffin2014-06-092-3/+3
| | | | | | | | In some cases there is a difference between the two, we should always be doing one or the other. For convenience, `type_cast` is still a private method on type, so new types that do not need different behavior don't need to implement two methods, but it has been moved to private so it cannot be used accidentally.
* Make `_before_type_cast` actually be before type castSean Griffin2014-06-092-46/+15
| | | | | | | | | | | | | - The following is now true for all types, all the time - `model.attribute_before_type_cast == given_value` - `model.attribute == model.save_and_reload.attribute` - `model.attribute == model.dup.attribute` - `model.attribute == YAML.load(YAML.dump(model)).attribute` - Removes the remaining types implementing `type_cast_for_write` - Simplifies the implementation of time zone aware attributes - Brings tz aware attributes closer to being implemented as an attribute decorator - Adds additional point of control for custom types
* Merge pull request #15546 from sgrif/sg-lazy-decoratorsRafael Mendonça França2014-06-071-4/+2
|\ | | | | Don't query the database schema when calling `serialize`
| * Don't query the database schema when calling `serialize`Sean Griffin2014-06-071-4/+2
| | | | | | | | | | | | We need to decorate the types lazily. This is extracted to a separate API, as there are other refactorings that will be able to make use of it, and to allow unit testing the finer points more granularly.
* | Merge pull request #15561 from sgrif/sg-time-zone-aware-arraysRafael Mendonça França2014-06-071-1/+11
|\ \ | | | | | | Ensure time zones don't change after round trip with array columns
| * | Ensure time zones don't change after round trip with array columnsSean Griffin2014-06-071-1/+11
| | | | | | | | | | | | | | | The times would be equivalent, even if they were in different time zones. E.g. 12:00 UTC == 5:00 PDT
* | | Do not type cast twice on attribute assignmentSean Griffin2014-06-071-11/+20
|/ / | | | | | | | | | | | | | | | | | | | | | | | | The definition of `write_attribute` in dirty checking ultimately leads to the columns calling `type_cast` on the value to perform the comparison. However, this is a potentially expensive computation that we cache when it occurs in `read_attribute`. The only case that we need the non-type-cast form is for numeric, so we pass that through as well (something I'm looking to remove in the future). This also reduces the number of places that manually access various stages in an attribute's type casting lifecycle, which will aid in one of the larger refactorings that I'm working on.
* | changelog for #15556 and credit @kuldeepaggarwal [Kuldeep Aggarwal]Yves Senn2014-06-071-2/+0
| | | | | | | | | | | | This is a follow up to #15556 @kuldeepaggarwal did submit this patch way back (#13624).
* | Add array support when time zone aware attributes are enabledSean Griffin2014-06-071-1/+13
|/
* Return a null column when no column exists for an attributeSean Griffin2014-06-032-7/+8
|
* Refactor determination of whether the field has changedSean Griffin2014-06-032-35/+2
| | | | | The types know more about what is going on than the dirty module. Let's ask them!
* Remove most code related to serialized propertiesSean Griffin2014-06-013-58/+7
| | | | | | | | | | | 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
* Don't change values in `@raw_attributes` during serializationSean Griffin2014-06-011-1/+1
| | | | | | During `init_with`, the attributes given to the coder will be placed into `@raw_attributes`. As such, we should read from `@raw_attributes` when encoding, rather than `@attributes`, which has been type cast.
* Rename attribute related instance variables to better express intentSean Griffin2014-05-306-16/+16
| | | | | | | | | `@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.
* Don't reference comments that do not existSean Griffin2014-05-291-2/+0
|
* Move `type_cast_for_write` behavior over to the serialized type objectSean Griffin2014-05-292-48/+8
|
* Refactor serialized types to be partially defined as custom propertiesSean Griffin2014-05-291-20/+6
| | | | | | | Many of the methods defined in `AttributeMethods::Serialization` can be refactored onto this type as well, but this is a reasonable small step. Removes the `Type` class, and the need for `decorate_columns` to handle serialized types.
* Add an interface for type objects to control Ruby => SQLSean Griffin2014-05-262-8/+4
| | | | | Adds the ability to save custom types, which type cast to non-primitive ruby objects.
* Merge pull request #14613 from Sirupsen/fix-serialize-update-columnRafael Mendonça França2014-05-212-14/+30
|\ | | | | | | | | | | | | Fix serialized field returning serialized data after update_column Conflicts: activerecord/CHANGELOG.md
| * Fix serialized field returning serialized data after update_columnSimon Eskildsen2014-04-052-14/+30
| |
* | Remove :timestamp column typeSean Griffin2014-05-192-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | The `:timestamp` type for columns is unused. All database adapters treat them as the same database type. All code in `ActiveRecord` which changes its behavior based on the column's type acts the same in both cases. However, when the type is passed to code that checks for the `:datetime` type, but not `:timestamp` (such as XML serialization), the result is unexpected behavior. Existing schema definitions will continue to work, and the `timestamp` type is transparently aliased to `datetime`.
* | docs, double meaning of `serialize` argument. Closes #14284.Yves Senn2014-04-141-5/+16
|/ | | | | | | | The second argument to serialize has double meaning: * `class_name` of the Type to serialize * `coder` to use then serializing. The new variable name and the docs better describe that behavior.
* [Active Record] Renamed private methods create_record and update_recordPrathamesh Sonpatki2014-02-201-2/+2
| | | | | | This is to ensure that they are not accidentally called by the app code. They are renamed to _create_record and _update_record respectively. Closes #11645
* AestheticRafael Mendonça França2014-01-291-6/+5
|
* Fixing issue with activerecord serialization not being able to dump a record ↵Mauricio Linhares2014-01-291-0/+11
| | | | after loading it from YAML - fixes #13861
* Merge pull request #13799 from kbrock/better_dirtyJeremy Kemper2014-01-222-1/+29
|\ | | | | Better ActiveRecord hierarchy for Dirty and others
| * Move changed_attributes into dirty.rbKeenan Brock2014-01-222-1/+29
| | | | | | Move serialization dirty into serialization.rb
* | Add more tests for the dirty feature for enumsRafael Mendonça França2014-01-211-1/+0
| |
* | Extract all attribute changed work to its own methodRafael Mendonça França2014-01-211-3/+7
|/ | | | This will make easier to hook on this feature to customize the behavior