Module: ActiveRecord::TestFixtures::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#fixture_pathObject



45
46
47
48
49
50
51
52
# File 'activerecord/lib/active_record/test_fixtures.rb', line 45

def fixture_path
  ActiveRecord.deprecator.warn(<<~WARNING)
    TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2. Use #fixture_paths instead.
    If multiple fixture paths have been configured with #fixture_paths, then #fixture_path will just return
    the first path.
  WARNING
  fixture_paths.first
end

#fixture_path=(path) ⇒ Object



54
55
56
57
# File 'activerecord/lib/active_record/test_fixtures.rb', line 54

def fixture_path=(path)
  ActiveRecord.deprecator.warn("TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2. Use #fixture_paths instead.")
  self.fixture_paths = Array(path)
end

#fixtures(*fixture_set_names) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'activerecord/lib/active_record/test_fixtures.rb', line 59

def fixtures(*fixture_set_names)
  if fixture_set_names.first == :all
    raise StandardError, "No fixture path found. Please set `#{self}.fixture_paths`." if fixture_paths.blank?
    fixture_set_names = fixture_paths.flat_map do |path|
      names = Dir[::File.join(path, "{**,*}/*.{yml}")].uniq
      names.reject! { |f| f.start_with?(file_fixture_path.to_s) } if defined?(file_fixture_path) && file_fixture_path
      names.map! { |f| f[path.to_s.size..-5].delete_prefix("/") }
    end.uniq
  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.



41
42
43
# File 'activerecord/lib/active_record/test_fixtures.rb', line 41

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

#setup_fixture_accessors(fixture_set_names = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'activerecord/lib/active_record/test_fixtures.rb', line 75

def setup_fixture_accessors(fixture_set_names = nil)
  fixture_set_names = Array(fixture_set_names || fixture_table_names)
  unless fixture_set_names.empty?
    self.fixture_sets = fixture_sets.dup
    fixture_set_names.each do |fs_name|
      key = fs_name.to_s.include?("/") ? -fs_name.to_s.tr("/", "_") : fs_name
      key = -key.to_s if key.is_a?(Symbol)
      fs_name = -fs_name.to_s if fs_name.is_a?(Symbol)
      fixture_sets[key] = fs_name
    end
  end
end

#uses_transaction(*methods) ⇒ Object

Prevents automatically wrapping each specified test in a transaction, to allow application logic transactions to be tested in a top-level (non-nested) context.



91
92
93
94
# File 'activerecord/lib/active_record/test_fixtures.rb', line 91

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

#uses_transaction?(method) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'activerecord/lib/active_record/test_fixtures.rb', line 96

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