Skip to content

Commit

Permalink
Deprecate implicit DOM ID on elements in element_view_for
Browse files Browse the repository at this point in the history
Relying on the default DOM ID for your element is deprecated and will
be removed from Alchemy 6.1. If you do not need a DOM ID on your
elements you can just ignore this message. But if you NEED a DOM ID
on your elements, please pass the desired DOM ID as `id` option into
`element_view_for`.
  • Loading branch information
tvdeyen committed Sep 2, 2022
1 parent c03d64b commit bfcd895
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/helpers/alchemy/elements_block_helper.rb
Expand Up @@ -125,13 +125,21 @@ def ingredient_by_role(role)
# @option options :tags_formatter
# A lambda used for formatting the element's tags (see Alchemy::ElementsHelper::element_tags_attributes). Set to +false+ to not include tags in the wrapper element.
#
def element_view_for(element, options = {})
def element_view_for(element, opts = {})
options = {
tag: :div,
id: "#{element.name}_#{element.id}",
class: element.name,
tags_formatter: ->(tags) { tags.join(" ") },
}.merge(options)
}.merge(opts)

if opts[:id].nil?
Alchemy::Deprecation.warn(
"Relying on the default DOM ID for your '#{element.name}' element is deprecated and will be removed from Alchemy 6.1. " \
"If you do not need a DOM ID on your '#{element.name}' element you can just ignore this message. " \
"But if you NEED a DOM ID on your element, please pass the desired DOM ID as `id` option into `element_view_for`."
)
end

# capture inner template block
output = capture do
Expand Down
6 changes: 6 additions & 0 deletions spec/helpers/alchemy/elements_block_helper_spec.rb
Expand Up @@ -11,6 +11,12 @@ module Alchemy
let(:expected_wrapper_tag) { "div.#{element.name}##{element.name}_#{element.id}" }

describe "#element_view_for" do
around do |exmple|
Alchemy::Deprecation.silence do
exmple.run
end
end

it "should yield an instance of ElementViewHelper" do
expect { |b| element_view_for(element, &b) }.
to yield_with_args(ElementsBlockHelper::ElementViewHelper)
Expand Down
12 changes: 12 additions & 0 deletions spec/helpers/alchemy/elements_helper_spec.rb
Expand Up @@ -13,6 +13,12 @@ module Alchemy
end

describe "#render_element" do
around do |example|
Alchemy::Deprecation.silence do
example.run
end
end

subject { render_element(element) }

context "with nil element" do
Expand Down Expand Up @@ -79,6 +85,12 @@ module Alchemy
end

describe "#render_elements" do
around do |example|
Alchemy::Deprecation.silence do
example.run
end
end

subject { helper.render_elements(options) }

let(:page) { create(:alchemy_page, :public) }
Expand Down

0 comments on commit bfcd895

Please sign in to comment.