aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/scoping
Commit message (Collapse)AuthorAgeFilesLines
...
* Prevent duplicating `where` clauses when model is extended from an abstract ↵Mehmet Emin İNAÇ2015-04-151-0/+1
| | | | | | | | | | class Fixes #19528 fix for mysql2 test better test
* Fix eager loading association using default_scope for finder methods.Santosh Wadghule2015-03-311-1/+1
| | | | | | - Eager loading was not working for the default_scope (class method) for 'find' & 'find_by' methods. - Fixed these by adding a new check 'respond_to?(:default_scope)'.
* Isolate access to .default_scopes in ActiveRecord::Scoping::DefaultBen Woosley2015-03-122-11/+5
| | | | | | | | | | | Instead use .scope_attributes? consistently in ActiveRecord to check whether there are attributes currently associated with the scope. Move the implementation of .scope_attributes? and .scope_attributes to ActiveRecord::Scoping because they don't particularly have to do specifically with Named scopes and their only dependency, in the case of .scope_attributes?, and only caller, in the case of .scope_attributes is contained in Scoping.
* `current_scope` shouldn't pollute sibling STI classesSean Griffin2015-02-111-4/+13
| | | | | | | | | | | | 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
* Stoping using Relation#merge in default_scopedRafael Mendonça França2015-02-061-1/+7
| | | | | | | | Relation#merge checks if the argument is an array and the only possible returns of build_default_scope is nil or a Relation. Doing this we can raise an ArgumentError when Relation#merge receive a nil value.
* Follow the coding conventionsAndrey Deryabin2014-11-121-1/+1
|
* Merge pull request #17374 from maurogeorge/scope-exceptionYves Senn2014-10-271-0/+4
|\ | | | | | | 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/+3
|/ | | | | | This changes the actual exception `NoMethodError: undefined method `call' for #<ActiveRecord::Relation []>` to a `ArgumentError` when try to define a scope without a callable.
* Revert "Merge pull request #14544 from jefflai2/named_scope_sti"Rafael Mendonça França2014-05-211-6/+2
| | | | | | | | | | | | 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-2/+6
|\ | | | | | | | | | | | | Fixes Issue #13466. Conflicts: activerecord/CHANGELOG.md
| * Fixes Issue #13466.Jefferson Lai2014-04-231-2/+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.
* | Merge pull request #14154 from al2o3cr/issue12770Aaron Patterson2014-03-271-3/+3
|\ \ | |/ |/| Pass a base relation to build_default_scope when joining
| * Pass a base relation to build_default_scope when joiningMatt Jones2014-02-211-3/+3
| | | | | | | | | | This allows the default scope to be built using the current table alias. Resolves #12770
* | fix typo app -> alldmathieu2014-03-071-1/+1
| | | | | | | | Thank you @bquorning
* | unscope doesn't remove only the default_scope, but all of them.dmathieu2014-03-071-4/+5
|/ | | | | [ci-skip] Closes rails/rails#14294
* `scope` now raises on "dangerous" name conflictsGodfrey Chan2014-01-291-0/+6
| | | | | | | | | | 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*
* Don't need to check if the scope respond to callRafael Mendonça França + Kassio Borges2013-08-311-5/+1
| | | | | We are checking this when defining the default scope and raising an ArgumentError
* Remove deprecated branch on the scope method.Rafael Mendonça França2013-08-051-6/+2
| | | | The deprecation message was removed on 50cbc03d18c5984347965a94027879623fc44cce but the code was not.
* Remove deprecated `scope` use without passing a callable object.Arun Agrawal2013-07-031-13/+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`.
* Removed deprecated method default_scopes?Neeraj Singh2013-07-011-8/+0
|
* Simplify/fix implementation of default scopesJon Leighton2013-06-281-10/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Delegating the value getter and setters in the ScopeRegistry to thewangjohn2013-04-081-2/+2
| | | | current ScopeRegister object.
* Grouping thread locals in the ActiveRecord scopes so that thewangjohn2013-04-081-2/+2
| | | | | current_scope and ignore_default_scope locals are brought together under a registry object.
* Deprecate ActiveRecord#Base.default_scopes?Agis-2013-04-061-1/+10
| | | | See #10107.
* Fix scope chaining + STIJon Leighton2013-04-051-7/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* unscoped works with named scope [ci skip]Neeraj Singh2013-03-231-8/+0
| | | | | Update comment to reflect that unscoped works with named scope even when named scope is using without block form
* Update test name to reflect change in how scopes merging worksCarlos Antonio da Silva2013-03-081-1/+0
| | | | | | | Introduced in f1082b8588a9144eedb34d511f0074031f692d98. Full changelog in 9f007d7fe5f90257c71baa2c4e7c76fb44512986. [ci skip]
* Combine scope conditions using ANDNeeraj Singh and John Leighton2013-03-071-3/+13
| | | | | | | | | | | | | Currently Post.active.inactive will result in Post.inactive since the last where clause wins when scopes are merged. This pull request will merge all scopes ( barring defaul scope) using AND. The default scope will be overridden if another scope acts on the same where clause. closes #7365
* Refactor to use each_key, remove extra spacesCarlos Antonio da Silva2013-01-281-3/+1
|
* Fix named scope + class method exampleCarlos Antonio da Silva2013-01-071-3/+1
| | | | Closes #8804 [ci skip]
* Replace comments' non-breaking spaces with spacesclaudiob2012-12-041-2/+2
| | | | | | | | | | Sometimes, on Mac OS X, programmers accidentally press Option+Space rather than just Space and don’t see the difference. The problem is that Option+Space writes a non-breaking space (0XA0) rather than a normal space (0x20). This commit removes all the non-breaking spaces inadvertently introduced in the comments of the code.
* Make caller attribute in deprecation methods optionalAlexey Gaziev2012-10-302-4/+2
|
* Provide a call stack for deprecation warnings where needed.Nikita Afanasenko2012-10-292-2/+4
| | | | It's sometimes hard to quickly find where deprecated call was performed, especially in case of migrating between Rails versions. So this is an attempt to improve the call stack part of the warning message by providing caller explicitly.
* update AR::Scoping documentation [ci skip]Francesco Rodriguez2012-09-212-91/+77
|
* load active_support/deprecation in active_support/railsXavier Noria2012-08-022-2/+0
|
* load active_support/core_ext/class/attribute in active_support/railsXavier Noria2012-08-021-1/+0
|
* load active_support/concern in active_support/railsXavier Noria2012-08-021-1/+0
|
* load active_support/core_ext/object/blank in active_support/railsXavier Noria2012-08-021-1/+0
|
* Deprecate ActiveRecord::Base.scoped.Jon Leighton2012-07-271-5/+1
| | | | | | | It doesn't serve much purpose now that ActiveRecord::Base.all returns a Relation. The code is moved to active_record_deprecated_finders.
* ActiveRecord::Base.all returns a Relation.Jon Leighton2012-07-271-11/+8
| | | | | | | | | | | Previously it returned an Array. If you want an array, call e.g. `Post.to_a` rather than `Post.all`. This is more explicit. In most cases this should not break existing code, since Relations use method_missing to delegate unknown methods to #to_a anyway.
* remove :nodoc: of AR::Scoping#unscoped [ci skip]Francesco Rodriguez2012-07-011-4/+4
|
* Simplify AR configuration code.Jon Leighton2012-06-151-1/+1
| | | | | Get rid of ActiveModel::Configuration, make better use of ActiveSupport::Concern + class_attribute, etc.
* extract deprecated codeJon Leighton2012-04-251-4/+2
|
* Extract deprecated codeJon Leighton2012-04-251-9/+8
|
* extract to active_record_deprecated_findersJon Leighton2012-04-251-6/+5
|
* giving a hash to default scope should not be deprecated (well, not for this ↵Jon Leighton2012-04-251-1/+1
| | | | reason)
* Corrected grammatical errors in schema_dumper and scoping/defaultdcurtis2012-04-101-1/+1
|
* Removes caching from ActiveRecord::Core::ClassMethods#relationBenedikt Deicke2012-04-031-2/+2
| | | | | | | | | | | The #relation method gets called in four places and the return value was instantly cloned in three of them. The only place that did not clone was ActiveRecord::Scoping::Default::ClassMethods#unscoped. This introduced a bug described in #5667 and should really clone the relation, too. This means all four places would clone the relation, so it doesn't make a lot of sense caching it in the first place. The four places with calls to relations are: activerecord/lib/active_record/scoping/default.rb:110:in `block in build_default_scope'" activerecord/lib/active_record/scoping/default.rb:42:in `unscoped'" activerecord/lib/active_record/scoping/named.rb:38:in `scoped'" activerecord/lib/active_record/scoping/named.rb:52:in `scope_attributes'"
* Deprecate eager-evaluated scopes.Jon Leighton2012-03-212-12/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Don't use this: scope :red, where(color: 'red') default_scope where(color: 'red') Use this: scope :red, -> { where(color: 'red') } default_scope { where(color: 'red') } The former has numerous issues. It is a common newbie gotcha to do the following: scope :recent, where(published_at: Time.now - 2.weeks) Or a more subtle variant: scope :recent, -> { where(published_at: Time.now - 2.weeks) } scope :recent_red, recent.where(color: 'red') Eager scopes are also very complex to implement within Active Record, and there are still bugs. For example, the following does not do what you expect: scope :remove_conditions, except(:where) where(...).remove_conditions # => still has conditions