Sha256: 862ea770c0bd9eba0b50922786f8e8cbf121e4b02de57e743cb570c4e2bcdfe6

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require 'active_record/fixtures'

module NamedSeeds

  class FixtureClassNotFound < ActiveRecord::FixtureClassNotFound
  end

  class Identity

    @@all_cached = Hash.new

    class << self

      def reset_cache
        @@all_cached.clear
      end

      def named(name, options)
        @@all_cached[name] ||= new(name, options)
      end

    end

    def initialize(name, options={})
      @name = name
      @options = options
      @fixtures = {}
    end

    def find(*identities)
      fixtures = identities.map { |identity| model_find(identity) }
      fixtures.many? ? fixtures : fixtures.first
    end

    def identities
      @options[:identities] || @options[:ids]
    end


    private

    def model_find(identity)
      @fixtures[identity.to_s] ||= model_class.unscoped { model_class.find model_id(identity) }
    end

    def model_class
      return @model_class if defined?(@model_class)
      @model_class = (@options[:class] || @options[:klass]) || @name.to_s.classify.safe_constantize
      raise FixtureClassNotFound, "No class found for named_seed #{@name.inspect}. Use :class option." unless @model_class
      @model_class
    end

    def model_id(identity)
      case identities
      when NilClass then NamedSeeds.identify(identity, model_primary_key_type)
      when Hash     then identities[identity]
      when :natural then identity
      end
    end

    def model_primary_key_type
      type     = model_class.columns_hash[model_class.primary_key].type
      sql_type = model_class.columns_hash[model_class.primary_key].sql_type
      return :uuid if ['uuid', 'guid', 'uniqueidentifier'].include?(sql_type)
      type ? type.to_sym : :id
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
named_seeds-2.2.0 lib/named_seeds/identity.rb
named_seeds-2.1.0 lib/named_seeds/identity.rb