Skip to content

Commit

Permalink
Deprecate element dom_id and dom_id_class
Browse files Browse the repository at this point in the history
Please pass an explicit `id` to the `element_view_for` helper instead.
  • Loading branch information
tvdeyen committed Sep 3, 2024
1 parent 5d27ff3 commit 761e20f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/models/alchemy/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
1 change: 1 addition & 0 deletions app/models/alchemy/element/dom_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion app/models/alchemy/element/presenters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 23 additions & 6 deletions spec/models/alchemy/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 761e20f

Please sign in to comment.