Sha256: e558b86733f4b10ca38b5fab704aced0a2e9d0386d5cc1b09e954a12cb636d30

Contents?: true

Size: 1.36 KB

Versions: 55

Compression:

Stored size: 1.36 KB

Contents

module FactoryGirl
  module Strategy
    class Stub
      @@next_id = 1000

      def association(runner)
        runner.run(Strategy::Stub)
      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 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

55 entries across 55 versions & 4 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.7.4 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.7.3 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.7.2 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.7.1 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.7.0 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.9 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.8 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.7 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.6 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.5 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.4 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.3 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.2 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.1 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.6.0 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.5.17 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.5.16 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.5.15 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb
classiccms-0.5.14 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/strategy/stub.rb