Module: ActiveRecord::TestFixtures::ClassMethods

Defined in:
activerecord/lib/active_record/fixtures.rb

Instance Method Summary collapse

Instance Method Details

#fixtures(*fixture_set_names) ⇒ Object



904
905
906
907
908
909
910
911
912
913
914
# File 'activerecord/lib/active_record/fixtures.rb', line 904

def fixtures(*fixture_set_names)
  if fixture_set_names.first == :all
    fixture_set_names = Dir["#{fixture_path}/{**,*}/*.{yml}"]
    fixture_set_names.map! { |f| f[(fixture_path.to_s.size + 1)..-5] }
  else
    fixture_set_names = fixture_set_names.flatten.map(&:to_s)
  end

  self.fixture_table_names |= fixture_set_names
  setup_fixture_accessors(fixture_set_names)
end

#set_fixture_class(class_names = {}) ⇒ Object

Sets the model class for a fixture when the class name cannot be inferred from the fixture name.

Examples:

set_fixture_class some_fixture:        SomeModel,
                  'namespaced/fixture' => Another::Model

The keys must be the fixture names, that coincide with the short paths to the fixture files.



900
901
902
# File 'activerecord/lib/active_record/fixtures.rb', line 900

def set_fixture_class(class_names = {})
  self.fixture_class_names = self.fixture_class_names.merge(class_names.stringify_keys)
end

#setup_fixture_accessors(fixture_set_names = nil) ⇒ Object



916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'activerecord/lib/active_record/fixtures.rb', line 916

def setup_fixture_accessors(fixture_set_names = nil)
  fixture_set_names = Array(fixture_set_names || fixture_table_names)
  methods = Module.new do
    fixture_set_names.each do |fs_name|
      fs_name = fs_name.to_s
      accessor_name = fs_name.tr('/', '_').to_sym

      define_method(accessor_name) do |*fixture_names|
        force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload

        @fixture_cache[fs_name] ||= {}

        instances = fixture_names.map do |f_name|
          f_name = f_name.to_s if f_name.is_a?(Symbol)
          @fixture_cache[fs_name].delete(f_name) if force_reload

          if @loaded_fixtures[fs_name][f_name]
            @fixture_cache[fs_name][f_name] ||= @loaded_fixtures[fs_name][f_name].find
          else
            raise StandardError, "No fixture named '#{f_name}' found for fixture set '#{fs_name}'"
          end
        end

        instances.size == 1 ? instances.first : instances
      end
      private accessor_name
    end
  end
  include methods
end

#uses_transaction(*methods) ⇒ Object



947
948
949
950
# File 'activerecord/lib/active_record/fixtures.rb', line 947

def uses_transaction(*methods)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.concat methods.map(&:to_s)
end

#uses_transaction?(method) ⇒ Boolean



952
953
954
955
# File 'activerecord/lib/active_record/fixtures.rb', line 952

def uses_transaction?(method)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.include?(method.to_s)
end