title: Why does an angular factory interfere with MongoId?
body: <p>I do have a Rails application (Rails 4 / Mongoid) which uses Capybara / Rspec. Furthermore I do use Angular.js for some client stuff. Unfortunately I experience a strange behavior in a spec test.</p>
  
  <pre><code>RuntimeError: Circular dependency detected while autoloading constant Offer
  </code></pre>
  
  <p><code>creating_offers_spec.rb</code></p>
  
  <pre><code>require 'spec_helper'
  
  feature 'creating offers' do
      before do
          visit '/offers/new'
      end
  
      scenario 'can create an offer', js: true do
          offer_number = Random.rand(10000)
  
          fill_in 'offer_number', with: offer_number
          fill_in 'offer_description', with: 'hello world!'
  
          click_button 'btn_submit'
  
          offer_in_db = Offer.where(number: offer_number).first
          expect(offer_in_db.description).to eql 'hello world!'
      end
  end
  </code></pre>
  
  <p>As JavaScript driver for Capybara I do use poltergeist.</p>
  
  <p><code>OfferController.js.coffee</code></p>
  
  <pre><code>angular.module('exampleApp').controller 'OffersController', ($scope, Offer) -&gt;
      $scope.offer = {}
      $scope.offer.positions = []
  
      $scope.addOffer = -&gt;
          new Offer().create($scope.offer)
  
      $scope.addPosition = -&gt;
          position = { order: $scope.offer.positions.length + 1 }
          $scope.offer.positions.push(position)
  </code></pre>
  
  <p><code>OfferService.js.coffee</code></p>
  
  <pre><code>angular.module('exampleApp').factory 'Offer', ($resource, $http) -&gt;
      class Offer
          constructor: -&gt;
              @service = $resource('/api/offers/:offersId', {offersId: '@id'}, {update: {method: 'PATCH'}})
  
              defaults = $http.defaults.headers
              defaults.patch = defaults.patch || {}
              defaults.patch['Content-Type'] = 'application/json'
  
          create: (attrs) -&gt;
              offer = new @service(offer: attrs)
              offer.$save ($offer) -&gt;
                  attrs.id = $offer._id
              attrs
  </code></pre>
  
  <p>Now when I rename the service to OfferService, I don't get the error with the circular dependency. Why does the angular.js service interfere with the mongoid model?</p>
  
  <p>Update: I just figured out that the circular dependency issue does also raise even when I do rename the controller. I forgot to uncomment the <code>Offer.where(...)</code> part of the spec. Why does this happen or let me ask otherwise, any ideas what I could have messed up?</p>
  
  <h2>Update I</h2>
  
  <p>I've added the <code>gem 'capybara-angular'</code> and included it into the <code>spec_helber.rb</code> as described on <a href="https://github.com/wrozka/capybara-angular" rel="nofollow">https://github.com/wrozka/capybara-angular</a>. At least the test do run know. Unfortunately I experience now the problem, that the evaluate_script gets no longer routed to poltergeist because of the above gem.</p>