Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/noesya/faceted_search


https://github.com/noesya/faceted_search

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

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

[![Maintainability](https://api.codeclimate.com/v1/badges/70579009d11cfa0d7cac/maintainability)](https://codeclimate.com/github/lespoupeesrusses/faceted_search/maintainability)

## Installation
Add this line to your application's Gemfile:

```ruby
gem 'faceted_search'
```

And then execute:
```bash
$ bundle
```

Or install it yourself as:
```bash
$ 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):



<%= render 'faceted_search/facets', facets: @facets %>



<% @items.each do |item| %>

<%= item %>


Products: <%= item.products.join(', ') %>


Categories: <%= item.categories.join(', ') %>


Kinds: <%= item.kinds.join(', ') %>



<% end %>


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](https://github.com/RamonSmit/Nestable2) implementation. If you want to use this library, add the [CSS](https://github.com/RamonSmit/Nestable2/blob/master/dist/jquery.nestable.min.css) & [JS](https://github.com/RamonSmit/Nestable2/blob/master/dist/jquery.nestable.min.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](https://opensource.org/licenses/MIT).