[Tags] Convert "Hide empty?" to checkbox

Also added option for a default value and made checkbox inputs
react to "truthy" for  backwards compatability
This commit is contained in:
Earlopain 2022-07-10 16:09:24 +02:00
parent 7cd266d7bb
commit ff5dc78cf4
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
2 changed files with 5 additions and 5 deletions

View File

@ -3,20 +3,20 @@ class SearchFormBuilder < SimpleForm::FormBuilder
options[:input_html] ||= {}
options[:input_html][:data] = {}
options[:input_html][:data][:autocomplete] = options[:autocomplete] if options[:autocomplete]
options = insert_value_from_search_params(attribute_name, options)
options = insert_value(attribute_name, options)
super
end
private
def insert_value_from_search_params(attribute_name, options)
value = @options[:search_params][attribute_name]
def insert_value(attribute_name, options)
value = @options[:search_params][attribute_name] || options[:default]&.to_s
return options if value.nil?
if options[:collection]
options[:selected] = value
elsif options[:as]&.to_sym == :boolean
options[:input_html][:checked] = true if value == "1"
options[:input_html][:checked] = true if value.truthy?
else
options[:input_html][:value] = value
end

View File

@ -2,8 +2,8 @@
<%= f.input :name_matches, label: "Name", hint: "Use * for wildcard", autocomplete: "tag" %>
<%= f.input :category, label: "Category", collection: TagCategory.canonical_mapping.to_a, include_blank: true %>
<%= f.input :order, collection: [%w[Newest date], %w[Count count], %w[Name name]], include_blank: false %>
<%= f.input :hide_empty, label: "Hide empty?", collection: %w[yes no] %>
<%= f.input :has_wiki, label: "Has wiki?", collection: %w[yes no], include_blank: true %>
<%= f.input :has_artist, label: "Has artist?", collection: %w[yes no], include_blank: true %>
<%= f.input :hide_empty, label: "Hide empty?", as: :boolean, default: true %>
<%= f.submit "Search" %>
<% end %>