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.

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VALIDATION_FAILURE_ACTIONS =

Validation failure actions supported by the Active Record adapter.

Returns:

  • (Array<Symbol>)
%i[raise warn].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|configuration| ... } ⇒ ActiveRecord

Create a new Active Record adapter.

Yields:

Yield Parameters:



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

def initialize
  @configuration = Configuration.new
  yield configuration if block_given?
end

Instance Attribute Details

#configurationPlanter::Adapters::ActiveRecord::Configuration (readonly)

The adapter configuration.



64
65
66
# File 'lib/planter/adapters/active_record.rb', line 64

def configuration
  @configuration
end

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)


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

def create_record(context:, lookup_attributes:, create_attributes:)
  if (model = model(context))
    create_model_record(model, context, lookup_attributes, 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)


119
120
121
# File 'lib/planter/adapters/active_record.rb', line 119

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)


105
106
107
# File 'lib/planter/adapters/active_record.rb', line 105

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>)


129
130
131
# File 'lib/planter/adapters/active_record.rb', line 129

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>)


137
138
139
140
141
# File 'lib/planter/adapters/active_record.rb', line 137

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