Class: Planter::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/planter/adapters/active_record.rb

Overview

Default adapter for seeding Active Record tables.

When a table maps to an Active Record model, records are created through that model. Tables without a matching model, such as join tables, are seeded directly through the database connection.

Custom adapters should implement this public API:

  • create_record(context:, lookup_attributes:, create_attributes:)
  • parent_ids(context:)
  • foreign_key(context:)
  • table_columns(context:)
  • table_names

context is a Planter::SeedContext. Adapters are responsible for resolving those values into whatever persistence or reflection objects they need.

Instance Method Summary collapse

Instance Method Details

#create_record(context:, lookup_attributes:, create_attributes:) ⇒ Object

Create a record unless one already exists.

Parameters:

  • context (Planter::SeedContext)

    seeder configuration

  • lookup_attributes (Hash)

    attributes used to find the record

  • create_attributes (Hash)

    additional attributes used only when creating a new record

Returns:

  • (Object)


36
37
38
39
40
41
42
43
44
# File 'lib/planter/adapters/active_record.rb', line 36

def create_record(context:, lookup_attributes:, create_attributes:)
  if (model = model(context))
    model
      .where(lookup_attributes)
      .first_or_create!(create_attributes)
  else
    create_table_record(context, lookup_attributes, create_attributes)
  end
end

#foreign_key(context:) ⇒ String, Symbol

Return the foreign key used to assign a parent id on a child record.

The Active Record adapter resolves foreign keys through model associations. Tables without matching models should use a custom adapter for parent seeding.

Parameters:

Returns:

  • (String, Symbol)


70
71
72
# File 'lib/planter/adapters/active_record.rb', line 70

def foreign_key(context:)
  association_options(context).fetch(:foreign_key, "#{context.parent}_id")
end

#parent_ids(context:) ⇒ Array

Return the parent ids to use when seeding child records.

The Active Record adapter resolves parents through model associations. Tables without matching models should use a custom adapter for parent seeding.

Parameters:

Returns:

  • (Array)


56
57
58
# File 'lib/planter/adapters/active_record.rb', line 56

def parent_ids(context:)
  parent_model(context).constantize.pluck(primary_key(context))
end

#table_columns(context:) ⇒ Array<String>

Return native table columns for the table being seeded.

Parameters:

Returns:

  • (Array<String>)


80
81
82
# File 'lib/planter/adapters/active_record.rb', line 80

def table_columns(context:)
  ::ActiveRecord::Base.connection.columns(context.table_name).map(&:name)
end

#table_namesArray<String>

Return application table names that can have seeders generated.

Returns:

  • (Array<String>)


88
89
90
91
92
# File 'lib/planter/adapters/active_record.rb', line 88

def table_names
  ::ActiveRecord::Base.connection.tables.reject do |table|
    %w[ar_internal_metadata schema_migrations].include?(table)
  end
end