Sha256: 569649499317ff47b657cf6df1c751c7322f4690476b6e1397018e7b08c288f0

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'spec_helper'

module LoadConst
  def const_missing(name)
    name = "#{self.name}::#{name}"
    eval("#{name} = Class.new; #{name}.send(:include, CouchPotato::Persistence); #{name}.extend(LoadConst)")
  end
end

class Autoloader
  extend LoadConst
end

class WithUnloadedEmbedded
  include CouchPotato::Persistence
  extend LoadConst
  
  property :embedded
  
  view :all, :type => :custom, :map => 'function(doc) {emit(doc._id, null)}', :include_docs => true
end


describe CouchPotato::Database, 'rails specific behavior' do
  
  before(:each) do
    recreate_db
  end
  
  context 'load a document' do
    it "should load models whose constants are currently uninitialized (like with rails in development mode)" do
      CouchPotato.couchrest_database.save_doc(JSON.create_id => 'Autoloader::Uninitialized', '_id' => '1')
      CouchPotato.database.load('1').class.name.should == 'Autoloader::Uninitialized'
    end
  
    it "should load nested models" do
      CouchPotato.couchrest_database.save_doc(JSON.create_id => 'Autoloader::Nested::Nested2', '_id' => '1')
      CouchPotato.database.load('1').class.name.should == 'Autoloader::Nested::Nested2'
    end
  end  
  
  context 'load documents using a view' do
    it "should load models from a view whose constants are currently uninitialized" do
      doc = {JSON.create_id => 'WithUnloadedEmbedded', '_id' => '1', 'embedded' => {JSON.create_id => 'WithUnloadedEmbedded::Uninitialized'}}
      CouchPotato.couchrest_database.save_doc(doc)
      CouchPotato.database.view(WithUnloadedEmbedded.all).first.embedded.class.name.should == 'WithUnloadedEmbedded::Uninitialized'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
couch_potato-0.3.2 spec/rails_spec.rb
couch_potato-0.3.1 spec/rails_spec.rb