Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect additional essence attributes during ingredients migration #2154

Merged
merged 2 commits into from
Jul 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lib/alchemy/upgrader/tasks/ingredients_migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class IngredientsMigrator < Thor
def create_ingredients
Alchemy::Deprecation.silence do
elements_with_ingredients = Alchemy::ElementDefinition.all.select { |d| d.key?(:ingredients) }
if ENV["ONLY"]
elements_with_ingredients = elements_with_ingredients.select { |d| d[:name].in? ENV["ONLY"].split(",") }
end
# eager load all elements that have ingredients defined but no ingredient records yet.
all_elements = Alchemy::Element
.named(elements_with_ingredients.map { |d| d[:name] })
Expand All @@ -22,21 +25,26 @@ def create_ingredients
puts "-- Creating ingredients for #{elements.count} #{element_definition[:name]}(s)"
elements.each do |element|
Alchemy::Element.transaction do
element.ingredients = element_definition[:ingredients].map do |ingredient_definition|
element_definition[:ingredients].each do |ingredient_definition|
content = element.content_by_name(ingredient_definition[:role])
next unless content

essence = content.essence
ingredient = Alchemy::Ingredient.build(role: ingredient_definition[:role], element: element)
belongs_to_associations = content.essence.class.reflect_on_all_associations(:belongs_to)
belongs_to_associations = essence.class.reflect_on_all_associations(:belongs_to)
if belongs_to_associations.any?
ingredient.related_object = content.essence.public_send(belongs_to_associations.first.name)
ingredient.related_object = essence.public_send(belongs_to_associations.first.name)
else
ingredient.value = content.ingredient
end
content.destroy!
ingredient.class.stored_attributes.fetch(:data, []).each do |attr|
value = essence.public_send(attr)
ingredient.public_send("#{attr}=", value)
end
print "."
ingredient
end.compact
ingredient.save!
content.destroy!
end
end
end
puts "\n"
Expand Down