Sha256: 3bb67b295c9dd584038c1732a1fb06f253e30af22c88c513ee9fa1e3aedef4e2

Contents?: true

Size: 1.91 KB

Versions: 59

Compression:

Stored size: 1.91 KB

Contents

module FactoryGirl
  module Syntax

    # Extends ActiveRecord::Base to provide generation methods for factories.
    #
    # Usage:
    #
    #   require 'factory_girl/syntax/generate'
    #
    #   FactoryGirl.define do
    #     factory :user do
    #       name 'Billy Bob'
    #       email 'billy@bob.example.com'
    #     end
    #   end
    #
    #   # Creates a saved instance without raising (same as saving the result
    #   # of FactoryGirl.build)
    #   User.generate(name: 'Johnny')
    #
    #   # Creates a saved instance and raises when invalid (same as
    #   # FactoryGirl.create)
    #   User.generate!
    #
    #   # Creates an unsaved instance (same as FactoryGirl.build)
    #   User.spawn
    #
    #   # Creates an instance and yields it to the passed block
    #   User.generate do |user|
    #     # ...do something with user...
    #   end
    #
    # This syntax was derived from Rick Bradley and Yossef Mendelssohn's
    # object_daddy.
    module Generate
      module ActiveRecord #:nodoc:

        def self.included(base) # :nodoc:
          base.extend ClassMethods
        end

        module ClassMethods #:nodoc:

          def generate(overrides = {}, &block)
            instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
            instance.save
            yield(instance) if block_given?
            instance
          end

          def generate!(overrides = {}, &block)
            instance = FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
            yield(instance) if block_given?
            instance
          end

          def spawn(overrides = {}, &block)
            instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
            yield(instance) if block_given?
            instance
          end

        end

      end
    end
  end
end

ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Generate::ActiveRecord)

Version data entries

59 entries across 59 versions & 4 rubygems

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