Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #47: use a database index when generating proxy binding in Garner::Mixins::Mongoid::Identity with multiple Garner.config.mongoid_identity_fields. #49

Merged
merged 1 commit into from
Jul 11, 2013

Conversation

dblock
Copy link
Contributor

@dblock dblock commented Jul 11, 2013

This is just the index part of the fix in #48.

… Garner::Mixins::Mongoid::Identity with multiple Garner.config.mongoid_identity_fields.
fancyremarker pushed a commit that referenced this pull request Jul 11, 2013
Fixed #47: use a database index when generating proxy binding in Garner::Mixins::Mongoid::Identity with multiple Garner.config.mongoid_identity_fields.
@fancyremarker fancyremarker merged commit 35e357a into artsy:master Jul 11, 2013
@fancyremarker
Copy link
Contributor

Thanks!

@dblock
Copy link
Contributor Author

dblock commented Jul 11, 2013

Fyi, if you have Garner.config.mongoid_identity_fields = [:_id, :_slugs] with something like Mongoid::Slug, use this monkey patch if you want to override the behavior in your own code and avoid the $or query.

module Garner
  module Mixins
    module Mongoid
      class Identity
        private

        def self.conditions_for(klass, handle)
          # optimize away the query from an $or to a find where appropriate
          # in the case of Gravity _id and _slugs can be used independently
          # and we can never have a non BSON _id and valid BSON IDs are redundant with _id inside _slugs
          conditions = Moped::BSON::ObjectId.legal?(handle) ? { _id: handle } : { _slugs: handle }

          # _type conditions
          selector = klass.where({})
          conditions.merge!(selector.send(:type_selection)) if selector.send(:type_selectable?)

          conditions
        end
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants