Default Scopes Feature Implementation

88 views
Skip to first unread message

hamin

unread,
Jan 3, 2012, 5:16:46 AM1/3/12
to MongoMapper
Hey Guys,

Hope everyone had decent holidays. I will be back at work Tuesday
morning :)

I'm on megabus en route to NYC and was just hacking on something and
was playing with MongoMapper scopes and I came across something, we
still don't have a default_scope method :)

I've been looking at the scopes plugin in MM and how it works. I'm
interested in implementing this feature but wanted to run a couple of
things by you guys (especially Brandon and John ;) ) before I started
to work on it:

1. Do you think it would be best to add the default_scope and unscoped
methods needed for this functionality to the Scopes plugin or would it
be better to create a new DefaultScope plugin? I prefer the former
over the latter. Thoughts?

2. Looking at the Scopes plugin, it is pretty straightforward on how
one would merge conditions to a plucky query. However, do you guys
know of another way we could access conditions passed to the ruby
driver before ANY query is executed? The reason I ask is that there
are instances where you might not be getting a plucky query (i.e.
Model#find, Model#first). We would want to merge a default scope's
conditions even to such non-plucky queries. Is there a before query or
before execute query hook i can tap into? I haven't come across
looking through MM source code.

Any other thoughts on this? I would love to bang this out and submit a
pull request (with tests of course :) )

It's definitely not a crucial feature but a nice one to have.

Haris

Brandon Keepers

unread,
Jan 3, 2012, 3:01:45 PM1/3/12
to mongo...@googlegroups.com
I personally don't use default scopes. Every time I tried, it ended up biting me.

If we do add support for it to MongoMapper, we need to make it easier to exclude the scope on queries.  Active Record only allows you to exclude it with a block, but I'd prefer to se a method that can be chained to clear out the scope.

As for the implementation, we should just make all the finder methods (including #find, #first, etc) delegate to a plucky scope, and default_scope sets that (or uses an empty one if nothing is specified).

=b
--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at

jnunemaker

unread,
Jan 3, 2012, 4:54:25 PM1/3/12
to mongo...@googlegroups.com
I agree with Brandon. I've never had default_scope be useful. It always burns you in the long run. Much better to create a scope/method and always use that method.

def sorted
  sort(...)
end

User.sorted
Gauge.users.sorted

Stuff like that won't burn you.

hamin

unread,
Jan 4, 2012, 5:29:56 PM1/4/12
to MongoMapper
I've talked to a few other people and they seem to share your
sentiments John and Brandon. I'll file this one away then as
unnecessary :)

Haris
Reply all
Reply to author
Forward
0 new messages