|
|
# ActiveAdmin
|
|
|
|
|
|
## Custom Filters
|
|
|
|
|
|
Defining custom filters in activeadmin is a simple task but the details on how to implement them are not easy to find on web. Here is an example of how to define them.
|
|
|
|
|
|
First you have to define a new ransacker in the model you want to apply the filter on:
|
|
|
|
|
|
ransacker :country,
|
|
|
formatter: proc { |country|
|
|
|
results = Order.find_by_country(country).map(&:id)
|
|
|
results = results.present? ? results : nil
|
|
|
}, splat_params: true do |parent|
|
|
|
parent.table[:id]
|
|
|
end
|
|
|
|
|
|
Then you have to define the actual code that will do the filter:
|
|
|
|
|
|
def self.find_by_country(country_name)
|
|
|
Order.where(
|
|
|
"shipping_address @> hstore(:key, :value)",
|
|
|
key: "country",
|
|
|
value: country_name
|
|
|
)
|
|
|
end
|
|
|
|
|
|
[Source](https://git.altum.com.br/gocase/gocase-factory/blob/develop/app/models/order.rb)
|
|
|
|
|
|
Lastly you can register the filter using activeadmin (usually we do this in the index page implementation):
|
|
|
|
|
|
filter :country_in, as: :select, collection: Country::ALL
|
|
|
|
|
|
[Source](https://git.altum.com.br/gocase/gocase-factory/blob/develop/app/ui/go_logistics/order/index.rb)
|
|
|
|
|
|
## References
|
|
|
|
|
|
[ActiveAdmin Filters with Ransack](https://cavewall.jaguardesignstudio.com/2014/05/01/activeadmin-filters-with-ransack/)
|
|
|
|
|
|
|
|
|
|