ruby on rails - ActiveAdmin: Filter on method rather than attribute? -


i have method on 1 of models following:

def current?   self.parent.start_date <= time.zone.now.to_date && self.parent.end_date >= time.zone.now.to_date && !self.archived end 

i'd create simple filter in activeadmin based on result of method (a simple select filter called current options yes, no, , any) can't seem figure out how.

what's best approach filtering on model method rather 1 of attributes?

the following class method in activeadmin.rb return records according conditions e.g. activeadmin.current("yes") or activeadmin.current("no").

def self.current(inp = "yes") # default inp "yes"   d = time.zone.now.to_date   return case inp     # when inp == "yes" return records archived == false (won't return archived == nil)      # , parents start_date <= d , end_date >= d     when "yes"                 where(archived: false ).                joins(:parent).                where("parents.start_date <= ? , parents.end_date >= ?",d,d)      # when inp == "no" return records archived == true (won't return archived == nil)     # , parents start_date > d or end_date < d     when "no"                  where(archived: true ).                joins(:parent).                where("parents.start_date > ? or parents.end_date < ?",d,d)      # when inp = "any" return records     when "any"                scoped       # return records if inp not match of above options     else                scoped     end  end 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -