Sha256: fadc59c26960738230a1fa152d0fdaa65f8a2df5bdbbe8f878401d0a4d67b75b

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

require 'spec_helper'

describe Mongoid::Ids::Finders do
  before :each do
  end

  it 'define a finder based on a field_name' do
    klass = Class.new
    field = :another_token
    Mongoid::Ids::Finders.define_custom_token_finder_for(klass, field)
    expect(klass.singleton_methods).to include(:"find_by_#{field}")
  end

  it 'don\'t override the `find` method of the document' do
    klass = Class.new
    klass.define_singleton_method(:find) { |*args| :original_find }
    klass.define_singleton_method(:find_by) { |*args| :token_find }

    Mongoid::Ids::Finders.define_custom_token_finder_for(klass)

    expect(klass.find(BSON::ObjectId.new)).to eq(:original_find)
    expect(klass.find(BSON::ObjectId.new, BSON::ObjectId.new)).to eq(:original_find)
    expect(klass.find()).to eq(:original_find)
    expect(klass.find(BSON::ObjectId.new, 'token')).to eq(:original_find)
    expect(klass.find('token')).to eq(:original_find)
  end

  it 'retrieve a document using the dynamic finder' do
    class Document; include Mongoid::Document; field :token; end
    document = Document.create!(token: '1234')
    Mongoid::Ids::Finders.define_custom_token_finder_for(Document)
    expect(Document.find_by_token('1234')).to eq(document)
  end

  it 'retrieve a document using the `find` method' do
    class AnotherDocument; include Mongoid::Document;  end
    document = AnotherDocument.create! :_id => '1234'
    Mongoid::Ids::Finders.define_custom_token_finder_for(AnotherDocument)
    expect(AnotherDocument.find('1234')).to eq(document)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-ids-0.1.8 spec/mongoid/ids/finders_spec.rb
mongoid-ids-0.1.7 spec/mongoid/ids/finders_spec.rb
mongoid-ids-0.1.1 spec/mongoid/ids/finders_spec.rb