【发布时间】:2016-02-17 13:46:28
【问题描述】:
我正在使用 ActiveAdmin,并且正在尝试实现自己的过滤器。
- 按日/周/月过滤
- 按价格(虚拟属性)过滤,大于和小于
businesses.rb
filter :period_eq, as: :select, collection: [['day', 'day'], ['week', 'week']]
filter :price_lteq, as: :string
filter :price_gteq, as: :string
business.rb
ransacker :period,
formatter: -> period {
from = {
day: Time.now.beginning_of_day,
week: 1.week.ago.beginning_of_day,
month: 1.month.ago.beginning_of_day,
year: 1.year.ago.beginning_of_day,
}[period.to_sym]
Business.where('updated_at >= ?', from).pluck(:id)
}, splat_params: true do |parent|
parent.table[:id]
end
ransacker :price do |parent|
# equivalent to Business.where('(owner_price + comission) >= ?', price)
end
我需要 2 个单独的价格字段,而不是 ActiveAdmin 默认的 [lt, gt, eq] 下拉菜单。
我想不通,我应该将哪个 Arel 表达式放在 ransacker 身体中?
我的问题有哪些替代解决方案?我的意思是避免使用 ransack。
更新
我设法让 :price 过滤器工作:
filter :price, as: :numeric_range
ransacker :price,
formatter: -> price {
price.to_s.gsub /[\u00A0\s]+/, ''
}, splat_params: true do |parent|
Arel::Nodes::InfixOperation.new('+',
parent.table[:comission], parent.table[:owner_price])
end
但仍然需要 :period 过滤器方面的帮助
【问题讨论】:
-
您是否尝试过为
price_lteq和price_gteq指定as选项?我认为它会在模型上进行查找,以查看这些列/属性是否存在或忽略它们。 -
@codenamev 谢谢,那行得通。一个后续问题,我可以创建带有 1 个标签和 2 个输入字段(从、到)的过滤器吗?我的代码创建了 2 个带有 2 个标签的过滤器。
标签: ruby-on-rails activeadmin arel ransack