Commit Graph

19 Commits

Author SHA1 Message Date
Carlos Antonio da Silva
5f1e394165 Fix bad usage of #select with hash 2013-12-03 10:45:40 -02:00
Jon Leighton
64b9e93bb5 Fix ActiveRecord::Relation#unscope
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.
2013-11-20 22:23:16 +00:00
Rafael Mendonça França
bd09afb5fb Don't skip tests if we don't need to.
We can conditional define the tests depending on the adapter or
connection.

Lets keep the skip for fail tests that need to be fixed.
2013-11-08 13:59:07 -02:00
Rafael Mendonça França
4d7080f80c unscope now works on default_scope after 94924dc32baf78f13e289172534c2e71c9c8cade 2013-11-02 22:11:20 -02:00
Yves Senn
19639c7184 remove trailing whitespace added with b057765 [ci skip]. 2013-10-21 17:25:25 +02:00
Eric Hankins
b057765817 Allow unscope to work with where.not
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.
2013-10-21 09:43:57 -05:00
Aaron Patterson + Akira Matsuda
79238bad3d asakusa.rb hack night!
Fix in-memory tests
2013-08-06 21:26:10 +09:00
Rafael Mendonça França
92c5a2244e Revert change on ActiveRecord::Relation#order method that prepends new
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.
2013-07-29 23:18:33 -03:00
Aaron Patterson
16e4d013d8 close our connection when we are done 2013-07-08 16:36:46 -07:00
Arun Agrawal
50cbc03d18 Remove deprecated scope use without passing a callable object.
Removed tests from deprecated code.
2013-07-03 23:23:11 +02:00
Neeraj Singh
71db2420a1 calling default_scope without a proc will raise ArgumentError
Calling default_scope without a proc will now raise `ArgumentError`.
2013-07-02 08:07:33 +05:30
Yves Senn
22b3481ba2 remove deprecated implicit join references. 2013-06-29 10:50:44 +02:00
Jon Leighton
94924dc32b Simplify/fix implementation of default scopes
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.
2013-06-28 13:45:57 +01:00
Carlos Antonio da Silva
85a56ff01d Delegate #unscope query method 2013-04-28 23:00:35 -03:00
Jon Leighton
8606a7fbe9 Fix scope chaining + STI
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.
2013-04-05 13:14:28 +01:00
Neeraj Singh
f029fb07c2 failing test for #9869 2013-04-05 13:14:28 +01:00
Takehiro Adachi
7be9e88682 split relation_scoping_test.rb's default scoping tests into another file 2013-03-30 13:23:01 +09:00
Takehiro Adachi
e4da432a3a rename named_scope_test.rb to a proper file name
The file name should be name_scoping_test.rb and the class should be
`NamedScopingTest` according to ActiveRecord::Scoping::Name
2013-03-30 13:15:37 +09:00
Takehiro Adachi
1fde758297 move tests for NamedScope and DefaultScope under test/cases/scoping/
The scoping/default.rb and scoping/named.rb got moved under scoping/ in
commit 2b22564c4efaa63d4bbc006762838c4025c1bdca,
but the tests never did.
2013-03-30 13:13:18 +09:00