[英]Custom ActiveAdmin Filter for Date Range - Complex Logic in Method
I am trying to create a custom ActiveAdmin filter that takes date_range as a parameter.我正在尝试创建一个将 date_range 作为参数的自定义 ActiveAdmin 过滤器。 Every solution I've found has been for excessively simple model methods.我找到的每个解决方案都是针对过于简单的模型方法。
Is there a way to pass both parameters into the model ransacker method, and/or at the very least to control the order in which these parameters are passed as well as to know which one is being passed?有没有办法将这两个参数传递给模型 ransacker 方法,和/或至少控制传递这些参数的顺序以及知道传递的是哪个参数? (end_date vs. start_date -- start_date is passed first, whereas I might be able to work around this is end_date were sent first). (end_date 与 start_date - start_date 首先传递,而我可能能够解决这个是 end_date 首先发送的问题)。 Any alternative solution, which would not break all other filters in the application (ie, overwriting activeadmin filters to use scopes - this is one filter out of hundreds in the application) welcome as well.任何不会破坏应用程序中所有其他过滤器的替代解决方案(即覆盖 activeadmin 过滤器以使用范围 - 这是应用程序中数百个过滤器中的一个)也欢迎。
Thank you!谢谢!
admin/model.rb管理员/模型.rb
filter :model_method_in, as: :date_range
models/model.rb模型/模型.rb
ransacker :model_method, :formatter => proc { |start_date, end_date|
Model.complicated_method(start_date, end_date)
} do |parent|
parent.table[:id]
end
...
def method_for_base_queries(end_date)
Model.long_complicated_sql_call_using_end_date
end
def complicated_method(start_date, end_date)
model_instances = method_for_base_queries(end_date)
model_instances.logic_too_complex_for_sql_using_start_date
end
Similar question, but filter/model logic was simple enough for an alternative solution that didn't require both parameters to be passed in: Custom ActiveAdmin filter for Date Range类似的问题,但过滤器/模型逻辑对于不需要传入两个参数的替代解决方案来说足够简单: Custom ActiveAdmin filter for Date Range
This might help.这可能会有所帮助。 Given your index filter鉴于您的索引过滤器
filter :model_method, as: :date_range
you can write the following in your model:您可以在模型中编写以下内容:
scope :model_method_gteq_datetime, -> (start_date) {
self.where('users.your_date_column >= ?', start_date)
}
scope :model_method_lteq_datetime, -> (end_date) {
# added one day since apparently the '=' is not being counted in the query,
# otherwise it will return 0 results for a query on the same day (as "greater than")
self.where('users.your_date_column <= ?', (Time.parse(end_date) + 1.day).to_date.to_s)
}
def self.ransackable_scopes(auth_object = nil)
[model_method_gteq_datetime, :model_method_lteq_datetime]
end
..._gteq_datetime
and ..._lteq_datetime
is how Activeadmin interprets the two dates in a custom date_range
index filter (see also the corresponding url generated after adding the filter). ..._gteq_datetime
和..._lteq_datetime
是 Activeadmin 在自定义date_range
索引过滤器中解释两个日期的方式(另请参阅添加过滤器后生成的相应 url)。
I've written a sample query that fits my case (given that users
is a model related to the current one), since I don't know the complexity of yours.我编写了一个适合我的情况的示例查询(假设users
是与当前模型相关的模型),因为我不知道你的复杂性。
I'm using:我正在使用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.