Sha256: bcbdff60a90e6de939801426026190570d756c0007e4cb99a54ca9fc24998c00

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require "fixtury/store"

module Fixtury
  module Hooks

    extend ::ActiveSupport::Concern

    included do
      class_attribute :fixtury_dependencies
      self.fixtury_dependencies = Set.new
    end

    module ClassMethods

      def fixtury(*names)
        self.fixtury_dependencies += names.flatten.compact.map(&:to_s)
      end

    end

    def fixtury(name)
      raise ArgumentError unless self.fixtury_dependencies.include?(name.to_s)

      ::Fixtury::Store.instance.get(name)
    end

    def fixtury_loaded?(name)
      ::Fixtury::Store.instance.loaded?(name)
    end

    def fixtury_database_connections
      ActiveRecord::Base.connection_handler.connection_pool_list.map(&:connection)
    end

    # piggybacking activerecord fixture setup for now.
    def setup_fixtures(*args)
      if fixtury_dependencies.any?
        setup_fixtury_fixtures
      else
        super
      end
    end

    # piggybacking activerecord fixture setup for now.
    def teardown_fixtures(*args)
      if fixtury_dependencies.any?
        teardown_fixtury_fixtures
      else
        super
      end
    end

    def setup_fixtury_fixtures
      return unless use_transactional_fixtures

      clear_expired_fixtury_fixtures!
      load_all_fixtury_fixtures!

      fixtury_database_connections.each do |conn|
        conn.begin_transaction joinable: false
      end
    end

    def teardown_fixtury_fixtures
      return unless use_transactional_fixtures

      fixtury_database_connections.each(&:rollback_transaction)
    end

    def clear_expired_fixtury_fixtures!
      ::Fixtury::Store.instance.clear_expired_references!
    end

    def load_all_fixtury_fixtures!
      fixtury_dependencies.each do |name|
        fixtury(name) unless fixtury_loaded?(name)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fixtury-0.1.0.beta lib/fixtury/hooks.rb
fixtury-0.1.0.alpha2 lib/fixtury/hooks.rb
fixtury-0.1.0.alpha lib/fixtury/hooks.rb