Methods
- #
- C
- E
- F
- I
- N
- R
- S
- T
Constants
MAX_ID | = | 2 ** 30 - 1 |
Attributes
[R] | table_name | |
[R] | name | |
[R] | fixtures | |
[R] | model_class |
Class Public methods
# File activerecord/lib/active_record/fixtures.rb, line 456 def self.create_fixtures(fixtures_directory, table_names, class_names = {}) table_names = [table_names].flatten.map { |n| n.to_s } table_names.each { |n| class_names[n.tr('/', '_').to_sym] = n.classify if n.include?('/') } # FIXME: Apparently JK uses this. connection = block_given? ? yield : ActiveRecord::Base.connection files_to_read = table_names.reject { |table_name| fixture_is_cached?(connection, table_name) } unless files_to_read.empty? connection.disable_referential_integrity do fixtures_map = {} fixture_files = files_to_read.map do |path| table_name = path.tr '/', '_' fixtures_map[path] = ActiveRecord::Fixtures.new( connection, table_name, class_names[table_name.to_sym] || table_name.classify, File.join(fixtures_directory, path)) end all_loaded_fixtures.update(fixtures_map) connection.transaction(:requires_new => true) do fixture_files.each do |ff| conn = ff.model_class.respond_to?(:connection) ? ff.model_class.connection : connection table_rows = ff.table_rows table_rows.keys.each do |table| conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete' end table_rows.each do |table_name,rows| rows.each do |row| conn.insert_fixture(row, table_name) end end end # Cap primary key sequences to max(pk). if connection.respond_to?(:reset_pk_sequence!) table_names.each do |table_name| connection.reset_pk_sequence!(table_name.tr('/', '_')) end end end cache_fixtures(connection, fixtures_map) end end cached_fixtures(connection, table_names) end
Returns a consistent, platform-independent identifier for label. Identifiers are positive integers less than 2^32.
# File activerecord/lib/active_record/fixtures.rb, line 447 def self.instantiate_all_loaded_fixtures(object, load_instances = true) all_loaded_fixtures.each do |table_name, fixtures| ActiveRecord::Fixtures.instantiate_fixtures(object, table_name, fixtures, load_instances) end end
# File activerecord/lib/active_record/fixtures.rb, line 435 def self.instantiate_fixtures(object, fixture_name, fixtures, load_instances = true) if load_instances fixtures.each do |name, fixture| begin object.instance_variable_set "@#{name}", fixture.find rescue FixtureClassNotFound nil end end end end
# File activerecord/lib/active_record/fixtures.rb, line 523 def initialize(connection, table_name, class_name, fixture_path) @connection = connection @table_name = table_name @fixture_path = fixture_path @name = table_name # preserve fixture base name @class_name = class_name @fixtures = ActiveSupport::OrderedHash.new @table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}" # Should be an AR::Base type class if class_name.is_a?(Class) @table_name = class_name.table_name @connection = class_name.connection @model_class = class_name else @model_class = class_name.constantize rescue nil end read_fixture_files end
Instance Public methods
Return a hash of rows to be inserted. The key is the table, the value is a list of rows to insert to that table.
# File activerecord/lib/active_record/fixtures.rb, line 563 def table_rows now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now now = now.to_s(:db) # allow a standard key to be used for doing defaults in YAML fixtures.delete('DEFAULTS') # track any join tables we need to insert later rows = Hash.new { |h,table| h[table] = [] } rows[table_name] = fixtures.map do |label, fixture| row = fixture.to_hash if model_class && model_class < ActiveRecord::Base # fill in timestamp columns if they aren't specified and the model is set to record_timestamps if model_class.record_timestamps timestamp_column_names.each do |name| row[name] = now unless row.key?(name) end end # interpolate the fixture label row.each do |key, value| row[key] = label if value == "$LABEL" end # generate a primary key if necessary if has_primary_key_column? && !row.include?(primary_key_name) row[primary_key_name] = ActiveRecord::Fixtures.identify(label) end # If STI is used, find the correct subclass for association reflection reflection_class = if row.include?(inheritance_column_name) row[inheritance_column_name].constantize rescue model_class else model_class end reflection_class.reflect_on_all_associations.each do |association| case association.macro when :belongs_to # Do not replace association name with association foreign key if they are named the same fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s if association.name.to_s != fk_name && value = row.delete(association.name.to_s) if association.options[:polymorphic] && value.sub!(/\s*\(([^\)]*)\)\s*$/, "") # support polymorphic belongs_to as "label (Type)" row[association.foreign_type] = $1 end row[fk_name] = ActiveRecord::Fixtures.identify(value) end when :has_and_belongs_to_many if (targets = row.delete(association.name.to_s)) targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/) table_name = association.options[:join_table] rows[table_name].concat targets.map { |target| { association.foreign_key => row[primary_key_name], association.association_foreign_key => ActiveRecord::Fixtures.identify(target) } } end end end end row end rows end