-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), *, **) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
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 |
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.