Sha256: e225084fa129b63314b7ce4c17d989a69daae51bd91e0640a6b485cfa6643f52

Contents?: true

Size: 1.83 KB

Versions: 11

Compression:

Stored size: 1.83 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.in_time_zone
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 8 versions & 3 rubygems

Version Path
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/lib/factory_girl/strategy/stub.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/strategy/stub.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/strategy/stub.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/lib/factory_girl/strategy/stub.rb
factory_girl-4.4.0 lib/factory_girl/strategy/stub.rb
factory_girl-4.3.0 lib/factory_girl/strategy/stub.rb
challah-1.0.0 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/strategy/stub.rb
challah-1.0.0.beta3 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/strategy/stub.rb
challah-1.0.0.beta2 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/strategy/stub.rb
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/strategy/stub.rb
factory_girl-4.2.0 lib/factory_girl/strategy/stub.rb