The default string filter in ActiveAdmin has a select the options of:
This shows a dropdown next to the string search where you are able to choose these options.
My requirement is to have the filter to use contains search condition BUT not show the drop down/select to do this. So it will just have an input box for search, with no select to choose contains.
I originally achieved this by creating a partial, but that was problematic as it was then unable to work with the other filters that ActiveAdmin provides. Here is an image of the partial:
My current thinking is to create a custom filter that does this.
So below is the standard code for the string filter in ActiveAdmin. How could this be modified to default to contains and not show the drop down? I've tried removing the filter options but that doesn't work. Any ideas?
ActiveAdmin uses Ransack and Formtastic
module ActiveAdmin
module Inputs
class FilterStringInput < ::Formtastic::Inputs::StringInput
include FilterBase
include FilterBase::SearchMethodSelect
filter :contains, :equals, :starts_with, :ends_with
# If the filter method includes a search condition, build a normal string search field.
# Else, build a search field with a companion dropdown to choose a search condition from.
def to_html
if seems_searchable?
input_wrapping do
label_html <<
builder.text_field(method, input_html_options)
end
else
super # SearchMethodSelect#to_html
end
end
end
end
end
To remove the need for a predicate drop down, you can use the Ransack's string-based query interface. For example, if you have an ActiveRecord
User
class with an attribute name
and you want a containing filter on it, you could do something like:
ActiveAdmin.register User do
filter :name_cont, label: 'User name'
end
This will generate:
And it will search for Users
containing the introduced input in its name
From your question I can conclude, that you are looking for to have a filter, that searches by :contains
, and not having a drop down for other options ( :equals, :starts_with, :ends_with
).
If I do understand you right, you can simple use this (and not having to monkeypatch the ActiveAdmin):
filter :attribute_name, as: :string, label: 'Your custom label, if default doesn't fit'
As a bonus I can suggest you nice gem ( 'chosen-rails' I've discovered it today), which allows you to do autocomplete on filter (originally it is used to autocomplete the associated model in new/edit form, but I've easily adjusted it for my needs).
So for filters it is as easy as:
filter :title, as: :select, collection: -> {ClientApplication.all.map{|s| s.title}.uniq}, input_html: { class: 'chosen-input' } #or as you've shown before, using pluck :)
The only disadvantage, is that it works only if your string starts with the same letters, as the searched thing, eg if name is "Hello"
, it will hint you when you type "H"
, "He"
and so on, but won't if you type "el"
, "llo"
etc.
Ok, The only thing you need to adjust the filtering of ActiveAdmin to your need is to change (comment the line responsible for adding dropdown) the to_html
method in module [SearchMethodSelect][2]
:
module Inputs
module FilterBase
module SearchMethodSelect
#other methods
def to_html
input_wrapping do
label_html << # your label
#select_html << # THIS LINE -- the dropdown that holds the available search methods
input_html # your input field
end
end
#other methods
end
end
end
I've test it out, and it still works as :contains
, check it out :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.