Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute values > to_phlex_attribute_value, to_str, to_s #629

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions gd/phlex/sgml/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

include TestHelper

class ToPhlexAttributeValueable
def to_phlex_attribute_value
"to_phlex_attribute_value"
end
end

class ToSable
def to_s
"to_s"
end
end
Comment on lines +5 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that we can test for method precedence as well, what do you think about making these test classes build on each other like this:

Suggested change
class ToPhlexAttributeValueable
def to_phlex_attribute_value
"to_phlex_attribute_value"
end
end
class ToSable
def to_s
"to_s"
end
end
class ToPhlexAttributeValueable < ToSable
def to_phlex_attribute_value
"to_phlex_attribute_value"
end
end
class ToSable < ToStrable
def to_s
"to_s"
end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but not sure it's necessary. The code is hugely simple, so I think testing much more than the existence of the methods has very little benefit.

Happy to add if that's the consensus.


class ToStrable
def to_str
"foo"
"to_str"
end
end

Expand Down Expand Up @@ -40,12 +52,28 @@ def to_str
expect(component.new).to_render %(<div class="bg-red-500 rounded"></div>)
end

test "with a to_phlex_attribute_value-able object" do
component = build_component_with_template do
div class: ToPhlexAttributeValueable.new
end

expect(component.new).to_render %(<div class="to_phlex_attribute_value"></div>)
end

test "with a to_s-able object" do
component = build_component_with_template do
div class: ToSable.new
end

expect(component.new).to_render %(<div class="to_s"></div>)
end

test "with a to_str-able object" do
component = build_component_with_template do
div class: ToStrable.new
end

expect(component.new).to_render %(<div class="foo"></div>)
expect(component.new).to_render %(<div class="to_str"></div>)
end

test "with numeric integer/float" do
Expand Down
10 changes: 9 additions & 1 deletion lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,15 @@ def __build_attributes__(attributes, buffer:)
when Set
buffer << " " << name << '="' << ERB::Escape.html_escape(v.to_a.compact.join(" ")) << '"'
else
buffer << " " << name << '="' << ERB::Escape.html_escape(v.to_str) << '"'
value = if v.respond_to?(:to_phlex_attribute_value)
v.to_phlex_attribute_value
elsif v.respond_to?(:to_str)
v.to_str
else
v.to_s
end

buffer << " " << name << '="' << ERB::Escape.html_escape(value) << '"'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be updated because of a change I made.

Suggested change
buffer << " " << name << '="' << ERB::Escape.html_escape(value) << '"'
buffer << " " << name << '="' << Phlex::Escape.html_escape(value) << '"'

end
end

Expand Down