3
votes

I want to have a search widget/filter feature on a custom page using active admin to provide a look-up feature. Using the action_items I have my own action and form that renders the search page. On the search page the 'filters' that I need to show include text fields of the 'Parent' resource and a drop down list of the Parent's Parent. The association is as follows

Class MyChildResource
  belongs_to :myParentResource

Class MyParentResource
  attr_accessible :name, :close_to_place, :date
  has_many :myChildResources
  belongs_to :myGrandParentResource

class MyGrandParentResource
  has_many :myParentResources

In the active admin Resource

ActiveAdmin.register MyChildResource do
  action_item :only=>:index do
    link_to("Look Up Availability", search_admin_myChildResources_path)
  end

  collection_action :search do
    # do something here similar to the 'filter' feature like filter on myparentresource.date as date
    filter on myGrandParentResource as drop down 
    filter on myParentResource.close_to_place as string 
  end
end

Do I have to write my own custom meta_search features? I would be fine even if I have to write own search queries, based on the input that the user gives, but my problem is that how do I display the drop down values from the parent's parent model and/or leverage the power of active admin filters.

I read something similar in this question,How to add a filter to Active Admin dashboard? , but it's a hack and it definitely does not answer the question of displaying a list

1

1 Answers

2
votes

I haven't found any DRY way of doing this, so I've ended up creating my own search form which uses the active admin css for the 'sidebar' and 'panel' as mentioned in the link above and for each element of drop down, text field , date and buttons. So I get a widget which looks like the sidebar. For the search result, I am running my own queries and searching based on the user input. The Search Form looks like this:

Search For <br>
<div id="search_filter_partial" class="panel_contents"> 
<%= form_tag(search_path,:remote=>true,:class=>"filter_form", :name=>"search_filters") do %>

    <div class="filter_form_field filter_string">
      <%= label_tag(:author, "Author",:class=>" label") %>
      <%= text_field_tag (:author) %>
    </div>

    <div class="filter_form_field filter_select">
    <%= label_tag(:book, "Book",:class=>" label") %>
     <%= select("book","book_id", @books.map {|u| [u.name,u.id]}) %>
    </div>

    <div class="filter_form_field filter_string">
      <%= label_tag(:published_date, "Published Date",:class=>"label")%>
      <%=  date_select :book,:published_date %>
    </div>

    <div class="buttons">
     <%= submit_tag('Find It', :onclick => "validateForm();") %>
     <a class="clear_filters_btn" href="#">Clear</a>
     <input id="order" type="hidden" value="id_desc" name="order">
     <input id="scope" type="hidden" name="scope">
   </div>
 <%end%></div>

Doesn't look too good, but that's the best I have been able to come up with since I want to stay consistent with the look and feel of Active Admin