... | @@ -6,6 +6,7 @@ Defining custom filters in activeadmin is a simple task but the details on how t |
... | @@ -6,6 +6,7 @@ Defining custom filters in activeadmin is a simple task but the details on how t |
|
|
|
|
|
First you have to define a new ransacker in the model you want to apply the filter on:
|
|
First you have to define a new ransacker in the model you want to apply the filter on:
|
|
|
|
|
|
|
|
```ruby
|
|
ransacker :country,
|
|
ransacker :country,
|
|
formatter: proc { |country|
|
|
formatter: proc { |country|
|
|
results = Order.find_by_country(country).map(&:id)
|
|
results = Order.find_by_country(country).map(&:id)
|
... | @@ -13,9 +14,11 @@ ransacker :country, |
... | @@ -13,9 +14,11 @@ ransacker :country, |
|
}, splat_params: true do |parent|
|
|
}, splat_params: true do |parent|
|
|
parent.table[:id]
|
|
parent.table[:id]
|
|
end
|
|
end
|
|
|
|
```
|
|
|
|
|
|
Then you have to define the actual code that will do the filter:
|
|
Then you have to define the actual code that will do the filter:
|
|
|
|
|
|
|
|
```ruby
|
|
def self.find_by_country(country_name)
|
|
def self.find_by_country(country_name)
|
|
Order.where(
|
|
Order.where(
|
|
"shipping_address @> hstore(:key, :value)",
|
|
"shipping_address @> hstore(:key, :value)",
|
... | @@ -23,12 +26,15 @@ def self.find_by_country(country_name) |
... | @@ -23,12 +26,15 @@ def self.find_by_country(country_name) |
|
value: country_name
|
|
value: country_name
|
|
)
|
|
)
|
|
end
|
|
end
|
|
|
|
```
|
|
|
|
|
|
[Source](https://git.altum.com.br/gocase/gocase-factory/blob/develop/app/models/order.rb)
|
|
[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):
|
|
Lastly you can register the filter using activeadmin (usually we do this in the index page implementation):
|
|
|
|
|
|
|
|
```ruby
|
|
filter :country_in, as: :select, collection: Country::ALL
|
|
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)
|
|
[Source](https://git.altum.com.br/gocase/gocase-factory/blob/develop/app/ui/go_logistics/order/index.rb)
|
|
|
|
|
... | | ... | |