Skip to content

Commit

Permalink
Merge pull request #33 from AlchemyCMS/fix-nested-elements
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen authored Jan 21, 2021
2 parents 1c9008f + b14e52e commit cc53887
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/models/alchemy/json_api/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions app/models/alchemy/json_api/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
26 changes: 7 additions & 19 deletions spec/models/alchemy/json_api/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -179,19 +171,15 @@
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)
end
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)
Expand Down
28 changes: 20 additions & 8 deletions spec/serializers/alchemy/json_api/page_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cc53887

Please sign in to comment.