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

Stop showing select prompts for false attributes #51605

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion actionview/lib/action_view/helpers/tags/select_renderer.rb
Expand Up @@ -40,7 +40,7 @@ def add_options(option_tags, options, value = nil)
option_tags = tag_builder.content_tag_string("option", content, value: "", label: label) + "\n" + option_tags
end

if value.blank? && options[:prompt]
if value != false && value.blank? && options[:prompt]
tag_options = { value: "" }.tap do |prompt_opts|
prompt_opts[:disabled] = true if options[:disabled] == ""
prompt_opts[:selected] = true if options[:selected] == ""
Expand Down
22 changes: 20 additions & 2 deletions actionview/test/template/form_options_helper_test.rb
Expand Up @@ -790,7 +790,7 @@ def test_select_with_default_prompt
)
end

def test_select_no_prompt_when_select_has_value
def test_select_no_prompt_when_attribute_has_non_blank_string_value
@post = Post.new
@post.category = "<mus>"
assert_dom_equal(
Expand All @@ -799,7 +799,16 @@ def test_select_no_prompt_when_select_has_value
)
end

def test_select_with_given_prompt
def test_select_no_prompt_when_attribute_has_false_value
@post = Post.new
@post.allow_comments = false
assert_dom_equal(
"<select id=\"post_allow_comments\" name=\"post[allow_comments]\"><option value=\"false\" selected=\"selected\">false</option>\n<option value=\"true\">true</option></select>",
select("post", "allow_comments", [false, true], prompt: true)
)
end

def test_select_with_given_prompt_for_blank_string_value
@post = Post.new
@post.category = ""
assert_dom_equal(
Expand All @@ -808,6 +817,15 @@ def test_select_with_given_prompt
)
end

def test_select_with_given_prompt_for_nil_value
@post = Post.new
@post.allow_comments = nil
assert_dom_equal(
"<select id=\"post_allow_comments\" name=\"post[allow_comments]\"><option value=\"\">The prompt</option>\n<option value=\"false\">false</option>\n<option value=\"true\">true</option></select>",
select("post", "allow_comments", [false, true], prompt: "The prompt")
)
end

def test_select_with_given_prompt_escaped
@post = Post.new
assert_dom_equal(
Expand Down