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

with_content for ViewComponent flagged as dynamic render path #1832

Open
gavingmiller opened this issue Mar 2, 2024 · 4 comments
Open

with_content for ViewComponent flagged as dynamic render path #1832

gavingmiller opened this issue Mar 2, 2024 · 4 comments

Comments

@gavingmiller
Copy link
Contributor

Background

Brakeman version: 5.4.1
Rails version: 6.1.7.6
Ruby version: 2.7.7p221

Link to Rails application code: N/A

Issue

When rendering a ViewComponent via the with_content method a Dynamic Render Path warning will be raised. However, using the block version, the warning is not raised. Example using the basic implementation for a view component:

<!-- No error raised -->
<%= render ExampleComponent.new(title: "my title") %>

<!-- Error raised -->
<%= render ExampleComponent.new(title: "my title").with_content("string") %>

<!-- No error raised -->
<%= render ExampleComponent.new(title: "my title") do %>
  string
<% end %>

<!-- No error raised -->
<%= render ExampleComponent.new(title: "my title") do %>
  <%= "string" %>
<% end %>

My debugging has lead me to the renderable? method, and it appears like it's missing logic to detect the with_content usage of this pattern.

@gavingmiller
Copy link
Contributor Author

gavingmiller commented Mar 9, 2024

I've managed to put together what's potentially a patch for this. Code below:

def renderable? exp
  return false unless call?(exp)

  if exp.method == :with_content
    exp = exp.target
  end

  return false unless constant?(exp.target)   
  target_class_name = class_name(exp.target)
  known_renderable_class?(target_class_name) or tracker.find_method(:render_in, target_class_name)
end

This looks to handle the cases of with_content in our codebase, and I'm curious if this would be more broadly applicable?

@presidentbeef
Copy link
Owner

That works and would probably be how I'd do it, too.

@presidentbeef
Copy link
Owner

And maybe one day make this check optional.

@gavingmiller
Copy link
Contributor Author

@presidentbeef awesome, I'll PR it in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants