lib/alchemy/upgrader/tasks/ingredients_migrator.rb in alchemy_cms-6.0.4 vs lib/alchemy/upgrader/tasks/ingredients_migrator.rb in alchemy_cms-6.0.5

- old
+ new

@@ -22,39 +22,49 @@ elements_with_ingredients.map do |element_definition| elements = all_elements.select { |e| e.name == element_definition[:name] } if elements.any? puts "-- Creating ingredients for #{elements.count} #{element_definition[:name]}(s)" elements.each do |element| - Alchemy::Element.transaction do - element_definition[:ingredients].each do |ingredient_definition| - content = element.content_by_name(ingredient_definition[:role]) - next unless content - - essence = content.essence - ingredient = element.ingredients.build( - role: ingredient_definition[:role], - type: Alchemy::Ingredient.normalize_type(ingredient_definition[:type]), - ) - belongs_to_associations = essence.class.reflect_on_all_associations(:belongs_to) - if belongs_to_associations.any? - ingredient.related_object = essence.public_send(belongs_to_associations.first.name) - else - ingredient.value = content.ingredient - end - data = ingredient.class.stored_attributes.fetch(:data, []).each_with_object({}) do |attr, d| - d[attr] = essence.public_send(attr) - end - ingredient.data = data - print "." - ingredient.save! - content.destroy! - end - end + MigrateElementIngredients.call(element) + print "." end puts "\n" else puts "-- No #{element_definition[:name]} elements found for migration." end + end + end + end + end + + class MigrateElementIngredients + def self.call(element) + Alchemy::Element.transaction do + element.definition[:ingredients].each do |ingredient_definition| + ingredient = element.ingredients.build( + role: ingredient_definition[:role], + type: Alchemy::Ingredient.normalize_type(ingredient_definition[:type]), + ) + + content = element.content_by_name(ingredient_definition[:role]) + if content + essence = content.essence + if essence + belongs_to_associations = essence.class.reflect_on_all_associations(:belongs_to) + if belongs_to_associations.any? + ingredient.related_object = essence.public_send(belongs_to_associations.first.name) + else + ingredient.value = content.ingredient + end + data = ingredient.class.stored_attributes.fetch(:data, []).each_with_object({}) do |attr, d| + d[attr] = essence.public_send(attr) + end + ingredient.data = data + end + content.destroy! + end + + ingredient.save! end end end end end