Patch #12499

Use lambda in model scopes

Added by Jean-Baptiste Barth almost 5 years ago. Updated almost 5 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Jean-Baptiste Barth% Done:

0%

Category:Plugin API
Target version:2.3.0

Description

Yesterday I had to monkey-patch "Group" in a plugin, and it turned out to be really difficult because there's a scope defined in the Group class which doesn't use lambda. In this case, the scope is evaluated directly, which fails with "Could not find table users" for instance if you're in a "db:migrate" phase and your database is empty.

Hence I propose moving all scopes to their lambda form so that these difficulties disappear for plugin development.

One more thing, using scopes without lambda (or a call-able) will be deprecated in Rails 4 and probably removed in Rails 4.1 (see this commit).

Let me know what you think. If you're OK with this change I can take care of verifying all models, running tests and making the change everywhere...

Associated revisions

Revision 10949
Added by Jean-Baptiste Barth almost 5 years ago

Use lambda form in model scopes (#12499)

Revision 12486
Added by Toshi MARUYAMA almost 4 years ago

use lambda form in Version model scope (#12499)

Revision 12487
Added by Toshi MARUYAMA almost 4 years ago

use lambda form in acts_as_tree scope (#12499)

History

#1 Updated by Jean-Baptiste Barth almost 5 years ago

If you want to see what's involved, here are the two commits on my plugin : one, two (but please don't start me on the bad idea of monkey patching core classes, that's all I have now to accomplish this kind of change...)

#2 Updated by Etienne Massip almost 5 years ago

Looks ok to me, should be marked as a deprecated usage in RoR, too bad it is not?

#3 Updated by Jean-Philippe Lang almost 5 years ago

  • Target version set to 2.3.0

Agreed.

#4 Updated by Jean-Baptiste Barth almost 5 years ago

  • Status changed from New to Resolved

Done in r10949. I left scopes in the same form they used before (some use Arel syntax, some use the rails 2 hash syntax) but I might dig into that later.

#5 Updated by Jean-Philippe Lang almost 5 years ago

  • Status changed from Resolved to Closed

Jean-Baptiste Barth wrote:

Done in r10949. I left scopes in the same form they used before (some use Arel syntax, some use the rails 2 hash syntax) but I might dig into that later.

Thanks, scopes now use Arel BTW (r10950).

Also available in: Atom PDF