Skip to content

Raise when options_for_select is used as a value #293

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/phlex/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class HelpersCalledBeforeRenderError < StandardError; end
autoload :Streaming, "phlex/rails/streaming"

autoload :Builder, "phlex/rails/builder"

autoload :Never, "phlex/rails/never"
end

CSV.prepend(Phlex::Rails::CSV)
Expand Down
14 changes: 13 additions & 1 deletion lib/phlex/rails/helpers/options_for_select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@ module Phlex::Rails::Helpers::OptionsForSelect
extend Phlex::Rails::HelperMacros

# [Rails Docs](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select)
register_output_helper def options_for_select(...) = nil
def options_for_select(*args, **kwargs, &block)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should probably do this for options_from_collection_for_select too.

output = if block
view_context.options_for_select(*args, **kwargs) { |*args| capture(*args, &block) }
else
view_context.options_for_select(*args, **kwargs)
end

raw(output)

Phlex::Rails::Never.new do
raise Phlex::ArgumentError.new("You can’t use options_for_select as an argument for a select helper in Phlex. Instead, pass a block and call options_for_select inside that block.")
end
end
end
12 changes: 11 additions & 1 deletion lib/phlex/rails/helpers/select_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@ module Phlex::Rails::Helpers::SelectTag
extend Phlex::Rails::HelperMacros

# [Rails Docs](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag)
register_output_helper def select_tag(...) = nil
# register_output_helper def select_tag(...) = nil

def select_tag(name, *, **, &block)
output = if block
view_context.select_tag(name, capture(&block), *, **)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This actually differs form the official API in this case.

else
view_context.select_tag(name, *, **)
end

raw(output)
end
end
11 changes: 11 additions & 0 deletions lib/phlex/rails/never.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Phlex::Rails::Never < BasicObject
def initialize(&block)
@block = block
end

def method_missing(method_name, *, **)
@block.call(method_name, *, **)
end
end
Comment on lines +3 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is so creative!

Copy link
Collaborator Author

@joeldrapper joeldrapper May 8, 2025

Choose a reason for hiding this comment

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

You should see the PR I gave up on. It was popping things off the buffer. 🤣

The technique works but I thought it was too much to maintain.

91 changes: 91 additions & 0 deletions test/helpers/options_for_select.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# frozen_string_literal: true

test "options_for_select as a value for a select_tag" do
component = Class.new(Phlex::HTML) do
include Phlex::Rails::Helpers::SelectTag
include Phlex::Rails::Helpers::OptionsForSelect

define_method :view_template do
select_tag :test, options_for_select([["Option 1", 1], ["Option 2", 2]])
end
end

error = assert_raises Phlex::ArgumentError do
render(component)
end

assert error.message.include?("options_for_select")
end

test "options_for_select as a value for a form_with select" do
component = Class.new(Phlex::HTML) do
include Phlex::Rails::Helpers::FormWith
include Phlex::Rails::Helpers::OptionsForSelect

define_method :view_template do
form_with(url: "/") do |form|
form.select(:test, options_for_select([["Option 1", 1], ["Option 2", 2]]))
end
end
end

controller.define_singleton_method(:form_authenticity_token) { |_| "(example form authenticity token)" }

error = assert_raises Phlex::ArgumentError do
render(component)
end

assert error.message.include?("options_for_select")
end

test "options_for_select in a form with select" do
component = Class.new(Phlex::HTML) do
include Phlex::Rails::Helpers::FormWith
include Phlex::Rails::Helpers::OptionsForSelect

define_method :view_template do
form_with(url: "/") do |form|
form.select(:test) do
options_for_select([["Option 1", 1], ["Option 2", 2]])
end
end
end
end

controller.define_singleton_method(:form_authenticity_token) { |_| "(example form authenticity token)" }

output = render(component)

assert_equivalent_html output, <<~HTML
<form action="/" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓" autocomplete="off">
<input type="hidden" name="authenticity_token" value="(example form authenticity token)" autocomplete="off">
<select name="test">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</form>
HTML
end

test "options_for_select in a select_tag" do
component = Class.new(Phlex::HTML) do
include Phlex::Rails::Helpers::SelectTag
include Phlex::Rails::Helpers::OptionsForSelect

define_method :view_template do
select_tag :test do
options_for_select([["Option 1", 1], ["Option 2", 2]])
end
end
end

output = render(component)

assert_equivalent_html output, <<~HTML
<select name="test" id="test">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
HTML
end
Loading