aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/models
Commit message (Collapse)AuthorAgeFilesLines
* Deprecate the behavior of AR::Dirty inside of after_(create|update|save) ↵Sean Griffin2016-11-011-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | callbacks We pretty frequently get bug reports that "dirty is broken inside of after callbacks". Intuitively they are correct. You'd expect `Model.after_save { puts changed? }; model.save` to do the same thing as `model.save; puts model.changed?`, but it does not. However, changing this goes much farther than just making the behavior more intuitive. There are a _ton_ of places inside of AR that can be drastically simplified with this change. Specifically, autosave associations, timestamps, touch, counter cache, and just about anything else in AR that works with callbacks have code to try to avoid "double save" bugs which we will be able to flat out remove with this change. We introduce two new sets of methods, both with names that are meant to be more explicit than dirty. The first set maintains the old behavior, and their names are meant to center that they are about changes that occurred during the save that just happened. They are equivalent to `previous_changes` when called outside of after callbacks, or once the deprecation cycle moves. The second set is the new behavior. Their names imply that they are talking about changes from the database representation. The fact that this is what we really care about became clear when looking at `BelongsTo.touch_record` when tests were failing. I'm unsure that this set of methods should be in the public API. Outside of after callbacks, they are equivalent to the existing methods on dirty. Dirty itself is not deprecated, nor are the methods inside of it. They will only emit the warning when called inside of after callbacks. The scope of this breakage is pretty large, but the migration path is simple. Given how much this can improve our codebase, and considering that it makes our API more intuitive, I think it's worth doing.
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-294-15/+15
|
* Don't skip in-memory insertion of associations when loaded in validateSean Griffin2016-09-291-6/+0
| | | | | | | | | | | | | | | | This was caused by 6d0d83a33f59d9415685852cf77818c41e2e2700. While the bug it's trying to fix is handled if the association is loaded in an after_(create|save) callback, it doesn't handle any cases that load the association before the persistence takes place (validation, or before_* filters). Instead of caring about the timing of persistence, we can just ensure that we're not double adding the record instead. The test from that commit actually broke, but it was not because the bug has been re-introduced. It was because `Bulb` in our test suite is doing funky things that look like STI but isn't STI, so equality comparison didn't happen as the loaded model was of a different class. Fixes #26661.
* Add tests for ActiveRecord::Enum#enum when suffix specifiedYosuke Kabuto2016-09-121-0/+1
| | | | Make name of attribute medium instead of normal
* Merge pull request #24099 from k0kubun/preserve-readonlyRafael Mendonça França2016-08-181-0/+1
|\ | | | | | | Preserve readonly flag only for readonly association
| * Preserve readonly flag only for readonly associationTakashi Kokubun2016-07-301-0/+1
| | | | | | | | Fixes #24093
* | Add three new rubocop rulesRafael Mendonça França2016-08-1610-32/+32
| | | | | | | | | | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* | code gardening: removes redundant selfsXavier Noria2016-08-082-2/+2
| | | | | | | | | | | | | | | | | | A few have been left for aesthetic reasons, but have made a pass and removed most of them. Note that if the method `foo` returns an array, `foo << 1` is a regular push, nothing to do with assignments, so no self required.
* | Add `Style/EmptyLines` in `.rubocop.yml` and remove extra empty linesRyuta Kamizono2016-08-071-1/+0
| |
* | applies remaining conventions across the projectXavier Noria2016-08-068-13/+4
| |
* | normalizes indentation and whitespace across the projectXavier Noria2016-08-068-27/+26
| |
* | remove redundant curlies from hash argumentsXavier Noria2016-08-061-1/+1
| |
* | modernizes hash syntax in activerecordXavier Noria2016-08-0662-536/+535
| |
* | applies new string literal convention in activerecord/testXavier Noria2016-08-0658-307/+307
| | | | | | | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* | Don't assume all hashes are from multiparameter assignment in `composed_of`Sean Griffin2016-08-051-0/+1
|/ | | | | | | | | | | | | | | | | | | | | | | | So this bug is kinda funky. The code path is basically "if we weren't passed an instance of the class we compose to, and we have a converter, call that". Ignoring the hash case for a moment, everything after that was roughly intended to be the "else" clause, meaning that we are expected to have an instance of the class we compose to. Really, we should be blowing up in that case, as we can give a much better error message than what they user will likely get (e.g. `NameError: No method first for String` or something). Still, Ruby is duck typed, so if the object you're assigning responds to the same methods as the type you compose to, knock yourself out. The hash case was added in 36e9be8 to remove a bunch of special cased code from multiparameter assignment. I wrongly assumed that the only time we'd get a hash there is in that case. Multiparameter assignment will construct a very specific hash though, where the keys are integers, and we will have a set of keys covering `1..part.size` exactly. I'm pretty sure this could actually be passed around as an array, but that's a different story. Really I should convert this to something like `class MultiParameterAssignment < Hash; end`, which I might do soon. However for a change that I'm willing to backport to 4-2-stable, this is what I want to go with for the time being. Fixes #25978
* Merge pull request #25767 from ↵Rafael França2016-07-271-0/+6
|\ | | | | | | | | kamipo/association_name_is_the_same_as_join_table_name Correctly return `associated_table` when `associated_with?` is true
| * Correctly return `associated_table` when `associated_with?` is trueRyuta Kamizono2016-07-101-0/+6
| | | | | | | | | | | | | | `AssociationQueryHandler` requires `association` initialized `TableMetadata` even if `table_name == arel_table.name`. Fixes #25689.
* | Oracle TIMESTAMP sql type is associated with Rails `DateTime` type nowYasuo Honda2016-07-201-6/+0
|/ | | | | | - Refer https://github.com/rsim/oracle-enhanced/pull/845 Remove `set_date_columns` which has been deprecated in Oracle enhanced adapter - Refer https://github.com/rsim/oracle-enhanced/pull/869
* Ensure hashes can be passed to attributes using `composed_of`Sean Griffin2016-05-311-1/+6
| | | | | | | | | | | This behavior was broken by 36e9be85. When the value is assigned directly, either through mass assignment or directly assigning a hash, the hash gets passed through to this writer method directly. While this is intended to handle certain cases, when an explicit converter has been provided, we should continue to use that instead. The positioning of the added guard caused the new behavior to override that case. Fixes #25210
* Fix undefined method `owners' for NullPreloader:ClassLadislav Smola2016-04-063-1/+11
| | | | | | | | | | | | | | * Fix undefined method `owners' for NullPreloader:Class Fixing undefined method `owners' for ActiveRecord::Associations::Preloader::NullPreloader:Class * Use Ruby 1.9 hash format Use Ruby 1.9 hash format #24192 [Rafael Mendonça França + Ladislav Smola]
* Fix `warning: method redefined; discarding old female`Ryuta Kamizono2016-03-121-3/+0
| | | | | | | | | | | ``` $ ARCONN=mysql2 be ruby -w -Itest test/cases/scoping/default_scoping_test.rb Using mysql2 /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/scoping/named.rb:158: warning: method redefined; discarding old female /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/scoping/named.rb:158: warning: previous definition of female was here /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/scoping/named.rb:158: warning: method redefined; discarding old male /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/scoping/named.rb:158: warning: previous definition of male was here ```
* Execute default_scope defined by abstract class within the scope of subclassMehmet Emin İNAÇ2016-03-081-0/+13
|
* don't treat all associations with extensions as instance dependent.Yves Senn2016-03-031-0/+8
| | | | | | | | | | Closes #23934. This is a forward port of ac832a43b4d026dbad28fed196d2de69ec9928ac Previously the scope of all associations with extensions were wrapped in an instance dependent proc. This made it impossible to preload such associations.
* Merge pull request #18766 from yasyf/issue_17864Sean Griffin2016-02-291-0/+6
|\ | | | | | | | | Honour joining model order in `has_many :through` associations when eager loading
| * Honour the order of the joining model in a `has_many :through`Yasyf Mohamedali2015-03-021-0/+6
| | | | | | | | | | | | | | | | | | association when eager loading. Previously, eager loading a `has_many :through` association with no defined order would return the records in the natural order of the database. Now, these records will be returned in the order that the joining record is returned, in case there is a defined order there.
* | Ensure suppressor runs before validationseileencodes2016-02-241-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I ran into an issue where validations on a suppressed record were causing validation errors to be thrown on a record that was never going to be saved. There isn't a reason to run the validations on a record that doesn't matter. This change moves the suppressor up the chain to be run on the `save` or `save!` in the validations rather than in persistence. The issue with running it when we hit persistence is that the validations are run first, then we hit persistance, and then we hit the suppressor. The suppressor comes first. The change to the test was required since I added the `validates_presence_of` validations. Adding this alone was enough to demonstrate the issue. I added a new test to demonstrate the new behavior is explict.
* | Merge pull request #23628 from maclover7/fix-23625Sean Griffin2016-02-231-0/+6
|\ \ | | | | | | Fix issue #23625
| * | Fix issue #23625Jon Moss2016-02-181-0/+6
| | | | | | | | | | | | | | | | | | This resolves a bug where if the primary key used is not `id` (ex: `uuid`), and has a `validates_uniqueness_of` in the model, a uniqueness error would be raised. This is a partial revert of commit `119b9181ece399c67213543fb5227b82688b536f`, which introduced this behavior.
* | | Fixed `where` for polymorphic associations when passed an array containing ↵Philippe Huibonhoa2016-02-161-0/+2
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | different types. When passing in an array of different types of objects to `where`, it would only take into account the class of the first object in the array. PriceEstimate.where(estimate_of: [Treasure.find(1), Car.find(2)]) # => SELECT "price_estimates".* FROM "price_estimates" WHERE ("price_estimates"."estimate_of_type" = 'Treasure' AND "price_estimates"."estimate_of_id" IN (1, 2)) This is fixed to properly look for any records matching both type and id: PriceEstimate.where(estimate_of: [Treasure.find(1), Car.find(2)]) # => SELECT "price_estimates".* FROM "price_estimates" WHERE (("price_estimates"."estimate_of_type" = 'Treasure' AND "price_estimates"."estimate_of_id" = 1) OR ("price_estimates"."estimate_of_type" = 'Car' AND "price_estimates"."estimate_of_id" = 2))
* | Merge pull request #18109 from k0kubun/unscoped-joinsSean Griffin2016-02-111-0/+1
|\ \ | | | | | | | | | | | | | | | Allow `joins` to be unscoped Fixes #13775
| * | Allow `joins` to be unscopedTakashi Kokubun2016-01-311-0/+1
| | |
* | | Typos in AR testsAkira Matsuda2016-02-031-1/+1
|/ /
* | Add missing source_type if provided on hmt which belongs to an sti recordVipul A M2016-01-243-0/+10
| | | | | | | | Fixes #23209
* | Use the database type to deserialize enumSean Griffin2016-01-231-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | This fixes incorrect assumptions made by e991c7b that we can assume the DB is already casting the value for us. The enum type needs additional information to perform casting, and needs a subtype. I've opted not to call `super` in `cast`, as we have a known set of types which we accept there, and the subtype likely doesn't accept them (symbol -> integer doesn't make sense) Close #23190
* | Revert "Merge pull request #20835 from ↵Kasper Timm Hansen2016-01-141-3/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | glittershark/if-and-unless-in-secure-token" This reverts commit 224eddfc0eeff6555ae88691306e61c7a9e8b758, reversing changes made to 9d681fc74c6251d5f2b93fa9576c9b2113116680. When merging the pull request, I misunderstood `has_secure_token` as declaring a model has a token from birth and through the rest of its lifetime. Therefore, supporting conditional creation doesn't make sense. You should never mark a model as having a secure token if there's a time when it shouldn't have it on creation.
* | Skip the STI condition when evaluating a default scopeMatthew Draper2016-01-121-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | Given a default_scope on a parent of the current class, where that parent is not the base class, the parent's STI condition would become attached to the evaluated default scope, and then override the child's own STI condition. Instead, we can treat the STI condition as though it is a default scope, and skip it in this situation: the scope will be merged into the base relation, which already contains the correct STI condition. Fixes #22426.
* | Support :if and :unless in has_secure_tokenGriffin Smith2016-01-091-0/+3
| | | | | | | | | | Pass through :if and :unless options from has_secure_token to the generated before_create callback
* | Bugfix collection association #create method …Bogdan Gusiev2015-11-231-0/+6
| | | | | | | | | | When same association is loaded in the model creation callback The new object is inserted into association twice
* | Except keys of `build_record`'s argument from `create_scope` in ↵yui-knk2015-11-162-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | initialize_attributes If argument of `build_record` has key and value which is same as default value of database, we should also except the key from `create_scope` in `initialize_attributes`. Because at first `build_record` initialize record object with argument of `build_record`, then assign attributes derived from Association's scope. In this case `record.changed` does not include the key, which value is same as default value of database, so we should add the key to except list. Fix #21893.
* | Ensure `has_and_belongs_to_many` works with `belongs_to_required_by_default`Sean Griffin2015-10-291-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | Before this commit, if `ActiveRecord::Base.belongs_to_required_by_default` is set to `true`, then creating a record through `has_and_belongs_to_many` fails with the cryptic error message `Left side must exist`. This is because `inverse_of` isn't working properly in this case, presumably since we're doing trickery with anonymous classes in the middle. Rather than following this rabbit hole to try and get `inverse_of` to work in a case that we know is not publicly supported, we can just turn off this validation to match the behavior of 4.2 and earlier.
* | Fix merge conflicts from #19501Sean Griffin2015-10-291-0/+7
|\ \ | | | | | | | | | | | | | | | | | | | | | I'm making this commit separately because this has failing tests and style nitpicks that I'd like to make as individual commits, to make the changes I'm making explicit. We still want a single merge commit at the end, however.
| * | DRY up STI subclass logicCody Cutrer2015-03-241-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | the newer method used for discriminating new records did not use the older and more robust method used for instantiating existing records, but did have a better post-check to ensure the sublass was in the hierarchy. so move the descendants check to find_sti_class, and then simply call find_sti_class from subclass_from_attributes now with fixed specs
* | | Merge pull request #18548 from ↵Sean Griffin2015-10-281-0/+40
|\ \ \ | | | | | | | | | | | | | | | | | | | | sebjacobs/support-bidirectional-destroy-dependencies Add support for bidirectional destroy dependencies
| * | | Add support for bidirectional destroy dependenciesSeb Jacobs2015-01-161-0/+40
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Prior to this commit if you defined a bidirectional relationship between two models with destroy dependencies on both sides, a call to `destroy` would result in an infinite callback loop. Take the following relationship. class Content < ActiveRecord::Base has_one :content_position, dependent: :destroy end class ContentPosition < ActiveRecord::Base belongs_to :content, dependent: :destroy end Calling `Content#destroy` or `ContentPosition#destroy` would result in an infinite callback loop. This commit changes the behaviour of `ActiveRecord::Callbacks#destroy` so that it guards against subsequent callbacks. Thanks to @zetter for demonstrating the issue with failing tests[1]. [1] rails#13609
* | | | Revert "Merge pull request #21994 from mtodd/inherit-scopes"Rafael Mendonça França2015-10-271-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit 60c9701269f5b412849f1a507df61ba4735914d7, reversing changes made to 6a25202d9ea3b4a7c9f2d6154b97cf8ba58403db. Reason: Broken build
* | | | Make inherited scope test failMatt Todd2015-10-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This triggers the JoinDependency work to reflect on the associations and trigger an error as follows: ActiveRecord::ConfigurationError: Association named 'account' was not found on Company; perhaps you misspelled it? Fix Company.of_first_firm joins association name Should be `Company.joins(:accounts)` not `Company.joins(:account)`. Do the same for Client.of_first_firm
* | | | Merge pull request #19686 from tsun1215/index_errorsSean Griffin2015-10-262-0/+8
|\ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | Errors can be indexed with nested attributes Close #8638
| * | | | Errors can be indexed with nested attributesMichael Probber2015-04-172-0/+8
| | |/ / | |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `has_many` can now take `index_errors: true` as an option. When this is enabled, errors for nested models will be returned alongside an index, as opposed to just the nested model name. This option can also be enabled (or disabled) globally through `ActiveRecord::Base.index_nested_attribute_errors` E.X. ```ruby class Guitar < ActiveRecord::Base has_many :tuning_pegs accepts_nested_attributes_for :tuning_pegs end class TuningPeg < ActiveRecord::Base belongs_to :guitar validates_numericality_of :pitch end ``` - Old style - `guitar.errors["tuning_pegs.pitch"] = ["is not a number"]` - New style (if defined globally, or set in has_many_relationship) - `guitar.errors["tuning_pegs[1].pitch"] = ["is not a number"]` [Michael Probber, Terence Sun]
* | | | Refactor Calculations#execute_grouped_calculation and clean AR test caseRafael Sales2015-10-221-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * When tried to use `Company#accounts` test/models/company.rb I got: ``` ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: accounts.company_id: SELECT COUNT(*) AS count_all, "companies"."firm_id" AS companies_firm_id FROM "companies" INNER JOIN "accounts" ON "accounts"."company_id" = "companies"."id" GROUP BY "companies"."firm_id" ``` * The refactor on Calculations class was just to simplify the code
* | | | Fix merge conflicts for #19938Sean Griffin2015-10-203-0/+6
|\ \ \ \ | | | | | | | | | | | | | | | | | | | | This is a separate commit, as it is not just a changelog conflict. Want to point out the changes in the code