Module: ActiveRecord::TestFixtures::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#fixtures(*fixture_set_names) ⇒ Object



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

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



37
38
39
# File 'activerecord/lib/active_record/test_fixtures.rb', line 37

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'activerecord/lib/active_record/test_fixtures.rb', line 55

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
        return_single_record = fixture_names.size == 1
        fixture_names = @loaded_fixtures[fs_name].fixtures.keys if fixture_names.empty?

        @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

        return_single_record ? instances.first : instances
      end
      private accessor_name
    end
  end
  include methods
end

#uses_transaction(*methods) ⇒ Object



88
89
90
91
# File 'activerecord/lib/active_record/test_fixtures.rb', line 88

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

#uses_transaction?(method) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'activerecord/lib/active_record/test_fixtures.rb', line 93

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