Sha256: d409e03745205dc9b157d0072d6989e67e35804a27650f2591c7aec6353f64ac

Contents?: true

Size: 1.82 KB

Versions: 27

Compression:

Stored size: 1.82 KB

Contents

module FactoryGirl
  module Strategy
    class Stub
      @@next_id = 1000

      def association(runner)
        runner.run(:build_stubbed)
      end

      def result(evaluation)
        evaluation.object.tap do |instance|
          stub_database_interaction_on_result(instance)
          evaluation.notify(:after_stub, 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 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

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

        created_at_missing_default = result_instance.respond_to?(:created_at) && !result_instance.created_at
        result_instance_missing_created_at = !result_instance.respond_to?(:created_at)

        if created_at_missing_default || result_instance_missing_created_at
          result_instance.instance_eval do
            def created_at
              @created_at ||= Time.now
            end
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 19 versions & 4 rubygems

Version Path
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.1.0/lib/factory_girl/strategy/stub.rb
challah-0.9.1.beta.3 vendor/bundle/gems/factory_girl-4.1.0/lib/factory_girl/strategy/stub.rb
challah-0.9.1.beta vendor/bundle/gems/factory_girl-4.1.0/lib/factory_girl/strategy/stub.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.1.0/lib/factory_girl/strategy/stub.rb
challah-0.9.0 vendor/bundle/gems/factory_girl-4.1.0/lib/factory_girl/strategy/stub.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/factory_girl-4.0.0/lib/factory_girl/strategy/stub.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/factory_girl-4.1.0/lib/factory_girl/strategy/stub.rb
challah-rolls-0.2.0 vendor/bundle/gems/factory_girl-3.6.1/lib/factory_girl/strategy/stub.rb
challah-rolls-0.2.0 vendor/bundle/gems/factory_girl-4.0.0/lib/factory_girl/strategy/stub.rb
challah-rolls-0.2.0 vendor/bundle/gems/factory_girl-4.1.0/lib/factory_girl/strategy/stub.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/factory_girl-3.6.1/lib/factory_girl/strategy/stub.rb
challah-0.8.3 vendor/bundle/gems/factory_girl-4.1.0/lib/factory_girl/strategy/stub.rb
challah-0.8.3 vendor/bundle/gems/factory_girl-4.0.0/lib/factory_girl/strategy/stub.rb
factory_girl-3.6.2 lib/factory_girl/strategy/stub.rb
challah-0.8.1 vendor/bundle/gems/factory_girl-4.0.0/lib/factory_girl/strategy/stub.rb
factory_girl-4.1.0 lib/factory_girl/strategy/stub.rb
challah-rolls-0.1.0 vendor/bundle/gems/factory_girl-4.0.0/lib/factory_girl/strategy/stub.rb
challah-rolls-0.1.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/factory_girl-3.6.1/lib/factory_girl/strategy/stub.rb
challah-rolls-0.1.0 vendor/bundle/gems/factory_girl-3.6.1/lib/factory_girl/strategy/stub.rb
challah-0.8.0.pre vendor/bundle/gems/factory_girl-3.6.1/lib/factory_girl/strategy/stub.rb