Skip to content

Commit

Permalink
Deprecate element_dom_id helper
Browse files Browse the repository at this point in the history
Use `element.dom_id` instead.
  • Loading branch information
tvdeyen committed Sep 3, 2022
1 parent a4ed8e1 commit 8c1ab79
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/helpers/alchemy/elements_block_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def ingredient_by_role(role)
def element_view_for(element, options = {})
options = {
tag: :div,
id: element_dom_id(element),
id: element.dom_id,
class: element.name,
tags_formatter: ->(tags) { tags.join(" ") },
}.merge(options)
Expand Down
7 changes: 4 additions & 3 deletions app/helpers/alchemy/elements_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ def render_element(element, options = {}, counter = 1)
end

# Returns a string for the id attribute of a html element for the given element
# @deprecated
def element_dom_id(element)
return "" if element.nil?

"#{element.name}_#{element.id}".html_safe
element&.dom_id
end

deprecate element_dom_id: "element.dom_id", deprecator: Alchemy::Deprecation

# Renders the HTML tag attributes required for preview mode.
def element_preview_code(element)
tag_builder.tag_options(element_preview_code_attributes(element))
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/alchemy/url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def download_alchemy_attachment_url(attachment)

# Returns the full url containing host, page and anchor for the given element
def full_url_for_element(element)
"#{current_server}/#{element.page.urlname}##{element_dom_id(element)}"
"#{current_server}/#{element.page.urlname}##{element.dom_id}"
end
end
end
4 changes: 2 additions & 2 deletions app/views/alchemy/pages/show.rss.builder
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ xml.rss version: "2.0" do
if element.has_ingredient?("date")
xml.pubDate element.ingredient("date").to_s(:rfc822)
end
xml.link show_alchemy_page_url(@page, anchor: element_dom_id(element))
xml.guid show_alchemy_page_url(@page, anchor: element_dom_id(element))
xml.link show_alchemy_page_url(@page, anchor: "##{element.dom_id}")
xml.guid show_alchemy_page_url(@page, anchor: "##{element.dom_id}")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/alchemy/elements_block_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Alchemy
describe "ElementsBlockHelper" do
let(:page) { create(:alchemy_page, :public) }
let(:element) { create(:alchemy_element, page: page, tag_list: "foo, bar") }
let(:expected_wrapper_tag) { "div.#{element.name}##{element_dom_id(element)}" }
let(:expected_wrapper_tag) { "div.#{element.name}##{element.dom_id}" }

describe "#element_view_for" do
it "should yield an instance of ElementViewHelper" do
Expand Down
22 changes: 14 additions & 8 deletions spec/helpers/alchemy/elements_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Alchemy
end

it "renders the element's view partial" do
is_expected.to have_selector("##{element.name}_#{element.id}")
is_expected.to have_selector("##{element.dom_id}")
end

context "with element view partial not found" do
Expand Down Expand Up @@ -65,10 +65,16 @@ module Alchemy
end

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

subject { helper.element_dom_id(element) }

it "should render a unique dom id for element" do
is_expected.to eq("#{element.name}_#{element.id}")
is_expected.to eq(element.dom_id)
end
end

Expand All @@ -83,8 +89,8 @@ module Alchemy
let(:options) { {} }

it "should render all elements from current pages public version." do
is_expected.to have_selector("##{element.name}_#{element.id}")
is_expected.to have_selector("##{another_element.name}_#{another_element.id}")
is_expected.to have_selector("##{element.dom_id}")
is_expected.to have_selector("##{another_element.dom_id}")
end

context "in preview_mode" do
Expand All @@ -95,7 +101,7 @@ module Alchemy
end

it "page draft version is used" do
is_expected.to have_selector("##{draft_element.name}_#{draft_element.id}")
is_expected.to have_selector("##{draft_element.dom_id}")
end
end
end
Expand Down Expand Up @@ -125,8 +131,8 @@ module Alchemy
let!(:another_element) { create(:alchemy_element, page: another_page, page_version: another_page.public_version) }

it "should render all elements from that page." do
is_expected.to have_selector("##{element.name}_#{element.id}")
is_expected.to have_selector("##{another_element.name}_#{another_element.id}")
is_expected.to have_selector("##{element.dom_id}")
is_expected.to have_selector("##{another_element.dom_id}")
end

context "in preview_mode" do
Expand All @@ -137,7 +143,7 @@ module Alchemy
end

it "page draft version is used" do
is_expected.to have_selector("##{draft_element.name}_#{draft_element.id}")
is_expected.to have_selector("##{draft_element.dom_id}")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/custom_news_elements_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

class CustomNewsElementsFinder
def elements(*)
[Alchemy::Element.new(name: "news", id: 1001)]
[Alchemy::Element.new(name: "news", id: 1001, position: 1)]
end
end

0 comments on commit 8c1ab79

Please sign in to comment.