New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with custom numeric filter #479

Closed
Epiju opened this Issue Dec 9, 2014 · 3 comments

Comments

Projects
None yet
3 participants
@Epiju

Epiju commented Dec 9, 2014

Hi!

I'm using ransack with activeadmin and I have a problem with custom filters.

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.values.present? ? data.values.flatten : 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.values.present? ? data.values.flatten : 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.values.present? ? data.values.flatten : 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.

I already posted an issue on the AA page: activeadmin/activeadmin#3683 and there are the custom ransack predicates used by activeadmin: https://github.com/activeadmin/activeadmin/blob/master/lib/ransack_ext.rb

Thank you for your help!

@timoschilling

This comment has been minimized.

Show comment
Hide comment
@timoschilling

timoschilling Dec 10, 2014

Contributor

Try this:

  ransacker :duration, do |parent|
    Arel.sql("(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)")
  end

You maybe need to set a type: :integer attribute.

Contributor

timoschilling commented Dec 10, 2014

Try this:

  ransacker :duration, do |parent|
    Arel.sql("(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)")
  end

You maybe need to set a type: :integer attribute.

@Epiju

This comment has been minimized.

Show comment
Hide comment
@Epiju

Epiju Dec 10, 2014

It works only with "equals" option in ActiveAdmin

   (5.1ms)  SELECT COUNT(*) FROM "rentals"  WHERE "rentals"."deleted_at" IS NULL AND (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) = '20'

How to make it works with the other option (greater than and less than) ?

Thanks!

Epiju commented Dec 10, 2014

It works only with "equals" option in ActiveAdmin

   (5.1ms)  SELECT COUNT(*) FROM "rentals"  WHERE "rentals"."deleted_at" IS NULL AND (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) = '20'

How to make it works with the other option (greater than and less than) ?

Thanks!

@timoschilling

This comment has been minimized.

Show comment
Hide comment
@timoschilling

timoschilling Dec 10, 2014

Contributor

I thinks this can be closed. I exelaint it you in activeadmin/activeadmin#3683, does it not work at the moment.

Contributor

timoschilling commented Dec 10, 2014

I thinks this can be closed. I exelaint it you in activeadmin/activeadmin#3683, does it not work at the moment.

@jonatack jonatack closed this Jul 26, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment