[feature] named scopes #190

Closed
pehrlich opened this Issue Apr 18, 2012 · 3 comments

Comments

Projects
None yet
3 participants
Contributor

pehrlich commented Apr 18, 2012

Here's a request, probably for 2.0+

Have named scopes which can be applied to customize declared relationships. This is a feature available in both AR 3+ and Mongoid.

# post model:
has_many(:comments).to('Comment')

# comment model:
scope :default, desc(:created_at)
scope :visible, where(visible: true).limit(10)

# usage:
Comment.all # sorted
Comment.all.visible # visible and sorted
post.comments # sorted
post.comments.visible # visible and sorted
  • default is a special name for a scope, which applies to the default/base query.
  • The syntax should be as consistent as possible (desc, asc, where, etc) with the Lucene finders and with mongoid & AR.

This would be a great way to clean up model files and benefit rapid prototyping!

Owner

andreasronge commented Apr 19, 2012

Interesting. It will probably not use lucene but instead Cypher since it is not possible to sort post.comments.visiblie with lucene.
Not sure if we should try to use the AR3 and Mongoid API or if we should continue using our API (rules).
I don't want to maintain two similar APIs. Maybe I can adapt the rules method to look like scope (or alias) and let it take the same parameters.

Btw, what do you think about the Model.find method which takes a hash with a condition in it - should we deprecate that and try to use AR3 style instead ?
Agree, I think we should wait with this after 2.0 release

Contributor

pehrlich commented Apr 19, 2012

Agreed, this is for sure a cypher job.

If neo4j is growing the way I think it is, the next few years should show many more people coming over than those already using the gem. Likely the first to come will be people who have also experience with mongoid, and we'd have a lot to gain by becoming part of a de-facto standard.

I think that the methods which do scoping are far more powerful than an options hash. All the best queries are built incrementally, either in different parts or one method, or assembled by several, and this incremental construction cannot be done (well) with an options hash.

http://speakerdeck.com/u/holman/p/ruby-patterns-from-githubs-codebase?slide=44

For example, github demonstrates a paginate method, which takes any query with conditions etc already assembled, and slaps on limit and skip based on URL Params. I've been using this pattern with neo4j helper in my own code base to great effect.

Cheers

Sent from my iPhone

On Apr 19, 2012, at 12:19 PM, Andreas Rongereply@reply.github.com wrote:

Interesting. It will probably not use lucene but instead Cypher since it is not possible to sort post.comments.visiblie with lucene.
Not sure if we should try to use the AR3 and Mongoid API or if we should continue using our API (rules).
I don't want to maintain two similar APIs. Maybe I can adapt the rules method to look like scope (or alias) and let it take the same parameters.

Btw, what do you think about the Model.find method which takes a hash with a condition in it - should we deprecate that and try to use AR3 style instead ?
Agree, I think we should wait with this after 2.0 release


Reply to this email directly or view it on GitHub:
andreasronge#190 (comment)

Contributor

saterus commented Jul 3, 2013

This has been solved with Rules.

saterus closed this Jul 3, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment