-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2373 from tvdeyen/add-dom-id-class
Add a element DOM id class
- Loading branch information
Showing
5 changed files
with
98 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
# Returns a dom id used for elements html id tag. | ||
# | ||
# Uses the elements name and its position on the page. | ||
# If the element is nested in a parent element it prefixes | ||
# the id with the parent elements dom_id. | ||
# | ||
# Register your own dom id class with | ||
# | ||
# Alchemy::Element.dom_id_class = MyDomIdClass | ||
# | ||
class Element < BaseRecord | ||
class DomId | ||
def initialize(element) | ||
@element = element | ||
@parent_element = element.parent_element | ||
end | ||
|
||
def call | ||
[parent_element&.dom_id, element.name, element.position].compact.join("-") | ||
end | ||
|
||
private | ||
|
||
attr_reader :element, :parent_element | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::Element::DomId do | ||
describe "#call" do | ||
subject(:dom_id) do | ||
described_class.new(element).call | ||
end | ||
|
||
let(:element) { build_stubbed(:alchemy_element, position: 1) } | ||
|
||
it "returns a string from element name and position" do | ||
expect(dom_id).to eq("#{element.name}-#{element.position}") | ||
end | ||
|
||
context "with a parent element" do | ||
let(:parent_element) do | ||
build_stubbed(:alchemy_element, position: 1) | ||
end | ||
|
||
let(:element) do | ||
build_stubbed(:alchemy_element, position: 1, parent_element: parent_element) | ||
end | ||
|
||
it "returns a string from element name and position" do | ||
expect(dom_id).to eq("#{parent_element.name}-#{parent_element.position}-#{element.name}-#{element.position}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters