Last active
December 11, 2015 07:08
-
-
Save gudata/4564264 to your computer and use it in GitHub Desktop.
real life searching with ransack
including search in field concatation and sorting on virtual columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Models | |
| class User | |
| ransacker :full_name, :formatter => proc {|v| UnicodeUtils.downcase(v) } do |parent| | |
| Arel::Nodes::NamedFunction.new('LOWER', | |
| [Arel::Nodes::NamedFunction.new('concat_ws', [' ', parent.table[:first_name], parent.table[:middle_name], parent.table[:last_name]])] | |
| ) | |
| end | |
| end | |
| Default search order in the controller | |
| def collection | |
| if params[:q].nil? | |
| params[:q] = {} | |
| params[:q][:s] = ['created_at desc'] | |
| end | |
| relation = filtered_collection.includes(:user) | |
| @search = relation.search(params[:q]) | |
| @results = @search.result(:distinct => true) | |
| @results = @results.page(params[:page]).per(params[:per_page] || 12) | |
| end | |
| another controller with custom search | |
| helper_method :compound_field_name | |
| def compound_field_name | |
| %w(name description ingredients narrative user_full_name).join('_or_') + '_cont' | |
| end | |
| def collection | |
| if params[:q].nil? | |
| params[:q] = {} | |
| params[:q][:s] = ['created_at desc'] | |
| end | |
| relation = filtered_collection.includes(:user) | |
| @search = relation.search(params[:q]) | |
| @results = @search.result(:distinct => true) | |
| if params[:q] and params[:q][:s] and params[:q][:s].include?("readytime") | |
| @results = @results.select('*, time_to_cook + time_to_prepare as readytime') | |
| @results = @results.order('readytime DESC') | |
| raise @results.to_sql | |
| end | |
| @results = @results.page(params[:page]).per(params[:per_page] || 12) | |
| end | |
| The form | |
| #filters.well | |
| = search_form_for @search, url: category_photos_path(current_category), html: { class: ['form-search'] } do |f| | |
| ul.breadcrumb.pull-left | |
| li | |
| = f.sort_link :readytime, default_order: 'desc' | |
| span.divider / | |
| li | |
| = f.sort_link :user_id | |
| span.divider / | |
| li | |
| = f.sort_link :price, t("Цена"), default_order: 'asc' | |
| span.divider / | |
| li | |
| = f.sort_link :votes, default_order: 'desc' | |
| span.divider / | |
| li | |
| = f.sort_link :created_at, default_order: 'desc' | |
| .input-append.pull-right | |
| = f.text_field compound_field_name.to_sym, | |
| placeholder: 'Търсене', | |
| class: 'span2 search-query', | |
| autocompleteff: 'off', | |
| data: { url: polymorphic_path([:texts, current_category, resource_type]), provide: "typeahead" } | |
| button(type="submit" class="btn btn-red") | |
| i.icon-search.icon-white | |
| .clearfix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment