sequielo
sequielo

Reputation: 1631

ActiveAdmin create a custom filter based on getter

I have an ActiveAdmin page with filters. I would like to set a custom filter based on a getter (a method defined on the resource, not an ActiveRecord attribute).

class House
  def is_enchanted?
    # Ask to witches and ghosts...
  end
end

This is the ActiveAdmin resource:

ActiveAdmin.register House do
  filter :is_enchanted?
end

The above code raises the following exception:

undefined method `is_enchanted?_eq' for #<Ransack::Search:0xc188178>

Upvotes: 3

Views: 1952

Answers (2)

dr. Neox
dr. Neox

Reputation: 431

you could use scopes

models/house.rb

class House
  scope :is_enchanted?, 
      #where(....) 
end

admin/house.rb

ActiveAdmin.register House do
  scope :is_enchanted?
end

Upvotes: 1

seanlinsley
seanlinsley

Reputation: 3205

You can create custom Ransack search methods called "ransackers". There isn't any official documentation for it, so you'll need to do some searching on the Ransack issue tracker on GitHub for discussions like this one: https://github.com/activerecord-hackery/ransack/issues/36

Upvotes: 0

Related Questions