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

Merged
merged 4 commits into from
May 28, 2025

Conversation

joeldrapper
Copy link
Collaborator

No description provided.

@joeldrapper joeldrapper marked this pull request as ready for review May 8, 2025 11:14

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.

@@ -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.

@joeldrapper joeldrapper added this to the 2.3 milestone May 8, 2025
Comment on lines +3 to +11
class Phlex::Rails::Never < BasicObject
def initialize(&block)
@block = block
end

def method_missing(method_name, *, **)
@block.call(method_name, *, **)
end
end
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.

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.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Rails' select helper is ultimately going to call options_for_select, so may be worth calling that out, i.e. "pass a block and call options_for_select inside that block, or pass the options to the select helper directly."

I just ran into this myself, and it turns out my usage of options_for_select was completely unnecessary. That function returns early if passed a string so Rails itself won't complain about this kind of usage.

@joeldrapper joeldrapper merged commit a93ab1b into main May 28, 2025
9 checks passed
@joeldrapper joeldrapper deleted the require-options-for-select-as-output branch May 28, 2025 10:17
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

Successfully merging this pull request may close these issues.

3 participants