Sha256: b887de9a1ada4f0efefb7a11af8fe1b003396aa1a72b5f7266d3cf05b5e23c15

Contents?: true

Size: 1.52 KB

Versions: 22

Compression:

Stored size: 1.52 KB

Contents

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

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

      def result(attribute_assigner, to_create)
        attribute_assigner.object.tap do |result_instance|
          stub_database_interaction_on_result(result_instance)
          run_callbacks(:after_stub, result_instance)
        end
      end

      private

      def next_id
        @@next_id += 1
      end

      def stub_database_interaction_on_result(result_instance)
        result_instance.id = next_id
        result_instance.instance_eval do
          def persisted?
            !new_record?
          end

          def created_at
            @created_at ||= Time.now
          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
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
challah-0.6.1 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.6.0 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.5.4 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.5.3 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.5.2 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.5.0 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
factory_girl-2.5.2 lib/factory_girl/proxy/stub.rb
challah-0.4.1 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.4.0 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.3.5 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.3.4 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.3.3 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.3.2 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
challah-0.3.1 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/proxy/stub.rb
factory_girl-2.5.1 lib/factory_girl/proxy/stub.rb
challah-0.3.0 vendor/bundle/gems/factory_girl-2.5.0/lib/factory_girl/proxy/stub.rb
challah-0.2.1 vendor/bundle/gems/factory_girl-2.5.0/lib/factory_girl/proxy/stub.rb
challah-0.2.0 vendor/bundle/gems/factory_girl-2.5.0/lib/factory_girl/proxy/stub.rb
factory_girl-2.5.0 lib/factory_girl/proxy/stub.rb
factory_girl-2.4.2 lib/factory_girl/proxy/stub.rb