[](https://badge.fury.io/rb/spotlight_search) # SpotlightSearch It helps filtering, sorting and exporting tables easier. First create a new rails project with the following command. If you are adding to existing project skip this ``` rails new blog -m https://raw.githubusercontent.com/commutatus/cm-rails-template/devise_integration/template.rb ``` ## Installation Add this line to your application's Gemfile: ```ruby gem 'spotlight_search' ``` And then execute: $ bundle Or install it manually: $ gem install spotlight_search Generator that installs mandatory files and gems to application $ rails g spotlight_search:install The install generator does the following * `require spotlight_search` added to application.js * Copies required files for the spotlight_search to work, Such as gemassets.rb, webpacker.yml, environment.js * Copies initializer file * Adds a line in route for mounting. Generator that installs filter and table files to application $ rails g spotlight_search filter orders --filters scope_name:filter_type $ rails g spotlight_search filter orders --filters search:input order_status:multi_select status:select scope_name is the model scope name, scope can written after running this generator, it won't throw any error that it has to be present. Following filter type are supported * input * single-select * multi-select * datetime * daterange ## Usage 1. [Filtering, Sorting and Pagination](#filtering-sorting-and-pagination) * [Controller](#controller) * [View](#view) 2. [Export table data to excel](#export-table-data-to-excel) * [Initializer](#initializer) * [Routes](#Routes) * [Model](#model) * [View](#export-view) ### Filtering, Sorting and Pagination #### Controller **STEP - 1** First step is to add the search method to the index action. ``` @filtered_result = @workshop.filter_by(params[:page], filter_params.to_h, sort_params.to_h) ``` `filter_by` is the search method which accepts 3 arguments. `page`, `filter_params`, `sort_params`. All 3 params are sent from the JS which is handled by the gem. **STEP - 2** Second Step is to add the permitted params. Since the JS is taking up values from HTML, we do not want all params to be accepted. Permit only the scopes that you want to allow. ``` def filter_params params.require(:filters).permit(:search) if params[:filters] end def sort_params params.require(:sort).permit(:sort_column, :sort_direction) if params[:sort] end ``` #### View Please note that the below code is in haml. **STEP - 1 Filters** **Filter Wrapper, Select-tags and Inputs** | Generator | *Mandatory(Data Attributes, Select options) | *Optional(Classes, Placeholders) | |----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------| | `= filter_wrapper(data_behaviours, classes=nil)` | `{filter_url: '/users', replacement_class: 'users-table'}` | "filter-classes" | | | | | | `= cm_select_tag(select_options, data_behaviours, classes=nil, placeholder=nil)` | `{behaviour: "filter", scope: "status", type: "select-filter}` , User.all.map {\|user\| [user.name.titleize, user.id]} | `"user-select"`, placeholder = `"Users"` | | | | | | `= cm_textfield_tag(data_behaviours, classes=nil, placeholder=nil)` | `{behaviour: "filter", scope: "search", type: "input-filter}` | `"user-search"`, placeholder = `"Search"` | | | | | | `= clear_filters(clear_path, classes=nil, data_behaviours=nil, clear_text=nil)` | clear_path = `users_path` | `"clear-filter"`, data_behaviours = `{behaviour: 'clear'}`, clear_text = `"Clear all"` | **STEP - 2 Pagination** We will add the paginate helper to the bottom of the partial which gets replaced. ``` = cm_paginate(@filtered_result.facets) ``` **STEP - 3 Sort** If any of the header needs to be sorted, then we will add the following helper ``` th = sortable "name", "Name", @filtered_result.sort[:sort_column], @filtered_result.sort[:sort_direction] ``` ### Export table data to excel **Note** You will need to have a background job processor such as `sidekiq`, `resque`, `delayed_job` etc as the file will be generated in the background and will be sent to the email passed. If you need to use any other service for sending emails, you will need to override `ExportMailer` class. #### View Add `exportable email, model_object` in your view to display the export button. ```html+erb
Name | <% @records.each do |record| %> |
---|---|
<%= record.name %> | <%= record.value %> | <% end %>