activeadmin: Problem with custom numeric filter
Hi!
activeadmin (1.0.0.pre) ransack (1.5.1)
models/rental.rb:
ransacker :duration_equals, formatter: -> (opt) {
sql = "SELECT id FROM rentals WHERE (DATE_PART('day', rental_end_time::timestamp - rental_start_time::timestamp) * 24 +
DATE_PART('hour', rental_end_time::timestamp - rental_start_time::timestamp)) * 60 +
DATE_PART('minute', rental_end_time::timestamp - rental_start_time::timestamp) = #{opt}"
data = ActiveRecord::Base.connection.execute(sql)
data.present? ? Rental.find(data) : nil
}, splat_params: true do |parent|
parent.table[:id]
end
ransacker :duration_greater_than, formatter: -> (opt) {
sql = "SELECT id FROM rentals WHERE (DATE_PART('day', rental_end_time::timestamp - rental_start_time::timestamp) * 24 +
DATE_PART('hour', rental_end_time::timestamp - rental_start_time::timestamp)) * 60 +
DATE_PART('minute', rental_end_time::timestamp - rental_start_time::timestamp) >= #{opt}"
data = ActiveRecord::Base.connection.execute(sql)
data.present? ? Rental.find(data) : nil
}, splat_params: true do |parent|
parent.table[:id]
end
ransacker :duration_less_than, formatter: -> (opt) {
sql = "SELECT id FROM rentals WHERE (DATE_PART('day', rental_end_time::timestamp - rental_start_time::timestamp) * 24 +
DATE_PART('hour', rental_end_time::timestamp - rental_start_time::timestamp)) * 60 +
DATE_PART('minute', rental_end_time::timestamp - rental_start_time::timestamp) <= #{opt}"
data = ActiveRecord::Base.connection.execute(sql)
data.present? ? Rental.find(data) : nil
}, splat_params: true do |parent|
parent.table[:id]
end
admin/rentals.rb:
filter :duration, as: :numeric
It seems that duration_equals, duration_greater_than or duration_less_than are never called. If I delete one I have an error. If I add a ransacker called “duration”, this one is called but I can’t know if it comes from “equals”, “greater” or “less”…
[55] pry(main)> p
=> {"duration_gteq"=>"15", "duration_lteq"=>"20"}
[56] pry(main)> r = Rental.ransack p
=> Ransack::Search<class: Rental, base: Grouping <conditions: [Condition <predicate: gteq, values: ["15"]>, Condition <predicate: lteq, values: ["20"]>], combinator: and>>
There is no “attributes” in the Ransack::Search object.
Thank you for your help!
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Comments: 15 (7 by maintainers)
I could run using ransackable scopes
For the documentation, see the commit https://github.com/svoop/ransack/commit/72dd5d12d58919bf37199234cf13f9533f3b8cd5
Here’s an example:
in activeadmin:
in the model:
After playing a bit with ransacker, I think this is not a ActiveAdmin problem. I’m closing this issue because of that reason. If you have a ransacker with works on the console but not in ActiveAdmin, I will reopen this issue. I suggest you a maybe solution in the ransacker ticket.
Related to your last commend:
as: :numericon custom ransackers at the moment, see #3409 and #3510.duration_equalsis not a valid ransacker name.