aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/scoping/relation_scoping_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Association loading isn't to be affected by null relation scopingRyuta Kamizono2019-04-061-0/+24
| | | | | | Follow up of #35868. Closes #19349.
* Association loading isn't to be affected by scoping consistentlyRyuta Kamizono2019-04-051-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Follow-up of 5c71000, #29834, and #30271. Currently, preloading and eager loading are not to be affected by scoping, with the exception of `unscoped`. But non eager loaded association access is still affected by scoping. Although this is a breaking change, the association loading will work consistently whether preloaded / eager loaded or not. Before: ```ruby Post.where("1=0").scoping do Comment.find(1).post # => nil Comment.preload(:post).find(1).post # => #<Post id: 1, ...> Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...> end ``` After: ```ruby Post.where("1=0").scoping do Comment.find(1).post # => #<Post id: 1, ...> Comment.preload(:post).find(1).post # => #<Post id: 1, ...> Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...> end ``` Fixes #34638. Fixes #35398.
* Add Relation#annotate for SQL commentingMatt Yoho2019-03-211-0/+38
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch has two main portions: 1. Add SQL comment support to Arel via Arel::Nodes::Comment. 2. Implement a Relation#annotate method on top of that. == Adding SQL comment support Adds a new Arel::Nodes::Comment node that represents an optional SQL comment and teachers the relevant visitors how to handle it. Comment nodes may be added to the basic CRUD statement nodes and set through any of the four (Select|Insert|Update|Delete)Manager objects. For example: manager = Arel::UpdateManager.new manager.table table manager.comment("annotation") manager.to_sql # UPDATE "users" /* annotation */ This new node type will be used by ActiveRecord::Relation to enable query annotation via SQL comments. == Implementing the Relation#annotate method Implements `ActiveRecord::Relation#annotate`, which accepts a comment string that will be appeneded to any queries generated by the relation. Some examples: relation = Post.where(id: 123).annotate("metadata string") relation.first # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123 # LIMIT 1 /* metadata string */ class Tag < ActiveRecord::Base scope :foo_annotated, -> { annotate("foo") } end Tag.foo_annotated.annotate("bar").first # SELECT "tags".* FROM "tags" LIMIT 1 /* foo */ /* bar */ Also wires up the plumbing so this works with `#update_all` and `#delete_all` as well. This feature is useful for instrumentation and general analysis of queries generated at runtime.
* Fix the scoping with query methods in the scope blockRyuta Kamizono2018-11-301-1/+6
| | | | | | | | | Follow up #33394. #33394 only fixes the case of scoping with klass methods in the scope block which invokes `klass.all`. Query methods in the scope block also need to invoke `klass.all` to be affected by the scoping.
* Don't expose `current_scope` for internal useRyuta Kamizono2018-09-111-2/+2
|
* Avoid extra scoping in delegating to klass methods in the `scope` blockRyuta Kamizono2018-07-191-0/+5
| | | | | | | | | | Since #29301, delegating to klass methods in the `scope` block would cause extra scoping by the receiver itself. The extra scoping would always override intermediate scoping like `unscoped` and caused the regression #33387. To keep the original scoping behavior, should avoid the extra scoping in the `scope` block. Fixes #33387.
* Replace `assert !` with `assert_not`Daniel Colson2018-04-191-1/+1
| | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-10/+10
|
* `scoping` should respect current class and STI constraint (#29199)Ryuta Kamizono2017-11-061-0/+14
| | | | | | | | A relation includes `klass`, so it can not be used as it is if current class is different from `current_scope.klass`. It should be created new relation by current class to respect the klass and STI constraint. Fixes #17603. Fixes #23576.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Fix to scoping is correctly restoredRyuta Kamizono2017-06-291-0/+9
| | | | | | | | This regression was caused by #23004. If STI subclass is using scoping in parent class scoping, `current_scope` in subclass is never restored. I fixed to restore `current_scope` to previous value correctly.
* Fix crashing on circular left join references with scopingRyuta Kamizono2017-05-241-2/+9
| | | | Follow up of #25702.
* Restore `fixtures :author_addresses`Ryuta Kamizono2017-04-271-2/+2
| | | | | This change reverted in eac6f369 but it is needed for data integrity. See #25328.
* Revert "Merge pull request #27636 from ↵Rafael Mendonça França2017-04-261-2/+2
| | | | | | | | | mtsmfm/disable-referential-integrity-without-superuser-privilege-take-2" This reverts commit c1faca6333abe4b938b98fedc8d1f47b88209ecf, reversing changes made to 8c658a0ecc7f2b5fc015d424baf9edf6f3eb2b0b. See https://github.com/rails/rails/pull/27636#issuecomment-297534129
* Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser ↵Fumiaki MATSUSHIMA2017-03-261-2/+2
| | | | | | | | | | | | | | | privileges (take 2) Re-create https://github.com/rails/rails/pull/21233 eeac6151a5 was reverted (127509c071b4) because it breaks tests. ---------------- ref: 72c1557254 - We must use `authors` fixture with `author_addresses` because of its foreign key constraint. - Tests require PostgreSQL >= 9.4.2 because it had a bug about `ALTER CONSTRAINTS` and fixed in 9.4.2.
* Revert "Merge pull request #21233 from ↵Rafael Mendonça França2017-01-031-2/+2
| | | | | | | | | | mtsmfm/disable-referential-integrity-without-superuser-privileges" This reverts commit eeac6151a55cb7d5f799e1ae33aa64a839cbc3aa, reversing changes made to 5c40239d3104543e70508360d27584a3e4dc5baf. Reason: Broke the isolated tests. https://travis-ci.org/rails/rails/builds/188721346
* Merge pull request #21233 from ↵Rafael França2017-01-031-2/+2
|\ | | | | | | | | mtsmfm/disable-referential-integrity-without-superuser-privileges Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser privileges
| * Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser ↵Fumiaki MATSUSHIMA2016-12-031-2/+2
| | | | | | | | | | | | | | | | | | privileges ref: 72c1557254 - We must use `authors` fixture with `author_addresses` because of its foreign key constraint. - Tests require PostgreSQL >= 9.4.2 because it had a bug about `ALTER CONSTRAINTS` and fixed in 9.4.2.
* | "Use assert_nil if expecting nil. This will fail in minitest 6."Akira Matsuda2016-12-251-1/+1
|/
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-1/+1
|
* improve error message when include assertions failMichael Grosser2016-09-161-7/+7
| | | | | | assert [1, 3].includes?(2) fails with unhelpful "Asserting failed" message assert_includes [1, 3], 2 fails with "Expected [1, 3] to include 2" which makes it easier to debug and more obvious what went wrong
* Fix warning: ambiguous first argumentRyuta Kamizono2016-08-121-1/+1
|
* Use `FETCH FIRST` for Oracle12 database and Arel Oracle12 visitorYasuo Honda2016-08-081-1/+1
| | | | | also test `ROWNUM <=` for Oracle 11g or older version of Oracle and Oracle visitor Oracle 12c database and Arel Oracle12 visitor supports better top N query.
* modernizes hash syntax in activerecordXavier Noria2016-08-061-16/+16
|
* applies new string literal convention in activerecord/testXavier Noria2016-08-061-39/+39
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Remove circular join references in join_dependencyTakashi Kokubun2016-07-281-0/+7
| | | | Fixes #25653.
* Active scopes apply to child classes, though not parents/siblingsMatthew Draper2016-01-121-3/+17
| | | | | | | While the commit message (and changelog example) in 5e0b555b453ea2ca36986c111512627d806101e7 talked about sibling classes, the added test had a child ignore its parent's scoping, which seems less reasonable.
* `current_scope` shouldn't pollute sibling STI classesSean Griffin2015-02-111-0/+6
| | | | | | | | | | | | It looks like the only reason `current_scope` was thread local on `base_class` instead of `self` is to ensure that when we call a named scope created with a proc on the parent class, it correctly uses the default scope of the subclass. The reason this wasn't happening was because the proc captured `self` as the parent class, and we're not actually defining a real method. Using `instance_exec` fixes the problem. Fixes #18806
* `WhereClause#predicates` does not need to be publicSean Griffin2015-01-271-1/+1
| | | | | | | | | | | The only place it was accessed was in tests. Many of them have another way that they can test their behavior, that doesn't involve reaching into internals as far as they did. `AssociationScopeTest` is testing a situation where the where clause would have one bind param per predicate, so it can just ignore the predicates entirely. The where chain test was primarly duplicating the logic tested on `WhereClause` directly, so I instead just make sure it calls the appropriate method which is fully tested in isolation.
* Remove all references to `where_values` in testsSean Griffin2015-01-251-1/+1
|
* Build fix when running in isolationArun Agrawal2014-11-141-0/+1
| | | | | `Computer` class needs to be require See #17217 for more details
* :scissors: duplicated `require`sGodfrey Chan2014-10-141-1/+0
|
* make sure cache is not used for collection assocations tooAaron Patterson2014-10-141-1/+12
| | | | follow up for #17052
* break cache if we're inside a "scoping" call. fixes #17052Aaron Patterson2014-10-141-0/+10
| | | | | For now, we don't want to take "scoping" calls in to account when calculating cache keys for relations, so just opt-out.
* Make sure that fixtures are loaded before findingAkira Matsuda2014-08-141-0/+4
|
* FormatAkira Matsuda2014-08-141-2/+2
|
* do not rely on method_missing hitting arelAaron Patterson2014-03-241-2/+3
| | | | arel methods are not supposed to be public API for ActiveRecord
* split relation_scoping_test.rb's default scoping tests into another fileTakehiro Adachi2013-03-301-357/+0
|
* move tests for NamedScope and DefaultScope under test/cases/scoping/Takehiro Adachi2013-03-301-0/+688
The scoping/default.rb and scoping/named.rb got moved under scoping/ in commit 2b22564c4efaa63d4bbc006762838c4025c1bdca, but the tests never did.