Module: SpotlightSearch::ClassMethods
- Defined in:
- lib/spotlight_search.rb
Instance Method Summary collapse
- #filter(filter_params) ⇒ Object
- #filter_by(page, filter_params = {}, sort_params = {}) ⇒ Object
- #paginate(page, total_count) ⇒ Object
- #sort_list(sort_column, sort_direction) ⇒ Object
Instance Method Details
#filter(filter_params) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/spotlight_search.rb', line 37 def filter(filter_params) data = self if filter_params.present? && filter_params.class == ActiveSupport::HashWithIndifferentAccess filter_params.each do |key, value| data = data.send(key, value) end else data = self.all end return data end |
#filter_by(page, filter_params = {}, sort_params = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/spotlight_search.rb', line 23 def filter_by(page, filter_params = {}, sort_params = {}) filtered_result = OpenStruct.new sort_column = self.column_names.include?(sort_params[:sort_column]) ? sort_params[:sort_column] : "#{self.table_name}.created_at" sort_direction = %w[asc desc].include?(sort_params[:sort_direction]) ? sort_params[:sort_direction] : "asc" sort_params = {sort_column: sort_column, sort_direction: sort_direction} raw_data = self.filter(filter_params).sort_list(sort_column, sort_direction) filtered_result.data = raw_data.page(page).per(30) filtered_result.facets = self.paginate(page, raw_data.size) filtered_result.sort = sort_params filtered_result.facets.sort = sort_params return filtered_result end |
#paginate(page, total_count) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/spotlight_search.rb', line 53 def paginate(page, total_count) page = page.presence || 1 per_page = 30 facets = OpenStruct.new # initializing OpenStruct instance facets.total_count = total_count facets.filtered_count = total_count facets.total_pages = (total_count/per_page.to_f).ceil facets.current_page = page.to_i # Previous Page if facets.current_page - 1 == 0 facets.previous_page = false else facets.previous_page = true end # Next Page if facets.current_page + 1 > facets.total_pages facets.next_page = false else facets.next_page = true end return facets end |
#sort_list(sort_column, sort_direction) ⇒ Object
49 50 51 |
# File 'lib/spotlight_search.rb', line 49 def sort_list(sort_column, sort_direction) return self.order(sort_column + " " + sort_direction) end |