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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With