0.0
No release in over a year
All you need to create a faceted search, as simple as possible
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

FacetedSearch

All you need to create a faceted search, as simple as possible

Maintainability

Installation

Add this line to your application's Gemfile:

gem 'faceted_search'

And then execute:

$ bundle

Or install it yourself as:

$ gem install faceted_search

Getting started

Add Bootstrap and Font Awesome to your app/assets/stylesheets/application.sass

@import 'bootstrap'
@import 'font-awesome-sprockets'
@import 'font-awesome'

Create a model defining your facets:

class Item::Facets < FacetedSearch::Facets
  def initialize(params)
    super
    @model = Item.all
    filter_with_text :title
    filter_with_list :products, {
      find_by: :title,
      habtm: true
    }
    filter_with_list :kinds, {
      habtm: true
    }
    filter_with_list :style, {
      habtm: false
    }
    filter_with_tree :categories, {
      habtm: true,
      children_scope: Proc.new { |children|
        children.order(:title)
      }
    }
    filter_with_boolean :active
    # Other tree option, shows all values
    # filter_with_full_tree :categories, {
    #   habtm: true,
    #   children_scope: Proc.new { |children|
    #     children.order(:title)
    #   }
    # }

    # Other option, with searchable (SEO) categories
    filter_with_full_tree :categories, {
      habtm: true,
      searchable: true,
      path_pattern: Proc.new { |category_id|
        Rails.application.routes.url_helpers.category_path(category_id)
      }
    }

    # Filter with range input
    filter_with_range :distance, {
      min: 10,
      max: 200,
      step: 10,
      default_value: 50,
      hide_in_selected: true
    }
  end
end

Warning: do not provide model with order, as it messes with the distinct used in the facet computations.

In your controller, use it:

@facets = Item::Facets.new params[:facets]
@items = @facets.results.order(:title).page params[:page]

In your view, do something like that (with bootstrap):

<div class="row">
  <div class="col-md-3">
    <%= render 'faceted_search/facets', facets: @facets %>
  </div>
  <div class="col-md-9">
    <div class="row">
      <% @items.each do |item| %>
        <div class="col-md-4">
          <h2><%= item %></h2>
          <p>Products: <%= item.products.join(', ') %></p>
          <p>Categories: <%= item.categories.join(', ') %></p>
          <p>Kinds: <%= item.kinds.join(', ') %></p>
        </div>
      <% end %>
    </div>
  </div>
</div>

If you need, you can add an anchor to the links:

<%= render 'faceted_search/facets', facets: @facets, anchor: "#identifier" %>

Note about full tree

Tagging must be logical in order to use filter_with_full_tree. With these categories:

Blues
    Chicago blues
    Delta Blues
    Memphis Blues
Jazz
    Free jazz
    Swing
    Latin jazz

If something is tagged as "Delta blues", it MUST be tagged as "Blues" as well. Otherwise, it creates very odd comportments (selecting "Blues" does not show the object, whereas it is "Delta blues"). There is no inference whatsoever, so the data MUST be clean.

The HTML code is ready for Nestable2 implementation. If you want to use this library, add the CSS & JS in your vendor folder and import them in application.sass & application.js.

License

The gem is available as open source under the terms of the MIT License.