The document discusses search forms for Rails applications. It introduces Ransack as a popular gem for building search forms but notes some cons like including hacks and difficulty maintaining. It then proposes a new gem called Pickel that the author is developing to replace Ransack. Pickel is designed to build search queries using ActiveRecord directly without hacks and allow searching on attributes and associations through a simple API. Various design approaches for Pickel are discussed, with the goal of tagging it at v1.0 once it is production-ready to replace Ransack.
6. view
<%= search_form_for @q do |f| %>
# Search if the name field contains...
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
# Search if an associated articles.title starts with...
<%= f.label :articles_title_start %>
<%= f.search_field :articles_title_start %>
# Attributes may be chained. Search multiple attributes for one value...
<%= f.label :name_or_description_or_email_or_articles_title_cont %>
<%= f.search_field :name_or_description_or_email_or_articles_title_cont %>
<%= f.submit %>
<% end %>
6
7. Pros
? easy to introduce
? realizes complex conditions with Advanced Mode
? searches by associations
? used by many people(, and easy to ?nd references)
7
8. Cons
? includes many hack codes for ActiveRecord
? di?cult to maintenance
? CI failed on 5-2-stable
!
? I do not like the design about Authorization
8
12. in controller
def index
search_params = Pickel.permit(params, :name, :age_gt, :posts_title_start)
@search = Pickel.search(User, search_params)
@users = @search.result
end
12
13. in view
<%= form_for @search do |f| %>
<%# Search records contains the value %>
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
<%# Search records grater than the value %>
<%= f.label :age_gt %>
<%= f.number_field :age_gt %>
<%# Search records an associated posts.title starts with the value %>
<%= f.label :posts_title_start %>
<%= f.search_field :posts_title_start %>
<%= f.submit %>
<% end %>
13
14. Concept (Di?erence from Ransack)
? refrains hack for ActiveRecord
? basically builds a SQL in ActiveRecord.merge
? (However, Arel is used)
? allows search conditions by the StrongParameter
? uses form_for as it is
14