From b14e52e2b2bb99c61293fb7989f0af8e586a7829 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 21 Jan 2021 17:48:10 +0100 Subject: [PATCH] Fixes element filtering for nested elements --- app/models/alchemy/json_api/element.rb | 4 +-- app/models/alchemy/json_api/page.rb | 14 ++++++---- spec/models/alchemy/json_api/page_spec.rb | 26 +++++------------ .../alchemy/json_api/page_serializer_spec.rb | 28 +++++++++++++------ 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/app/models/alchemy/json_api/element.rb b/app/models/alchemy/json_api/element.rb index 62e8611..5a6be0f 100644 --- a/app/models/alchemy/json_api/element.rb +++ b/app/models/alchemy/json_api/element.rb @@ -12,14 +12,14 @@ class Element < BaseRecord scope :available, -> { where(public: true).where.not(position: nil) } def parent_element - page.elements.detect do |element| + page.all_elements.detect do |element| element.id == parent_element_id end end def nested_elements @_nested_elements ||= begin - page.elements.select do |element| + page.all_elements.select do |element| element.parent_element_id == id end end diff --git a/app/models/alchemy/json_api/page.rb b/app/models/alchemy/json_api/page.rb index b3c5128..b800dfc 100644 --- a/app/models/alchemy/json_api/page.rb +++ b/app/models/alchemy/json_api/page.rb @@ -23,17 +23,13 @@ class Page < BaseRecord # The top level public, non-fixed elements of this page that - if present - # contains their nested_elements. def elements - @_elements ||= all_elements.select do |element| - !element.fixed? || element.parent_element_id.nil? - end + @_elements ||= first_level_elements.reject(&:fixed?) end # The top level public, fixed elements of this page that - if present - # contains their nested_elements. def fixed_elements - @_fixed_elements ||= all_elements.select do |element| - element.fixed? || element.parent_element_id.nil? - end + @_fixed_elements ||= first_level_elements.select(&:fixed?) end def element_ids @@ -43,6 +39,12 @@ def element_ids def fixed_element_ids @_fixed_element_ids ||= fixed_elements.map(&:id) end + + private + + def first_level_elements + @_first_level_elements ||= all_elements.reject(&:parent_element_id) + end end end end diff --git a/spec/models/alchemy/json_api/page_spec.rb b/spec/models/alchemy/json_api/page_spec.rb index cd2a070..0750eb1 100644 --- a/spec/models/alchemy/json_api/page_spec.rb +++ b/spec/models/alchemy/json_api/page_spec.rb @@ -116,6 +116,7 @@ let!(:element_1) { FactoryBot.create(:alchemy_element, page: page) } let!(:element_2) { FactoryBot.create(:alchemy_element, page: page) } let!(:element_3) { FactoryBot.create(:alchemy_element, page: page) } + let!(:fixed_element) { FactoryBot.create(:alchemy_element, page: page, fixed: true) } before do element_3.move_to_top @@ -128,25 +129,15 @@ end context "with nestable elements" do - let(:nestable_element) { FactoryBot.create(:alchemy_element, :with_nestable_elements) } - - before do - nestable_element.nested_elements << FactoryBot.create(:alchemy_element, name: "slide") - page.elements << nestable_element - end + let!(:nested_element) { FactoryBot.create(:alchemy_element, page: page, parent_element: element_3) } it "does not contain nested elements of an element" do - expect(nestable_element.nested_elements).to_not be_empty - expect(element_ids).to_not include(nestable_element.nested_elements.first.id) + expect(element_ids).to_not include(nested_element.id) end end context "with trashed elements" do - let(:trashed_element) { FactoryBot.create(:alchemy_element, page: page) } - - before do - trashed_element.trash! - end + let!(:trashed_element) { FactoryBot.create(:alchemy_element, page: page).tap(&:trash!) } it "does not contain trashed elements" do expect(element_ids).to_not include(trashed_element.id) @@ -167,6 +158,7 @@ let!(:element_1) { FactoryBot.create(:alchemy_element, fixed: true, page: page) } let!(:element_2) { FactoryBot.create(:alchemy_element, fixed: true, page: page) } let!(:element_3) { FactoryBot.create(:alchemy_element, fixed: true, page: page) } + let!(:not_fixed) { FactoryBot.create(:alchemy_element, fixed: false, page: page) } before do element_3.move_to_top @@ -179,11 +171,7 @@ end context "with trashed fixed elements" do - let(:trashed_element) { FactoryBot.create(:alchemy_element, page: page, fixed: true) } - - before do - trashed_element.trash! - end + let!(:trashed_element) { FactoryBot.create(:alchemy_element, page: page, fixed: true).tap(&:trash!) } it "does not contain trashed fixed elements" do expect(fixed_elements).to_not include(trashed_element.id) @@ -191,7 +179,7 @@ end context "with hidden fixed elements" do - let(:hidden_element) { FactoryBot.create(:alchemy_element, page: page, fixed: true, public: false) } + let!(:hidden_element) { FactoryBot.create(:alchemy_element, page: page, fixed: true, public: false) } it "does not contain hidden fixed elements" do expect(fixed_elements).to_not include(hidden_element.id) diff --git a/spec/serializers/alchemy/json_api/page_serializer_spec.rb b/spec/serializers/alchemy/json_api/page_serializer_spec.rb index d71a5f5..b6165e5 100644 --- a/spec/serializers/alchemy/json_api/page_serializer_spec.rb +++ b/spec/serializers/alchemy/json_api/page_serializer_spec.rb @@ -44,14 +44,26 @@ subject { serializer.serializable_hash[:data][:relationships] } - it "has the right keys and values, and does not include trashed or hidden elements" do - expect(subject[:elements]).to eq( - data: [ - { id: element.id.to_s, type: :element }, - { id: fixed_element.id.to_s, type: :element }, - ], - ) - expect(subject[:language]).to eq(data: { id: page.language_id.to_s, type: :language }) + describe "elements" do + it "does not include trashed, fixed or hidden elements" do + expect(subject[:elements]).to eq( + data: [ + { id: element.id.to_s, type: :element }, + ], + ) + expect(subject[:language]).to eq(data: { id: page.language_id.to_s, type: :language }) + end + end + + describe "fixed_elements" do + it "does not include trashed, non-fixed or hidden elements" do + expect(subject[:fixed_elements]).to eq( + data: [ + { id: fixed_element.id.to_s, type: :element }, + ], + ) + expect(subject[:language]).to eq(data: { id: page.language_id.to_s, type: :language }) + end end end end