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.
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- VALIDATION_FAILURE_ACTIONS =
Validation failure actions supported by the Active Record adapter.
%i[raise warn].freeze
Instance Attribute Summary collapse
-
#configuration ⇒ Planter::Adapters::ActiveRecord::Configuration
readonly
The adapter configuration.
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.
-
#initialize {|configuration| ... } ⇒ ActiveRecord
constructor
Create a new Active Record adapter.
-
#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.
Constructor Details
#initialize {|configuration| ... } ⇒ ActiveRecord
Create a new Active Record adapter.
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
#configuration ⇒ Planter::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.
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.
119 120 121 |
# File 'lib/planter/adapters/active_record.rb', line 119 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.
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.
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_names ⇒ Array<String>
Return application table names that can have seeders generated.
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 |