Module: Mongoid::TestFixtures::ClassMethods

Defined in:
lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb

Instance Method Summary collapse

Instance Method Details

#fixtures(*table_names) ⇒ Object



842
843
844
845
846
847
848
849
850
851
852
853
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 842

def fixtures(*table_names)
  if table_names.first == :all
    table_names = Dir["#{Rails.root}#{fixture_path}/*.yml"] + Dir["#{Rails.root}#{fixture_path}/*.csv"]
    table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') }
  else
    table_names = table_names.flatten.map { |n| n.to_s }
  end

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

#require_fixture_classes(table_names = nil) ⇒ Object



868
869
870
871
872
873
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 868

def require_fixture_classes(table_names = nil)
  (table_names || fixture_table_names).each do |table_name|
    file_name = table_name.to_s
    try_to_load_dependency(file_name.singularize) # assume singularized model name and pluralized fixture file name
  end
end

#set_fixture_class(class_names = {}) ⇒ Object



838
839
840
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 838

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

#setup_fixture_accessors(table_names = nil) ⇒ Object



875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 875

def setup_fixture_accessors(table_names = nil)
  table_names = [table_names] if table_names && !table_names.respond_to?(:each)
  (table_names || fixture_table_names).each do |table_name|
    table_name = table_name.to_s.tr('.', '_')

    define_method(table_name) do |*fixtures|
      force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload

      @fixture_cache[table_name] ||= {}

      instances = fixtures.map do |fixture|
        @fixture_cache[table_name].delete(fixture) if force_reload

        if @loaded_fixtures[table_name][fixture.to_s]
          @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find
        else
          raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
        end
      end

      instances.size == 1 ? instances.first : instances
    end
  end
end

#try_to_load_dependency(file_name) ⇒ Object



855
856
857
858
859
860
861
862
863
864
865
866
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 855

def try_to_load_dependency(file_name)
  require_dependency file_name
rescue LoadError => e
  # Let's hope the developer has included it himself

  # Let's warn in case this is a subdependency, otherwise
  # subdependency error messages are totally cryptic
  mongoid_logger = Mongoid.config.send :logger
  if mongoid_logger
    mongoid_logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}")
  end
end

#uses_transaction(*methods) ⇒ Object



900
901
902
903
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 900

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

#uses_transaction?(method) ⇒ Boolean

Returns:

  • (Boolean)


905
906
907
908
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 905

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