Module: ActiveRecord::TestFixtures::ClassMethods

Defined in:
lib/active_record/fixtures.rb

Instance Method Summary collapse

Instance Method Details

#fixtures(*fixture_set_names) ⇒ Object



759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/active_record/fixtures.rb', line 759

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 { |n| n.to_s }
  end

  self.fixture_table_names |= fixture_set_names
  require_fixture_classes(fixture_set_names)
  setup_fixture_accessors(fixture_set_names)
end

#require_fixture_classes(fixture_set_names = nil) ⇒ Object



783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/active_record/fixtures.rb', line 783

def require_fixture_classes(fixture_set_names = nil)
  if fixture_set_names
    fixture_set_names = fixture_set_names.map { |n| n.to_s }
  else
    fixture_set_names = fixture_table_names
  end

  fixture_set_names.each do |file_name|
    file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names
    try_to_load_dependency(file_name)
  end
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. – It is also possible to pass the class name instead of the class:

set_fixture_class 'some_fixture' => 'SomeModel'

I think this option is redundant, i propose to deprecate it. Isn’t it easier to always pass the class itself? (2011-12-20 alexeymuranov) ++



755
756
757
# File 'lib/active_record/fixtures.rb', line 755

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



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/active_record/fixtures.rb', line 796

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
          @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

#try_to_load_dependency(file_name) ⇒ Object



772
773
774
775
776
777
778
779
780
781
# File 'lib/active_record/fixtures.rb', line 772

def try_to_load_dependency(file_name)
  require_dependency file_name
rescue LoadError => e
  # Let's hope the developer has included it
  # Let's warn in case this is a subdependency, otherwise
  # subdependency error messages are totally cryptic
  if ActiveRecord::Base.logger
    ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}")
  end
end

#uses_transaction(*methods) ⇒ Object



827
828
829
830
# File 'lib/active_record/fixtures.rb', line 827

def uses_transaction(*methods)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.concat methods.map { |m| m.to_s }
end

#uses_transaction?(method) ⇒ Boolean

Returns:

  • (Boolean)


832
833
834
835
# File 'lib/active_record/fixtures.rb', line 832

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