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)

Most upvoted comments

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:

filter :duration, as: :numeric

in the model:

scope :shopper_points_equals, -> (value) { "your sql syntax" }
scope :shopper_greater_than, -> (value) { "your sql syntax" }
scope :shopper_less_than, -> (value) { "your sql syntax" }

private

def self.ransackable_scopes(auth_object=nil)
    [:duration_equals, :duration_greater_than, :duration_less_than]
end

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:

  • The reason for that you have two line of input if you have one ransacker, is that ActiveAdmin don’t support as: :numeric on custom ransackers at the moment, see #3409 and #3510.
  • If you have 3 custom ransackers, the UI works because of that it falls back to the default attribute ransacker, while a ransacker need’s to named without predicate. That mean duration_equals is not a valid ransacker name.