-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Conversation
|
||
def select_tag(name, *, **, &block) | ||
output = if block | ||
view_context.select_tag(name, capture(&block), *, **) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
class Phlex::Rails::Never < BasicObject | ||
def initialize(&block) | ||
@block = block | ||
end | ||
|
||
def method_missing(method_name, *, **) | ||
@block.call(method_name, *, **) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so creative!
There was a problem hiding this comment.
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.") |
There was a problem hiding this comment.
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.
No description provided.