Class: LiveFixtures::Import::Fixtures
- Inherits:
-
Object
- Object
- LiveFixtures::Import::Fixtures
- Defined in:
- lib/live_fixtures/import/fixtures.rb
Instance Method Summary collapse
-
#each_table_row_with_label {|table_name, label, row| ... } ⇒ Object
Rewritten to take advantage of @label_to_id instead of AR::FixtureSet#identify, and to make an iterator.
-
#initialize(connection, table_name, class_name, filepath, label_to_id, skip_missing_refs: false) ⇒ Fixtures
constructor
A new instance of Fixtures.
- #model_connection ⇒ Object
Constructor Details
#initialize(connection, table_name, class_name, filepath, label_to_id, skip_missing_refs: false) ⇒ Fixtures
Returns a new instance of Fixtures.
19 20 21 22 23 24 25 26 |
# File 'lib/live_fixtures/import/fixtures.rb', line 19 def initialize(connection, table_name, class_name, filepath, label_to_id, skip_missing_refs: false) @skip_missing_refs = skip_missing_refs @ar_fixtures = ActiveRecord::FixtureSet.new connection, table_name, class_name, filepath @label_to_id = label_to_id end |
Instance Method Details
#each_table_row_with_label {|table_name, label, row| ... } ⇒ Object
Rewritten to take advantage of @label_to_id instead of AR::FixtureSet#identify, and to make an iterator.
Iterator which yields [table_name, label, row] for each fixture (and for any implicit join table records). The block is expected to insert the row and update @label_to_id with the record’s newly assigned id.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/live_fixtures/import/fixtures.rb', line 38 def each_table_row_with_label join_table_rows = Hash.new { |h,table| h[table] = [] } fixtures.map do |label, fixture| row = fixture.to_hash reflection_class = reflection_class_for row reflection_class.reflect_on_all_associations.each do |association| next unless row[association.name.to_s] case association.macro when :belongs_to maybe_convert_association_to_foreign_key row, association when :has_and_belongs_to_many join_table_name = association.join_table targets = row.delete(association.name.to_s) targets = targets.split(/\s*,\s*/) unless targets.is_a?(Array) join_table_rows[join_table_name] << { targets: targets, association: association, label: label } end end yield [table_name, label, row] end join_table_rows.each do |table_name, rows| rows.each do |targets:, association:, label:| targets.each do |target| assoc_fk = @label_to_id[target] || target row = { association.foreign_key => fetch_id_for_label(label), association.association_foreign_key => assoc_fk } yield [table_name, NO_LABEL, row] end end end end |
#model_connection ⇒ Object
80 81 82 |
# File 'lib/live_fixtures/import/fixtures.rb', line 80 def model_connection model_class.connection if model_class.respond_to? :connection end |