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

Fix unsafe html view component, allow ViewComponent 3.21+ #6055

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions admin/lib/solidus_admin/testing_support/component_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ module ComponentHelpers
# "Rendered"
# end
# end
def mock_component(&definition)
location = caller(1, 1).first
component_class = Class.new(SolidusAdmin::BaseComponent)
# ViewComponent will complain if we don't fake a class name:
# @see https://github.com/ViewComponent/view_component/blob/5decd07842c48cbad82527daefa3fe9c65a4226a/lib/view_component/base.rb#L371
component_class.define_singleton_method(:name) { "Foo" }
component_class.define_singleton_method(:to_s) { "#{name} (#{location})" }
component_class.class_eval(&definition) if definition
component_class
def mock_component(class_name = "Foo::Component", &definition)
component_class = stub_const(class_name, Class.new(described_class, &definition))
component_class.new
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion admin/solidus_admin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ Gem::Specification.new do |s|
s.add_dependency 'solidus_core', '> 4.2'
s.add_dependency 'stimulus-rails', '~> 1.2'
s.add_dependency 'turbo-rails', '~> 2.0'
s.add_dependency 'view_component', ['~> 3.9', '< 3.21.0']
s.add_dependency 'view_component', '~> 3.9'
end
7 changes: 3 additions & 4 deletions admin/spec/components/solidus_admin/base_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def call
icon_tag("user-line")
end
end.new
end

render_inline(component)

Expand Down Expand Up @@ -42,7 +42,7 @@ def call

describe ".stimulus_id" do
it "returns the stimulus id for the component" do
stub_const("SolidusAdmin::Foo::Bar::Component", Class.new(described_class))
mock_component("SolidusAdmin::Foo::Bar::Component") { erb_template "" }

expect(SolidusAdmin::Foo::Bar::Component.stimulus_id).to eq("foo--bar")
expect(SolidusAdmin::Foo::Bar::Component.new.stimulus_id).to eq("foo--bar")
Expand All @@ -55,8 +55,7 @@ def call

allow(Rails.logger).to receive(:debug) { debug_logs << _1 }

component_class = stub_const("Foo::Component", Class.new(described_class){ erb_template "" })
component = component_class.new
component = mock_component { erb_template "" }
render_inline(component)
translation = component.translate("foo.bar.baz")

Expand Down
Loading