Sha256: e330d977d977aa52894ff0b5c6b0d24ad7091b68d90381f237079fa9336a4911

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 KB

Contents

module FactoryGirl
  class Proxy
    class Stub < Proxy #:nodoc:
      @@next_id = 1000

      def initialize(klass)
        @instance = klass.new
        @instance.id = next_id
        @instance.instance_eval do
          def persisted?
            !new_record?
          end

          def new_record?
            id.nil?
          end

          def save(*args)
            raise "stubbed models are not allowed to access the database"
          end

          def destroy(*args)
            raise "stubbed models are not allowed to access the database"
          end

          def connection
            raise "stubbed models are not allowed to access the database"
          end

          def reload
            raise "stubbed models are not allowed to access the database"
          end

          def update_attribute(*args)
            raise "stubbed models are not allowed to access the database"
          end
        end
      end

      def next_id
        @@next_id += 1
      end

      def get(attribute)
        @instance.send(attribute)
      end

      def set(attribute, value)
        @instance.send(:"#{attribute}=", value)
      end

      def associate(name, factory_name, overrides)
        factory = FactoryGirl.factory_by_name(factory_name)
        set(name, factory.run(Proxy::Stub, overrides))
      end

      def association(factory_name, overrides = {})
        factory = FactoryGirl.factory_by_name(factory_name)
        factory.run(Proxy::Stub, overrides)
      end

      def result(to_create)
        run_callbacks(:after_stub)
        @instance
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
factory_girl-2.0.4 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.3 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.2 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.1 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.0.rc4 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.0.rc3 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.0.rc2 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.0.rc1 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.0.beta5 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.0.beta4 lib/factory_girl/proxy/stub.rb
factory_girl-2.0.0.beta3 lib/factory_girl/proxy/stub.rb