1

次のようなモデルの1つにメソッドがあります。

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

Currentこのメソッドの結果 ( options YesNo、および で呼び出される単純な選択フィルター) に基づいて ActiveAdmin で単純なフィルターを作成したいのですが、方法がAnyわかりません。

その属性の 1 つではなく、モデル メソッドでフィルタリングするための最良のアプローチは何ですか?

4

1 に答える 1

0

の次のクラス メソッドは、または などActiveAdmin.rbの条件に従ってすべてのレコードを返します。ActiveAdmin.current("Yes")ActiveAdmin.current("No")

def self.current(inp = "Yes") # default inp is "Yes"
  d = Time.zone.now.to_date
  return case inp
    # when inp == "Yes" it would return all the records with archived == false (won't return archived == nil) 
    # AND parents with start_date <= d and end_date >= d
    when "Yes" 
               where(archived: false ).
               joins(:parent).
               where("parents.start_date <= ? AND parents.end_date >= ?",d,d)

    # when inp == "No" it would return all the records with archived == true (won't return archived == nil)
    # AND parents with 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 all records
    when "Any"
               scoped 

    # return all records if inp does not match any of the above options
    else
               scoped
    end

end
于 2013-09-25T19:14:51.917 に答える