aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/scoping
Commit message (Collapse)AuthorAgeFilesLines
* Skip the STI condition when evaluating a default scopeMatthew Draper2016-01-121-0/+14
| | | | | | | | | | | | | 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.
* 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.
* Removed mocha from Active Record Part 1Ronak Jangir2015-08-251-4/+6
|
* Merge pull request #19770 from vngrs/prevent_duplicated_where_clausesRafael Mendonça França2015-08-131-0/+6
|\ | | | | Prevent duplicating `where` clauses
| * Prevent duplicating `where` clauses when model is extended from an abstract ↵Mehmet Emin İNAÇ2015-04-151-0/+6
| | | | | | | | | | | | | | | | | | | | class Fixes #19528 fix for mysql2 test better test
* | Fix unscope for less thanTAKAHASHI Kazuaki2015-03-271-0/+12
|/ | | | | | Code such as the following will be corrected. Developer.where(id: -Float::INFINITY...2).unscope(where: :id)
* Error message testing fixFranky W2015-02-201-2/+4
| | | | | | | | | The testing of error messages have been implemented wrongly a few times. This is an attempt to fix it. For example, some of these test should have failed with the new code. The reason they are not failling with the new string is the fact they were not being tested beforehand.
* `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-273-7/+7
| | | | | | | | | | | 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-253-7/+7
|
* Remove all cases of manuallly wrapping `Arel::Nodes::Quoted`Sean Griffin2014-12-291-4/+2
| | | | | | | | | | This is no longer required now that we are injecting a type caster object into the Arel table, with the exception of uniqueness validations. Since it calls `ConnectionAdapter#type_cast`, the value has already been cast for the database. We don't want Arel to attempt to cast it further, so we need to continue wrapping it in a quoted node. This can potentially go away when this validator is refactored to make better use of `where` or the predicate builder.
* Inform Arel that we don't need to cast a value in testsSean Griffin2014-12-261-2/+4
| | | | | | | Part of the larger refactoring to remove type casting from Arel. We can inform it that we already have the right type by wrapping the value in an `Arel::Nodes::Quoted`. This commit can be reverted when we have removed type casting from Arel in Rail 5.1
* Allow to unscope where conditions using `arel_table` with Symboldeeeki2014-12-031-0/+8
| | | | | | This commit fixes the following case. User.where(User.arel_table[:created_at].lteq(1.year.ago)).unscope(where :created_at)
* Pass symbol as an argument instead of a blockErik Michaels-Ober2014-11-291-32/+32
|
* Build fix when running in isolationArun Agrawal2014-11-143-0/+3
| | | | | `Computer` class needs to be require See #17217 for more details
* Merge pull request #17374 from maurogeorge/scope-exceptionYves Senn2014-10-271-0/+7
|\ | | | | | | Raises ArgumentError when try to define a scope without a callable
| * Raises ArgumentError when try to define a scope without a callableMauro George2014-10-231-0/+6
|/ | | | | | This changes the actual exception `NoMethodError: undefined method `call' for #<ActiveRecord::Relation []>` to a `ArgumentError` when try to define a scope without a callable.
* :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.
* Skip StatementCache for eager loaded associations (Fixes #16761)Sammy Larbi2014-09-041-1/+20
| | | | | | Eagerly loaded collection and singular associations are ignored by the StatementCache, which causes errors when the queries they generate reference columns that were not eagerly loaded. This commit skips the creation of the StatementCache as a fix for these scenarios.
* Enums shouldn't ruin people's anniversariesGodfrey Chan2014-09-041-2/+5
| | | | | | | | Added a few more methods on Module/Class to the dangerous class methods blacklist. (Technically, allocate and new are already protected currently because we happen to redefine them in the current implantation.) Closes #16792
* Make sure that fixtures are loaded before findingAkira Matsuda2014-08-141-0/+4
|
* FormatAkira Matsuda2014-08-141-2/+2
|
* Revert "Merge pull request #14544 from jefflai2/named_scope_sti"Rafael Mendonça França2014-05-211-6/+1
| | | | | | | | | | | | This reverts commit 9a1abedcdeecd9464668695d4f9c1d55a2fd9332, reversing changes made to c72d6c91a7c0c2dc81cc857a1d6db496e84e0065. Conflicts: activerecord/CHANGELOG.md activerecord/test/models/comment.rb This change break integration with activerecord-deprecated_finders so I'm reverting until we find a way to make it work with this gem.
* Merge pull request #14544 from jefflai2/named_scope_stiRafael Mendonça França2014-05-201-1/+6
|\ | | | | | | | | | | | | Fixes Issue #13466. Conflicts: activerecord/CHANGELOG.md
| * Fixes Issue #13466.Jefferson Lai2014-04-231-1/+6
| | | | | | | | | | | | Changed the call to a scope block to be evaluated with instance_eval. The result is that ScopeRegistry can use the actual class instead of base_class when caching scopes so queries made by classes with a common ancestor won't leak scopes.
* | Block a few default Class methods as scope name.Arthur Neves2014-04-031-0/+3
|/ | | | | | Add tests to make sure scopes cannot be create with names such as: private, protected, public. Make sure enum values don't collide with those methods too.
* do not rely on method_missing hitting arelAaron Patterson2014-03-241-2/+3
| | | | arel methods are not supposed to be public API for ActiveRecord
* Add tests for default scope behaviour changeJon Leighton2014-02-231-0/+18
| | | | See #13875
* Remove warnings for already defined methodsRafael Mendonça França2014-02-011-1/+3
|
* Let `unscope` ignore non Arel scope.where_valuesWashington Luiz2014-01-301-0/+10
|
* `scope` now raises on "dangerous" name conflictsGodfrey Chan2014-01-291-0/+57
| | | | | | | | | | Similar to dangerous attribute methods, a scope name conflict is dangerous if it conflicts with an existing class method defined within `ActiveRecord::Base` but not its ancestors. See also #13389. *Godfrey Chan*, *Philippe Creux*
* Ensure #second acts like #first AR finderJason Meller2014-01-201-4/+4
| | | | | | | | | | | | This commit bring the famous ordinal Array instance methods defined in ActiveSupport into ActiveRecord as fully-fledged finders. These finders ensure a default ascending order of the table's primary key, and utilize the OFFSET SQL verb to locate the user's desired record. If an offset is defined in the query, calling #second adds to the offset to get the actual desired record. Fixes #13743.
* Fix bad usage of #select with hashCarlos Antonio da Silva2013-12-031-1/+1
|
* Fix ActiveRecord::Relation#unscopeJon Leighton2013-11-201-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I'm pretty confused about the addition of this method. The documentation says that it was intended to allow the removal of values from the default scope (in contrast to #except). However it behaves exactly the same as except: https://gist.github.com/jonleighton/7537008 (other than having a slightly enhanced syntax). The removal of the default scope is allowed by 94924dc32baf78f13e289172534c2e71c9c8cade, which was not a change we could make until 4.1 due to the need to deprecate things. However after that change #unscope still gives us nothing that #except doesn't already give us. However there *is* a desire to be able to unscope stuff in a way that persists across merges, which would allow associations to be defined which unscope stuff from the default scope of the associated model. E.g. has_many :comments, -> { unscope where: :trashed } So that's what this change implements. I've also corrected the documentation. I removed the guide references to #except as I think unscope really supercedes #except now. While we're here, there's also a potential desire to be able to write this: has_many :comments, -> { unscoped } However, it doesn't make sense and would not be straightforward to implement. While with #unscope we're specifying exactly what we want to be removed from the relation, with "unscoped" we're just saying that we want it to not have some things which were added earlier on by the default scope. However in the case of an association, we surely don't want *all* conditions to be removed, otherwise the above would just become "SELECT * FROM comments" with no foreign key constraint. To make the above work, we'd have to somehow tag the relation values which get added when evaluating the default scope in order to differentiate them from other relation values. Which is way too much complexity and therefore not worth it when most use cases can be satisfied with unscope. Closes #10643, #11061.
* Don't skip tests if we don't need to.Rafael Mendonça França2013-11-081-25/+23
| | | | | | | We can conditional define the tests depending on the adapter or connection. Lets keep the skip for fail tests that need to be fixed.
* unscope now works on default_scope after ↵Rafael Mendonça França2013-11-021-1/+1
| | | | 94924dc32baf78f13e289172534c2e71c9c8cade
* remove trailing whitespace added with b057765 [ci skip].Yves Senn2013-10-211-2/+2
|
* Allow unscope to work with `where.not`Eric Hankins2013-10-211-6/+14
| | | | | | Allows you to call #unscope on a relation with negative equality operators, i.e. Arel::Nodes::NotIn and Arel::Nodes::NotEqual that have been generated through the use of where.not.
* asakusa.rb hack night!Aaron Patterson + Akira Matsuda2013-08-061-0/+2
| | | | Fix in-memory tests
* Revert change on ActiveRecord::Relation#order method that prepends newRafael Mendonça França2013-07-291-4/+4
| | | | | | | | | | | | | | | order on the old ones The previous behavior added a major backward incompatibility since it impossible to have a upgrade path without major changes on the application code. We are taking the most conservative path to be consistent with the idea of having a smoother upgrade on Rails 4. We are reverting the behavior for what was in Rails 3.x and, if needed, we will implement a new API to prepend the order clauses in Rails 4.1.
* close our connection when we are doneAaron Patterson2013-07-081-1/+6
|
* Remove deprecated `scope` use without passing a callable object.Arun Agrawal2013-07-031-10/+0
| | | | Removed tests from deprecated code.
* calling default_scope without a proc will raise ArgumentErrorNeeraj Singh2013-07-021-3/+2
| | | | Calling default_scope without a proc will now raise `ArgumentError`.
* remove deprecated implicit join references.Yves Senn2013-06-291-5/+0
|
* Simplify/fix implementation of default scopesJon Leighton2013-06-281-3/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The previous implementation was necessary in order to support stuff like: class Post < ActiveRecord::Base default_scope where(published: true) scope :ordered, order("created_at") end If we didn't evaluate the default scope at the last possible moment before sending the SQL to the database, it would become impossible to do: Post.unscoped.ordered This is because the default scope would already be bound up in the "ordered" scope, and therefore wouldn't be removed by the "Post.unscoped" part. In 4.0, we have deprecated all "eager" forms of scopes. So now you must write: class Post < ActiveRecord::Base default_scope { where(published: true) } scope :ordered, -> { order("created_at") } end This prevents the default scope getting bound up inside the "ordered" scope, which means we can now have a simpler/better/more natural implementation of default scoping. A knock on effect is that some things that didn't work properly now do. For example it was previously impossible to use #except to remove a part of the default scope, since the default scope was evaluated after the call to #except.
* Delegate #unscope query methodCarlos Antonio da Silva2013-04-281-0/+10
|
* Fix scope chaining + STIJon Leighton2013-04-051-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | See #9869 and #9929. The problem arises from the following example: class Project < ActiveRecord::Base scope :completed, -> { where completed: true } end class MajorProject < Project end When calling: MajorProject.where(tasks_count: 10).completed This expands to: MajorProject.where(tasks_count: 10).scoping { MajorProject.completed } However the lambda for the `completed` scope is defined on Project. This means that when it is called, `self` is Project rather than MajorProject. So it expands to: MajorProject.where(tasks_count: 10).scoping { Project.where(completed: true) } Since the scoping was applied on MajorProject, and not Project, this fails to apply the tasks_count condition. The solution is to make scoping apply across STI classes. I am slightly concerned about the possible side-effects of this, but no tests fail and it seems ok. I guess we'll see.
* failing test for #9869Neeraj Singh2013-04-051-0/+5
|