From 761e20ffa4c1f55865eee45dc1949cd965d3092c Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 2 Sep 2024 16:34:35 +0200 Subject: [PATCH] Deprecate element dom_id and dom_id_class Please pass an explicit `id` to the `element_view_for` helper instead. --- app/models/alchemy/element.rb | 6 +++++ app/models/alchemy/element/dom_id.rb | 1 + app/models/alchemy/element/presenters.rb | 5 +++- spec/models/alchemy/element_spec.rb | 29 +++++++++++++++++++----- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/models/alchemy/element.rb b/app/models/alchemy/element.rb index d66f38afff..6c4ee2afe3 100644 --- a/app/models/alchemy/element.rb +++ b/app/models/alchemy/element.rb @@ -136,15 +136,21 @@ def new(attributes = {}) # The class responsible for the +dom_id+ of elements. # Defaults to +Alchemy::Element::DomId+. + # @deprecated def dom_id_class + if caller.none? { |l| l =~ Regexp.new("alchemy/element/presenters.rb:87:in `dom_id'") } + Alchemy::Deprecation.warn("dom_id_class is deprecated and will be removed from Alchemy 8.0. Please pass an id to the element_view_for helper instead.") + end @_dom_id_class || DomId end # Register a custom +DomId+ class responsible for the +dom_id+ of elements. # Defaults to +Alchemy::Element::DomId+. + # @deprecated def dom_id_class=(klass) @_dom_id_class = klass end + deprecate :dom_id_class=, deprecator: Alchemy::Deprecation # This methods does a copy of source and all its ingredients. # diff --git a/app/models/alchemy/element/dom_id.rb b/app/models/alchemy/element/dom_id.rb index 589ca0a4ba..8f2ea84845 100644 --- a/app/models/alchemy/element/dom_id.rb +++ b/app/models/alchemy/element/dom_id.rb @@ -11,6 +11,7 @@ module Alchemy # # Alchemy::Element.dom_id_class = MyDomIdClass # + # @deprecated Use a headline ingredient with anchor setting instead. class Element < BaseRecord class DomId def initialize(element) diff --git a/app/models/alchemy/element/presenters.rb b/app/models/alchemy/element/presenters.rb index 85d6733c83..3e46aa5530 100644 --- a/app/models/alchemy/element/presenters.rb +++ b/app/models/alchemy/element/presenters.rb @@ -79,8 +79,11 @@ def display_name_with_preview_text(maxlength = 30) end # Returns a dom id used for elements html id tag. - # + # @deprecated def dom_id + if caller.none? { |l| l =~ Regexp.new("alchemy/elements_block_helper.rb:117:in `element_view_for'") } + Alchemy::Deprecation.warn("dom_id is deprecated and will be removed from Alchemy 8.0. Please pass an id to the element_view_for helper instead.") + end self.class.dom_id_class.new(self).call end diff --git a/spec/models/alchemy/element_spec.rb b/spec/models/alchemy/element_spec.rb index fb1348073d..e2068506f5 100644 --- a/spec/models/alchemy/element_spec.rb +++ b/spec/models/alchemy/element_spec.rb @@ -127,24 +127,36 @@ module Alchemy end describe ".dom_id_class" do - it "defaults to Alchemy::Element::DomId" do + it "defaults to Alchemy::Element::DomId", :silence_deprecations do expect(described_class.dom_id_class).to eq(Alchemy::Element::DomId) end + + it "warns about deprecation" do + expect(Alchemy::Deprecation).to receive(:warn) + described_class.dom_id_class + end end describe ".dom_id_class=" do let(:dummy_dom_id) { Class.new } around do |example| - default_class = described_class.dom_id_class - described_class.dom_id_class = dummy_dom_id - example.run - described_class.dom_id_class = default_class + Alchemy::Deprecation.silence do + default_class = described_class.dom_id_class + described_class.dom_id_class = dummy_dom_id + example.run + described_class.dom_id_class = default_class + end end it "sets the dom id class" do expect(described_class.dom_id_class).to eq(dummy_dom_id) end + + it "warns about deprecation" do + expect(Alchemy::Deprecation).to receive(:warn) + described_class.dom_id_class + end end describe ".copy" do @@ -473,10 +485,15 @@ module Alchemy describe "#dom_id" do let(:element) { build_stubbed(:alchemy_element, position: 1) } - it "calls dom id class" do + it "calls dom id class", :silence_deprecations do expect(Alchemy::Element.dom_id_class).to receive(:new).with(element).and_call_original element.dom_id end + + it "warns about deprecation" do + expect(Alchemy::Deprecation).to receive(:warn) + element.dom_id + end end describe "#preview_text" do