[Search] Add search fields that only show when they have a value

Also add it to a few places I could quickly find that can make use of this.
This commit is contained in:
Earlopain 2023-02-18 17:53:12 +01:00
parent 2604ea0257
commit f629e394ca
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
5 changed files with 12 additions and 3 deletions

View File

@ -1,14 +1,15 @@
class SearchFormBuilder < SimpleForm::FormBuilder
def input(attribute_name, options = {}, &)
value = value_for_attribute(attribute_name, options)
return if value.nil? && options[:hide_unless_value]
options = insert_autocomplete(options)
options = insert_value(attribute_name, options)
options = insert_value(value, options)
super
end
private
def insert_value(attribute_name, options)
value = @options[:search_params][attribute_name] || options[:default]&.to_s
def insert_value(value, options)
return options if value.nil?
if options[:collection]
@ -21,5 +22,9 @@ class SearchFormBuilder < SimpleForm::FormBuilder
options
end
def value_for_attribute(attribute_name, options)
@options[:search_params][attribute_name] || options[:default]&.to_s
end
include FormBuilderCommon
end

View File

@ -1,5 +1,6 @@
<%= form_search(path: artist_versions_path) do |f| %>
<%= f.input :updater_name, label: "User", autocomplete: "user" %>
<%= f.input :updater_id, label: "User ID", hide_unless_value: true %>
<%= f.input :name, autocomplete: "artist" %>
<%= f.submit "Search" %>
<% end %>

View File

@ -1,6 +1,7 @@
<%= form_search(path: comments_path) do |f| %>
<%= hidden_field_tag "group_by", "comment", :id => "group_by_full" %>
<%= f.input :creator_name, label: "Commenter", autocomplete: "user" %>
<%= f.input :creator_id, label: "Commenter ID", hide_unless_value: true %>
<%= f.input :body_matches, label: "Body" %>
<%= f.input :post_tags_match, label: "Tags", autocomplete: "tag-query" %>
<% if CurrentUser.is_admin? %>

View File

@ -2,6 +2,7 @@
<%= f.input :topic_title_matches, label: "Title" %>
<%= f.input :body_matches, label: "Body" %>
<%= f.input :creator_name, label: "Author", autocomplete: "user" %>
<%= f.input :creator_id, label: "Author ID", hide_unless_value: true %>
<%= f.input :topic_category_id, label: "Category", collection: ForumCategory.visible.reverse_mapping, include_blank: true %>
<%= f.submit "Search" %>
<% end %>

View File

@ -1,5 +1,6 @@
<%= form_search(path: user_feedbacks_path) do |f| %>
<%= f.input :user_name, label: "User", autocomplete: "user" %>
<%= f.input :user_id, label: "User ID", hide_unless_value: true %>
<%= f.input :creator_name, label: "Creator", autocomplete: "user" %>
<%= f.input :body_matches, label: "Message" %>
<%= f.input :category, collection: %w[positive negative neutral], include_blank: true %>