Sha256: 02afea5c036ef2aa76d62b217b08716f49715b2ba96c17723feca37c34439cfc

Contents?: true

Size: 811 Bytes

Versions: 2

Compression:

Stored size: 811 Bytes

Contents

module Mongoid::Criterion
  module Optional

    extend ActiveSupport::Concern

    # Overwrite the id method to find objects
    # by the specified slug rather than the id.
    # If you want to find via id you'll have to use
    # An explicit finder like:
    #
    #   where(:_id => some_id)
    #
    alias :for_ids! :for_ids

    def for_ids(*ids)
      ids.flatten!
      if @klass.respond_to?(:acts_as_slugoid_options) && !ids.first.is_a?(BSON::ObjectId)
        if ids.size > 1
          self.in(
              @klass.acts_as_slugoid_options[:store_as] => ::BSON::ObjectId.cast!(@klass, ids, @klass.primary_key.nil?)
          )
        else
          @selector[@klass.acts_as_slugoid_options[:store_as]] = ids.first
        end
        self
      else
        for_ids!(*ids)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slugoid4-0.0.6 lib/slugoid/mongoid/criterion/optional.rb
slugoid4-4.0.0 lib/slugoid/mongoid/criterion/optional.rb