Class: LiveFixtures::Import::Fixtures

Inherits:
Object
  • Object
show all
Defined in:
lib/live_fixtures/import/fixtures.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, table_name, class_name, filepath, label_to_id, skip_missing_refs: false) ⇒ Fixtures

Returns a new instance of Fixtures.

Parameters:

  • connection (ActiveRecord::ConnectionAdapters::AbstractAdapter)

    connection to the database into which to import the data.

  • table_name (String)

    name of the database table to populate with models

  • class_name (Constant)

    the model’s class name

  • filepath (String)

    path to the yml file containing the fixtures

  • label_to_id (Hash{String => Int})

    map from a reference’s label to its new id.



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.

Yield Parameters:

  • table_name (String)

    the database table’s name

  • label (String)

    the label for the model being currently imported

  • row (Hash{String => Value})

    the model’s attributes to be imported

See Also:



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 |row|
      row[:targets].each do |target|
        assoc_fk = @label_to_id[target] || target
        row_with_fk = { row[:association].foreign_key             => fetch_id_for_label(row[:label]),
                        row[:association].association_foreign_key => assoc_fk }
        yield [table_name, NO_LABEL, row_with_fk]
      end
    end
  end
end

#model_connectionObject



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