Skip to content

Commit

Permalink
Init tinymce editor for ingredients
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed May 27, 2021
1 parent 87b4377 commit 84d1ed3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/models/alchemy/page/page_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/pages/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions lib/alchemy/test_support/factories/element_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
trait :with_contents do
autogenerate_contents { true }
end

trait :with_ingredients do
autogenerate_ingredients { true }
end
end
end
56 changes: 56 additions & 0 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit 84d1ed3

Please sign in to comment.