From 84d1ed3009cb13130193bc3deb4df5aa23222004 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 26 May 2021 19:45:13 +0200 Subject: [PATCH] Init tinymce editor for ingredients --- app/models/alchemy/page/page_elements.rb | 9 +++ app/views/alchemy/admin/pages/edit.html.erb | 2 +- .../test_support/factories/element_factory.rb | 4 ++ spec/models/alchemy/page_spec.rb | 56 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/app/models/alchemy/page/page_elements.rb b/app/models/alchemy/page/page_elements.rb index 3cf7de10fa..2c2be7ca11 100644 --- a/app/models/alchemy/page/page_elements.rb +++ b/app/models/alchemy/page/page_elements.rb @@ -177,6 +177,15 @@ def richtext_contents_ids .collect(&:id) end + # Returns an array of all Richtext ingredients ids from not folded elements + # + def richtext_ingredients_ids + Alchemy::Ingredient.richtexts.joins(:element) + .where(Element.table_name => { page_version_id: draft_version.id, folded: false }) + .select(&:has_tinymce?) + .collect(&:id) + end + private # Looks in the page_layout descripion, if there are elements to autogenerate. diff --git a/app/views/alchemy/admin/pages/edit.html.erb b/app/views/alchemy/admin/pages/edit.html.erb index 0050d94385..7fa4a15d75 100644 --- a/app/views/alchemy/admin/pages/edit.html.erb +++ b/app/views/alchemy/admin/pages/edit.html.erb @@ -197,7 +197,7 @@ Alchemy.SortableElements(<%= @page.id %>, '<%= form_authenticity_token %>'); Alchemy.ElementEditors.init(); Alchemy.SelectBox('.element-editor'); - Alchemy.Tinymce.init(<%= @page.richtext_contents_ids.to_json %>); + Alchemy.Tinymce.init(<%= (@page.richtext_contents_ids + @page.richtext_ingredients_ids).to_json %>); $('#fixed-elements').tabs().tabs('paging', { follow: true, followOnSelect: true, diff --git a/lib/alchemy/test_support/factories/element_factory.rb b/lib/alchemy/test_support/factories/element_factory.rb index b0dbaafaf8..5d1dd1d6b5 100644 --- a/lib/alchemy/test_support/factories/element_factory.rb +++ b/lib/alchemy/test_support/factories/element_factory.rb @@ -28,5 +28,9 @@ trait :with_contents do autogenerate_contents { true } end + + trait :with_ingredients do + autogenerate_ingredients { true } + end end end diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index c6b6014e9a..ad907047ec 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -1933,6 +1933,62 @@ class AnotherUrlPathClass; end end end + describe "#richtext_ingredients_ids" do + let!(:page) { create(:alchemy_page) } + + let!(:expanded_element) do + create :alchemy_element, :with_ingredients, + name: "element_with_ingredients", + page_version: page.draft_version, + folded: false + end + + let!(:folded_element) do + create :alchemy_element, :with_ingredients, + name: "element_with_ingredients", + page_version: page.draft_version, + folded: true + end + + subject(:richtext_ingredients_ids) { page.richtext_ingredients_ids } + + it "returns ingredient ids for all expanded elements that have tinymce enabled" do + expanded_rtf_ingredients = expanded_element.ingredients.richtexts + expect(richtext_ingredients_ids).to eq(expanded_rtf_ingredients.pluck(:id)) + folded_rtf_ingredient = folded_element.ingredients.richtexts.first + expect(richtext_ingredients_ids).to_not include(folded_rtf_ingredient.id) + end + + context "with nested elements" do + let!(:nested_expanded_element) do + create :alchemy_element, :with_ingredients, + name: "element_with_ingredients", + page_version: page.draft_version, + parent_element: expanded_element, + folded: false + end + + let!(:nested_folded_element) do + create :alchemy_element, :with_ingredients, + name: "element_with_ingredients", + page_version: page.draft_version, + parent_element: folded_element, + folded: true + end + + it "returns ingredient ids for all expanded nested elements that have tinymce enabled" do + expanded_rtf_ingredients = expanded_element.ingredients.richtexts + nested_expanded_rtf_ingredients = nested_expanded_element.ingredients.richtexts + rtf_ingredient_ids = expanded_rtf_ingredients.pluck(:id) + nested_expanded_rtf_ingredients.pluck(:id) + expect(richtext_ingredients_ids.sort).to eq(rtf_ingredient_ids) + + nested_folded_rtf_ingredient = nested_folded_element.ingredients.richtexts.first + + expect(richtext_ingredients_ids).to_not include(nested_folded_rtf_ingredient.id) + end + end + end + describe "#fixed_attributes" do let(:page) { Alchemy::Page.new }