Skip to content

Commit

Permalink
Merge pull request #28 from AlchemyCMS/remove-deprecated-elements-fro…
Browse files Browse the repository at this point in the history
…m-everywhere

Remove deprecated elements from all element relations
  • Loading branch information
tvdeyen authored Jan 19, 2021
2 parents 9f6f941 + a8bc830 commit 66c7e07
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
4 changes: 3 additions & 1 deletion app/serializers/alchemy/json_api/element_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class ElementSerializer
element.contents.reject { |c| !!c.try(:deprecated?) }.map!(&:essence)
end

has_many :nested_elements, record_type: :element, serializer: self
has_many :nested_elements, record_type: :element, serializer: self do |element|
element.nested_elements.reject { |c| !!c.try(:deprecated?) }
end

with_options if: ->(_, params) { params[:admin] == true } do
attribute :tag_list
Expand Down
9 changes: 7 additions & 2 deletions app/serializers/alchemy/json_api/page_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class PageSerializer

belongs_to :language, record_type: :language, serializer: ::Alchemy::JsonApi::LanguageSerializer

has_many :elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer
has_many :fixed_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer
has_many :elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |page|
page.elements.reject { |e| !!e.try(:deprecated?) }
end

has_many :fixed_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |page|
page.fixed_elements.reject { |c| !!c.try(:deprecated?) }
end

has_many :all_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |page|
page.all_elements.select { |e| e.public? && !e.trashed? && !e.try(:deprecated?) }
Expand Down
15 changes: 13 additions & 2 deletions spec/serializers/alchemy/json_api/element_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# frozen_string_literal: true
require "rails_helper"
require "alchemy/test_support/factories"
require "alchemy/version"

RSpec.describe Alchemy::JsonApi::ElementSerializer do
let(:element) do
FactoryBot.create(
:alchemy_element,
autogenerate_contents: true,
tag_list: "Tag1,Tag2",
nested_elements: [nested_element],
nested_elements: [nested_element, deprecated_element],
parent_element: parent_element,
)
end
let(:nested_element) { FactoryBot.create(:alchemy_element) }
let(:parent_element) { FactoryBot.create(:alchemy_element) }
let(:deprecated_element) { FactoryBot.create(:alchemy_element, name: "old") }
let(:options) { {} }

subject(:serializer) { described_class.new(element, options) }
Expand Down Expand Up @@ -46,7 +48,16 @@
it "has the right keys and values" do
expect(subject[:page]).to eq(data: { id: element.page_id.to_s, type: :page })
expect(subject[:essences]).to eq(data: element.contents.map { |c| { id: c.essence_id.to_s, type: c.essence.class.name.demodulize.underscore.to_sym } })
expect(subject[:nested_elements]).to eq(data: [{ id: nested_element.id.to_s, type: :element }])
if Alchemy.gem_version >= Gem::Version.new("5.2.0.alpha")
expect(subject[:nested_elements]).to eq(data: [{ id: nested_element.id.to_s, type: :element }])
else
expect(subject[:nested_elements]).to eq(
data: [
{ id: nested_element.id.to_s, type: :element },
{ id: deprecated_element.id.to_s, type: :element },
],
)
end
expect(subject[:parent_element]).to eq(data: { id: parent_element.id.to_s, type: :element })
end
end
Expand Down
26 changes: 23 additions & 3 deletions spec/serializers/alchemy/json_api/page_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require "rails_helper"
require "alchemy/test_support/factories"
require "alchemy/version"

RSpec.describe Alchemy::JsonApi::PageSerializer do
let(:page) do
Expand Down Expand Up @@ -55,19 +56,38 @@
let(:element) { FactoryBot.create(:alchemy_element) }
let(:fixed_element) { FactoryBot.create(:alchemy_element, fixed: true) }
let(:trashed_element) { FactoryBot.create(:alchemy_element, :trashed) }
let(:deprecated_element) { FactoryBot.create(:alchemy_element, name: "old") }

subject { serializer.serializable_hash[:data][:relationships] }

before do
page.all_elements << element
page.all_elements << fixed_element
page.all_elements << trashed_element
page.all_elements << deprecated_element
trashed_element.trash!
end

it "has the right keys and values, and does not include trashed elements" do
expect(subject[:elements]).to eq(data: [{ id: element.id.to_s, type: :element }])
it "has the right keys and values, and does not include trashed, hidden or deprecated elements" do
if Alchemy.gem_version >= Gem::Version.new("5.2.0.alpha")
expect(subject[:elements]).to eq(data: [{ id: element.id.to_s, type: :element }])
expect(subject[:all_elements]).to eq(data: [{ id: element.id.to_s, type: :element }, { id: fixed_element.id.to_s, type: :element }])
else
expect(subject[:elements]).to eq(
data: [
{ id: element.id.to_s, type: :element },
{ id: deprecated_element.id.to_s, type: :element },
],
)
expect(subject[:all_elements]).to eq(
data: [
{ id: element.id.to_s, type: :element },
{ id: fixed_element.id.to_s, type: :element },
{ id: deprecated_element.id.to_s, type: :element },
],
)
end
expect(subject[:fixed_elements]).to eq(data: [{ id: fixed_element.id.to_s, type: :element }])
expect(subject[:all_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 })
end
end
Expand Down

0 comments on commit 66c7e07

Please sign in to comment.