class Mongoid::Slug::Criteria

Public Instance Methods

find(*args) click to toggle source

Find the matchind document(s) in the criteria for the provided ids or slugs.

If the document _ids are of the type BSON::ObjectId, and all the supplied parameters are convertible to BSON::ObjectId (via BSON::ObjectId#from_string), finding will be performed via _ids.

If the document has any other type of _id field, and all the supplied parameters are of the same type, finding will be performed via _ids.

Otherwise finding will be performed via slugs.

@example Find by an id.

criteria.find(BSON::ObjectId.new)

@example Find by multiple ids.

criteria.find([ BSON::ObjectId.new, BSON::ObjectId.new ])

@example Find by a slug.

criteria.find('some-slug')

@example Find by multiple slugs.

criteria.find([ 'some-slug', 'some-other-slug' ])

@param [ Array<Object> ] args The ids or slugs to search for.

@return [ Array<Document>, Document ] The matching document(s).

Calls superclass method
# File lib/mongoid/slug/criteria.rb, line 30
def find(*args)
  look_like_slugs?(args.__find_args__) ? find_by_slug!(*args) : super
end
find_by_slug!(*args) click to toggle source

Find the matchind document(s) in the criteria for the provided slugs.

@example Find by a slug.

criteria.find('some-slug')

@example Find by multiple slugs.

criteria.find([ 'some-slug', 'some-other-slug' ])

@param [ Array<Object> ] args The slugs to search for.

@return [ Array<Document>, Document ] The matching document(s).

# File lib/mongoid/slug/criteria.rb, line 45
def find_by_slug!(*args)
  slugs = args.__find_args__
  raise_invalid if slugs.any?(&:nil?)
  for_slugs(slugs).execute_or_raise_for_slugs(slugs, args.multi_arged?)
end
look_like_slugs?(args) click to toggle source
# File lib/mongoid/slug/criteria.rb, line 51
def look_like_slugs?(args)
  return false unless args.all? { |id| id.is_a?(String) }
  id_field = @klass.fields['_id']
  @slug_strategy ||= id_field.options[:slug_id_strategy] || build_slug_strategy(id_field.type)
  args.none? { |id| @slug_strategy.call(id) }
end

Protected Instance Methods

build_slug_strategy(id_type) click to toggle source

unless a :slug_id_strategy option is defined on the id field, use object_id or string strategy depending on the id_type otherwise default for all other id_types

# File lib/mongoid/slug/criteria.rb, line 63
def build_slug_strategy id_type
  type_method = id_type.to_s.downcase.split('::').last + "_slug_strategy"
  self.respond_to?(type_method, true) ? method(type_method) : lambda {|id| false}
end
check_for_missing_documents_for_slugs!(result, slugs) click to toggle source
# File lib/mongoid/slug/criteria.rb, line 101
def check_for_missing_documents_for_slugs!(result, slugs)
  missing_slugs = slugs - result.map(&:slugs).flatten

  if !missing_slugs.blank? && Mongoid.raise_not_found_error
    raise Errors::DocumentNotFound.new(klass, slugs, missing_slugs)
  end
end
execute_or_raise_for_slugs(slugs, multi) click to toggle source
# File lib/mongoid/slug/criteria.rb, line 95
def execute_or_raise_for_slugs(slugs, multi)
  result = uniq
  check_for_missing_documents_for_slugs!(result, slugs)
  multi ? result : result.first
end
for_slugs(slugs) click to toggle source
# File lib/mongoid/slug/criteria.rb, line 83
def for_slugs(slugs)
  #_translations
  localized = (@klass.fields['_slugs'].options[:localize] rescue false)
  if localized
    def_loc = I18n.default_locale
    query = { '$in' => slugs }
    where({'$or' => [{ _slugs: query }, { "_slugs.#{def_loc}" => query }]}).limit(slugs.length)
  else
    where({ _slugs: { '$in' => slugs } }).limit(slugs.length)
  end
end
objectid_slug_strategy(id) click to toggle source

a string will not look like a slug if it looks like a legal BSON::ObjectId

# File lib/mongoid/slug/criteria.rb, line 69
def objectid_slug_strategy id
  if Mongoid::Slug.mongoid3?
    Moped::BSON::ObjectId.legal? id
  else
    BSON::ObjectId.legal?(id)
  end
end
string_slug_strategy(id) click to toggle source

a string will always look like a slug

# File lib/mongoid/slug/criteria.rb, line 78
def string_slug_strategy id
  true
end