Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter by method on model in ActiveAdmin with parameters passed in

Using Rails 4.2.1 and Active Admin 1.0.0.pre2

I have an Apartment model which has many Occupancies. I want admins to be able to see whether an apartment in index overlaps with dates passed in as params. I have a method on Apartment

 def available_during(start_date, end_date)
   return !self.occupancies.any? { |occ| occ.date_range_overlap(Date.parse(start_date), Date.parse(end_date)) }
 end

Which returns true if the apartment has any occupancies that overlap with two given dates. The method date_range_overlap on occupancy is pretty self explanatory. I can't seem to figure out how to make ActiveAdmin's DSL to filter by that method or even make a form to input random params.

I was able to put a column that shows the boolean return value of the available_during method in the index.

if params[:from] && params[:until]
  column "available?" do |apt|
    apt.available_during(params[:from], params[:until])
  end
end

But I can only seem to get this to work by manually inputting the from and until params in the url.

How might I place an arbitrary search form to send the user to the right params? Or better yet, make a filter in that sidebar that uses that method?

like image 621
Vincent Avatar asked Oct 05 '16 15:10

Vincent


Video Answer


1 Answers

It looks like ActiveAdmin has a built in method to create a date picker within a view. Those docs can be found here. The example shows it being used with some random parameters, you will want to modify those for your use case.

form do |f|
  f.input :from, as: :datepicker,
    datepicker_options: {
    min_date: "2013-10-8",
    max_date: "+3D"
  }

  f.input :until, as: :datepicker,
    datepicker_options: {
    min_date: 3.days.ago.to_date,
    max_date: "+1W +5D"
   }
end
like image 89
Cody Gustafson Avatar answered Oct 01 '22 23:10

Cody Gustafson