Module: ActiveRecord::TestFixtures::ClassMethods
- Defined in:
- activerecord/lib/active_record/fixtures.rb
Instance Method Summary collapse
- #fixtures(*fixture_names) ⇒ Object
- #require_fixture_classes(fixture_names = nil) ⇒ Object
- #set_fixture_class(class_names = {}) ⇒ Object
- #setup_fixture_accessors(fixture_names = nil) ⇒ Object
- #try_to_load_dependency(file_name) ⇒ Object
- #uses_transaction(*methods) ⇒ Object
- #uses_transaction?(method) ⇒ Boolean
Instance Method Details
#fixtures(*fixture_names) ⇒ Object
795 796 797 798 799 800 801 802 803 804 805 806 |
# File 'activerecord/lib/active_record/fixtures.rb', line 795 def fixtures(*fixture_names) if fixture_names.first == :all fixture_names = Dir["#{fixture_path}/**/*.{yml,csv}"] fixture_names.map! { |f| f[(fixture_path.size + 1)..-5] } else fixture_names = fixture_names.flatten.map { |n| n.to_s } end self.fixture_table_names |= fixture_names require_fixture_classes(fixture_names) setup_fixture_accessors(fixture_names) end |
#require_fixture_classes(fixture_names = nil) ⇒ Object
820 821 822 823 824 825 826 |
# File 'activerecord/lib/active_record/fixtures.rb', line 820 def require_fixture_classes(fixture_names = nil) (fixture_names || fixture_table_names).each do |fixture_name| file_name = fixture_name.to_s 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
791 792 793 |
# File 'activerecord/lib/active_record/fixtures.rb', line 791 def set_fixture_class(class_names = {}) self.fixture_class_names = self.fixture_class_names.merge(class_names) end |
#setup_fixture_accessors(fixture_names = nil) ⇒ Object
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 |
# File 'activerecord/lib/active_record/fixtures.rb', line 828 def setup_fixture_accessors(fixture_names = nil) fixture_names = Array.wrap(fixture_names || fixture_table_names) methods = Module.new do fixture_names.each do |fixture_name| fixture_name = fixture_name.to_s.tr('./', '_') define_method(fixture_name) do |*fixtures| force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload @fixture_cache[fixture_name] ||= {} instances = fixtures.map do |fixture| @fixture_cache[fixture_name].delete(fixture) if force_reload if @loaded_fixtures[fixture_name][fixture.to_s] ActiveRecord::IdentityMap.without do @fixture_cache[fixture_name][fixture] ||= @loaded_fixtures[fixture_name][fixture.to_s].find end else raise StandardError, "No fixture with name '#{fixture}' found for table '#{fixture_name}'" end end instances.size == 1 ? instances.first : instances end private fixture_name end end include methods end |
#try_to_load_dependency(file_name) ⇒ Object
808 809 810 811 812 813 814 815 816 817 818 |
# File 'activerecord/lib/active_record/fixtures.rb', line 808 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 if ActiveRecord::Base.logger ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.} \n\n #{e.backtrace.join("\n")}") end end |
#uses_transaction(*methods) ⇒ Object
859 860 861 862 |
# File 'activerecord/lib/active_record/fixtures.rb', line 859 def uses_transaction(*methods) @uses_transaction = [] unless defined?(@uses_transaction) @uses_transaction.concat methods.map { |m| m.to_s } end |
#uses_transaction?(method) ⇒ Boolean
864 865 866 867 |
# File 'activerecord/lib/active_record/fixtures.rb', line 864 def uses_transaction?(method) @uses_transaction = [] unless defined?(@uses_transaction) @uses_transaction.include?(method.to_s) end |