diff --git a/app/controllers/alchemy/admin/elements_controller.rb b/app/controllers/alchemy/admin/elements_controller.rb index 826eae9737..c9eeac749d 100644 --- a/app/controllers/alchemy/admin/elements_controller.rb +++ b/app/controllers/alchemy/admin/elements_controller.rb @@ -106,18 +106,14 @@ def fold def element_includes [ { - contents: { - essence: :ingredient_association, - }, + contents: :essence, ingredients: :related_object, }, :tags, { all_nested_elements: [ { - contents: { - essence: :ingredient_association, - }, + contents: :essence, ingredients: :related_object, }, :tags, diff --git a/app/controllers/alchemy/api/contents_controller.rb b/app/controllers/alchemy/api/contents_controller.rb index c864de5fe2..ee4a1dc227 100644 --- a/app/controllers/alchemy/api/contents_controller.rb +++ b/app/controllers/alchemy/api/contents_controller.rb @@ -46,11 +46,7 @@ def show private def content_includes - [ - { - essence: :ingredient_association, - }, - ] + %i[essence] end end end diff --git a/app/controllers/alchemy/api/elements_controller.rb b/app/controllers/alchemy/api/elements_controller.rb index 9fb6fe129f..168c242e0a 100644 --- a/app/controllers/alchemy/api/elements_controller.rb +++ b/app/controllers/alchemy/api/elements_controller.rb @@ -47,18 +47,14 @@ def element_includes { nested_elements: [ { - contents: { - essence: :ingredient_association, - }, + contents: :essence, ingredients: :related_object, }, :tags, ], }, { - contents: { - essence: :ingredient_association, - }, + contents: :essence, ingredients: :related_object, }, :tags, diff --git a/app/controllers/alchemy/api/pages_controller.rb b/app/controllers/alchemy/api/pages_controller.rb index 7a914fc41c..aa483648cf 100644 --- a/app/controllers/alchemy/api/pages_controller.rb +++ b/app/controllers/alchemy/api/pages_controller.rb @@ -112,17 +112,13 @@ def page_includes { nested_elements: [ { - contents: { - essence: :ingredient_association, - }, + contents: :essence, }, :tags, ], }, { - contents: { - essence: :ingredient_association, - }, + contents: :essence, }, :tags, ], diff --git a/lib/alchemy/essence.rb b/lib/alchemy/essence.rb index 0feb2fc97d..54f8e2ade4 100644 --- a/lib/alchemy/essence.rb +++ b/lib/alchemy/essence.rb @@ -3,18 +3,6 @@ require "active_record" module Alchemy #:nodoc: - # A bogus association that skips eager loading for essences not having an ingredient association - class IngredientAssociation < ActiveRecord::Associations::BelongsToAssociation - # Skip eager loading if called by Rails' preloader - def klass - if caller.any? { |line| line =~ /preloader\.rb/ } - nil - else - super - end - end - end - module Essence #:nodoc: def self.included(base) base.extend(ClassMethods) @@ -43,8 +31,6 @@ def acts_as_essence(options = {}) ingredient_column: "body", }.update(options) - @_classes_with_ingredient_association ||= [] - class_eval <<-RUBY, __FILE__, __LINE__ + 1 attr_writer :validation_errors include Alchemy::Essence::InstanceMethods @@ -87,18 +73,6 @@ def preview_text_column alias_method :#{configuration[:ingredient_column]}, :ingredient_association alias_method :#{configuration[:ingredient_column]}=, :ingredient_association= RUBY - - @_classes_with_ingredient_association << self - end - end - - # Overwrite ActiveRecords method to return a bogus association class that skips eager loading - # for essence classes that do not have an ingredient association - def _reflect_on_association(name) - if name == :ingredient_association && !in?(@_classes_with_ingredient_association) - OpenStruct.new(association_class: Alchemy::IngredientAssociation) - else - super end end diff --git a/lib/alchemy/test_support/essence_shared_examples.rb b/lib/alchemy/test_support/essence_shared_examples.rb index bd630f8f98..a89907151e 100644 --- a/lib/alchemy/test_support/essence_shared_examples.rb +++ b/lib/alchemy/test_support/essence_shared_examples.rb @@ -7,18 +7,6 @@ let(:content) { Alchemy::Content.new(name: "foo") } let(:content_definition) { { "name" => "foo" } } - describe "eager loading" do - before do - 2.times { described_class.create! } - end - - it "does not throw error if eager loaded" do - expect { - described_class.all.includes(:ingredient_association).to_a - }.to_not raise_error - end - end - it "touches the element after save" do element = FactoryBot.create(:alchemy_element) content = FactoryBot.create(:alchemy_content, element: element, essence: essence, essence_type: essence.class.name) diff --git a/lib/alchemy/upgrader/tasks/ingredients_migrator.rb b/lib/alchemy/upgrader/tasks/ingredients_migrator.rb index ab977ff569..6ee992c540 100644 --- a/lib/alchemy/upgrader/tasks/ingredients_migrator.rb +++ b/lib/alchemy/upgrader/tasks/ingredients_migrator.rb @@ -16,7 +16,7 @@ def create_ingredients # 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] }) - .includes(contents: { essence: :ingredient_association }) + .includes(contents: :essence) .left_outer_joins(:ingredients).where(alchemy_ingredients: { id: nil }) .to_a elements_with_ingredients.map do |element_definition|