際際滷

際際滷Share a Scribd company logo
Search Form for Rails
Roppongi.rb #11 2019/06/20(直)
1
徭失B初
兆念: 舞堀
GitHub: @sinsoku (アイコン嘔貧)
Twitter: @sinsoku_listy (アイコン嘔和)
Railss: それなり
2
Searching
1. Elasticsearch
2. Amazon CloudSearch
3. Ransack
4. self implementation
5. other...?
3
4
controller
def index
@q = Person.ransack(params[:q])
@people = @q.result(distinct: true)
end
5
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
Pros
? easy to introduce
? realizes complex conditions with Advanced Mode
? searches by associations
? used by many people(, and easy to ?nd references)
7
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
Example
Support for Rails 6
https://github.com/activerecord-hackery/ransack/pull/1027
9
I came up with a new implementation
So, I am developing a gem to replace Ransack.
10
sinsoku/pickel
Pickel provides methods that make it
easy to build a search form for your
Rails app.
11
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
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
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
Design 1
? handle search conditions with Hash
conditinos = {
name_eq: 'foo',
posts_title_start: 'bar'
}
15
Design 2
? Convert a condition to Relation
rel_1 = User.where(name: 'foo')
cond = Post.arel_table[:title].start('bar%')
rel_2 = User.joins(:posts).merge(Post.where(cond))
16
Design 3
? Merge all conditions
rel = rel_1.merge(rel_2)
17
Is this usable in production?
18
Sorry, it is still in development.
Once it has been implemented to replace
Ransack, I will be tagged v1.0.0.
19
Click Watch
!
or Star
20

More Related Content

Search Form for Rails

  • 1. Search Form for Rails Roppongi.rb #11 2019/06/20(直) 1
  • 2. 徭失B初 兆念: 舞堀 GitHub: @sinsoku (アイコン嘔貧) Twitter: @sinsoku_listy (アイコン嘔和) Railss: それなり 2
  • 3. Searching 1. Elasticsearch 2. Amazon CloudSearch 3. Ransack 4. self implementation 5. other...? 3
  • 4. 4
  • 5. controller def index @q = Person.ransack(params[:q]) @people = @q.result(distinct: true) end 5
  • 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
  • 9. Example Support for Rails 6 https://github.com/activerecord-hackery/ransack/pull/1027 9
  • 10. I came up with a new implementation So, I am developing a gem to replace Ransack. 10
  • 11. sinsoku/pickel Pickel provides methods that make it easy to build a search form for your Rails app. 11
  • 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
  • 15. Design 1 ? handle search conditions with Hash conditinos = { name_eq: 'foo', posts_title_start: 'bar' } 15
  • 16. Design 2 ? Convert a condition to Relation rel_1 = User.where(name: 'foo') cond = Post.arel_table[:title].start('bar%') rel_2 = User.joins(:posts).merge(Post.where(cond)) 16
  • 17. Design 3 ? Merge all conditions rel = rel_1.merge(rel_2) 17
  • 18. Is this usable in production? 18
  • 19. Sorry, it is still in development. Once it has been implemented to replace Ransack, I will be tagged v1.0.0. 19