Class: Planter::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Planter::Adapters::ActiveRecord
- 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
-
#create_record(context:, lookup_attributes:, create_attributes:) ⇒ Object
Create a record unless one already exists.
-
#foreign_key(context:) ⇒ String, Symbol
Return the foreign key used to assign a parent id on a child record.
-
#parent_ids(context:) ⇒ Array
Return the parent ids to use when seeding child records.
-
#table_columns(context:) ⇒ Array<String>
Return native table columns for the table being seeded.
-
#table_names ⇒ Array<String>
Return application table names that can have seeders generated.
Instance Method Details
#create_record(context:, lookup_attributes:, create_attributes:) ⇒ Object
Create a record unless one already exists.
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.
70 71 72 |
# File 'lib/planter/adapters/active_record.rb', line 70 def foreign_key(context:) (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.
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.
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_names ⇒ Array<String>
Return application table names that can have seeders generated.
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 |