Sha256: 13d8faa7f5ad762fb1a140de2e0a6041d2a315eec7050d36135ded41bcf6d132

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

module FactoryGirl
  module Preload
    autoload :Helpers, "factory_girl/preload/helpers"
    autoload :Version, "factory_girl/preload/version"

    require "factory_girl/preload/rspec2" if defined?(RSpec)
    require "factory_girl/preload/extension"

    class << self
      attr_accessor :preloaders
      attr_accessor :factories
      attr_accessor :record_ids
      attr_accessor :clean_with
    end

    self.preloaders = []
    self.factories = {}
    self.record_ids = {}
    self.clean_with = :truncation

    def self.active_record
      ActiveRecord::Base
    end

    def self.connection
      active_record.connection
    end

    def self.run
      helper = Object.new.extend(Helpers)

      connection.transaction :requires_new => true do
        preloaders.each do |block|
          helper.instance_eval(&block)
        end
      end
    end

    def self.clean(*names)
      query = case clean_with
        when :truncation then try_truncation_query
        when :deletion then "DELETE FROM %s"
        else raise "Couldn't find #{clean_with} clean type"
      end

      names = active_record.descendants.collect(&:table_name).uniq if names.empty?

      connection.disable_referential_integrity do
        names.each do |table|
          connection.execute(query % connection.quote_table_name(table))
        end
      end
    end

    def self.reload_factories
      factories.each do |class_name, group|
        group.each do |name, factory|
          factories[class_name][name] = nil
        end
      end
    end

    private
    def self.try_truncation_query
      case connection.adapter_name
        when "SQLite"     then "DELETE FROM %s"
        when "PostgreSQL" then "TRUNCATE TABLE %s RESTART IDENTITY CASCADE"
        else "TRUNCATE TABLE %s"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
factory_girl-preload-2.2.0 lib/factory_girl/preload.rb
factory_girl-preload-2.1.1 lib/factory_girl/preload.rb
factory_girl-preload-2.1.0 lib/factory_girl/preload.rb