From b0664cf209c1958c48b972890eb1ec659349368d Mon Sep 17 00:00:00 2001
From: Thomas von Deyen <thomas@vondeyen.com>
Date: Tue, 19 Jan 2021 16:26:02 +0100
Subject: [PATCH 1/2] Add deprecated attribute to element serializer

This attrobute is a new feature that was introduced in Alchemy 5.2

We use the definition hash here instead of the Element#deprecated? method
because we still support older versions of Alchemy
and a hash simply returns nil for an unkown key
---
 .../alchemy/json_api/element_serializer.rb            |  5 +++++
 spec/dummy/config/alchemy/elements.yml                |  3 +++
 .../alchemy/json_api/element_serializer_spec.rb       | 11 +++++++++++
 3 files changed, 19 insertions(+)

diff --git a/app/serializers/alchemy/json_api/element_serializer.rb b/app/serializers/alchemy/json_api/element_serializer.rb
index 5010864..9383a71 100644
--- a/app/serializers/alchemy/json_api/element_serializer.rb
+++ b/app/serializers/alchemy/json_api/element_serializer.rb
@@ -11,6 +11,11 @@ class ElementSerializer
         :created_at,
         :updated_at,
       )
+
+      attribute :deprecated do |element|
+        !!element.definition[:deprecated]
+      end
+
       belongs_to :parent_element, record_type: :element, serializer: self
 
       belongs_to :page, record_type: :page, serializer: ::Alchemy::JsonApi::PageSerializer
diff --git a/spec/dummy/config/alchemy/elements.yml b/spec/dummy/config/alchemy/elements.yml
index 3d2d940..9a4e010 100644
--- a/spec/dummy/config/alchemy/elements.yml
+++ b/spec/dummy/config/alchemy/elements.yml
@@ -160,3 +160,6 @@
   fixed: true
   unique: true
   nestable_elements: [text]
+
+- name: old
+  deprecated: true
diff --git a/spec/serializers/alchemy/json_api/element_serializer_spec.rb b/spec/serializers/alchemy/json_api/element_serializer_spec.rb
index 17b6180..32be395 100644
--- a/spec/serializers/alchemy/json_api/element_serializer_spec.rb
+++ b/spec/serializers/alchemy/json_api/element_serializer_spec.rb
@@ -27,9 +27,20 @@
       expect(subject[:created_at]).to eq(element.created_at)
       expect(subject[:updated_at]).to eq(element.updated_at)
       expect(subject[:position]).to eq(element.position)
+      expect(subject[:deprecated]).to eq(false)
       expect(subject.keys).not_to include(:tag_list, :display_name)
     end
 
+    context "a deprecated element" do
+      let(:element) do
+        FactoryBot.create(:alchemy_element, name: "old")
+      end
+
+      it "has deprecated attribute set to true" do
+        expect(subject[:deprecated]).to eq(true)
+      end
+    end
+
     context "with admin set to true" do
       let(:options) { { params: { admin: true } } }
 

From 8f59fd76a5dd8873efb9b5c590945f0093b6ffac Mon Sep 17 00:00:00 2001
From: Thomas von Deyen <thomas@vondeyen.com>
Date: Tue, 19 Jan 2021 16:33:00 +0100
Subject: [PATCH 2/2] Add deprecated attribute to essence serializer

In Alchemy 5.2 contents can be deprecated. Since this is just a Hash key we can
simply use it even for older versions of Alchemy
---
 lib/alchemy/json_api/essence_serializer.rb               | 3 +++
 .../test_support/essence_serializer_behaviour.rb         | 9 +++++++++
 spec/dummy/config/alchemy/elements.yml                   | 1 +
 3 files changed, 13 insertions(+)

diff --git a/lib/alchemy/json_api/essence_serializer.rb b/lib/alchemy/json_api/essence_serializer.rb
index aacf410..08a1e58 100644
--- a/lib/alchemy/json_api/essence_serializer.rb
+++ b/lib/alchemy/json_api/essence_serializer.rb
@@ -11,6 +11,9 @@ def self.included(klass)
         klass.attribute :role do |essence|
           essence.content.name
         end
+        klass.attribute :deprecated do |essence|
+          !!essence.content.definition[:deprecated]
+        end
       end
     end
   end
diff --git a/lib/alchemy/json_api/test_support/essence_serializer_behaviour.rb b/lib/alchemy/json_api/test_support/essence_serializer_behaviour.rb
index a96de6b..39f63fd 100644
--- a/lib/alchemy/json_api/test_support/essence_serializer_behaviour.rb
+++ b/lib/alchemy/json_api/test_support/essence_serializer_behaviour.rb
@@ -5,6 +5,15 @@
 
     it "has the right keys and values" do
       expect(subject).to have_key(:ingredient)
+      expect(subject[:deprecated]).to be(false)
+    end
+
+    context "a deprecated content" do
+      let(:content) { FactoryBot.create(:alchemy_content, name: "intro", element: element) }
+
+      it "has deprecated attribute set to true" do
+        expect(subject[:deprecated]).to eq(true)
+      end
     end
   end
 
diff --git a/spec/dummy/config/alchemy/elements.yml b/spec/dummy/config/alchemy/elements.yml
index 9a4e010..d8fd2d6 100644
--- a/spec/dummy/config/alchemy/elements.yml
+++ b/spec/dummy/config/alchemy/elements.yml
@@ -14,6 +14,7 @@
   contents:
   - name: intro
     type: EssenceText
+    deprecated: true
   - name: headline
     type: EssenceText
     settings: