faceted_search 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1ce3ce9087daf197f5f45dd9a520a4bfcdd462c200ecc5d7ed22d1c4e38be7e5
4
+ data.tar.gz: c8ce82304faa681237b3d7d8233550ef633c8140dceca5d457c9e2a784b1a864
5
+ SHA512:
6
+ metadata.gz: 13065dc90a9ce7827fb938b824a6d724192ac43496cd76ef106534876cdb505476b0ccc02faeb49a65e0fb81e9d85488a26e01e5be3c8346fecf2dff305c8d83
7
+ data.tar.gz: ff8498afd99bdfa14aeecab05a427642e0d35ad408c6dbbf09ecaa3df40beb5fc49fde29109369f62e86b70b697365c514d10d6ea054f4d1fc5353e63cac2f78
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Arnaud Levy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # FacetedSearch
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'faceted_search'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install faceted_search
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'FacetedSearch'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,68 @@
1
+ module FacetedSearch
2
+ class Facets
3
+ attr_accessor :list
4
+
5
+ def initialize(params)
6
+ @params = params.nil? ? {}
7
+ : params.to_unsafe_hash
8
+ @list = []
9
+ define
10
+ end
11
+
12
+ def path
13
+ '?facets'
14
+ end
15
+
16
+ def path_for(facet, value)
17
+ p = path
18
+ @list.each do |current_facet|
19
+ p += current_facet == facet ? current_facet.path_for(value)
20
+ : current_facet.path
21
+ end
22
+ p
23
+ end
24
+
25
+ def results
26
+ scope = @model
27
+ list.each do |facet|
28
+ scope = facet.add_scope(scope)
29
+ end
30
+ scope
31
+ end
32
+
33
+ def model_table_name
34
+ @model.table_name
35
+ end
36
+
37
+ protected
38
+
39
+ def define
40
+ # Override
41
+ end
42
+
43
+ def set_model(model)
44
+ @model = model
45
+ end
46
+
47
+ def search(value)
48
+ add(Search.new( name: value,
49
+ params: params_for(value),
50
+ facets: self))
51
+ end
52
+
53
+ def filter(value, find_by: :id)
54
+ add(Filter.new( name: value,
55
+ params: params_for(value),
56
+ facets: self,
57
+ find_by: find_by))
58
+ end
59
+
60
+ def params_for(value)
61
+ @params[value] if @params.has_key? value
62
+ end
63
+
64
+ def add(facet)
65
+ @list << facet
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,38 @@
1
+ module FacetedSearch
2
+ class Facets::Default
3
+ attr_reader :name, :params, :facets, :find_by
4
+
5
+ def initialize(name:, params:, facets:, find_by: nil)
6
+ @name = name
7
+ @params = params
8
+ @facets = facets
9
+ @find_by = find_by
10
+ end
11
+
12
+ def title
13
+ name.to_s.humanize.titleize
14
+ end
15
+
16
+ def kind
17
+ self.class.to_s
18
+ end
19
+
20
+ def path_for(value)
21
+ "&facets[#{name}]=#{value}"
22
+ end
23
+
24
+ def add_scope(scope)
25
+ # Override
26
+ scope
27
+ end
28
+
29
+ def path(custom_params = @params)
30
+ return '' if custom_params.blank?
31
+ "&facets[#{@name}]=#{custom_params}"
32
+ end
33
+
34
+ def to_s
35
+ "#{title}"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ module FacetedSearch
2
+ class Facets::Filter < Facets::Default
3
+ def path_for(value)
4
+ value = value.to_s
5
+ custom_params = params_array.dup
6
+ selected?(value) ? custom_params.delete(value)
7
+ : custom_params.push(value)
8
+ path(custom_params.join(','))
9
+ end
10
+
11
+ def selected?(value)
12
+ value.to_s.in? params_array
13
+ end
14
+
15
+ def params_array
16
+ @params_array ||= @params.to_s.split(',')
17
+ end
18
+
19
+ def add_scope(scope)
20
+ return scope if params_array.blank?
21
+ scope.joins(name).where(name => { find_by => params_array })
22
+ end
23
+
24
+ def values
25
+ source.all
26
+ end
27
+
28
+ def source
29
+ @name.to_s.singularize.titleize.constantize
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,8 @@
1
+ module FacetedSearch
2
+ class Facets::Search < Facets::Default
3
+ def add_scope(scope)
4
+ return scope if @params.blank?
5
+ scope.where("#{@facets.model_table_name}.#{name} ILIKE ?", "%#{@params}%")
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ <ul class="list-unstyled">
2
+ <% @facets.list.each do |facet| %>
3
+ <li class="mb-3">
4
+ <b><%= facet.title %></b>
5
+ <%= render facet.kind.underscore, facet: facet %>
6
+ </li>
7
+ <% end %>
8
+ </ul>
@@ -0,0 +1,13 @@
1
+ <ul class="list-unstyled">
2
+ <% facet.values.each do |value| %>
3
+ <%
4
+ identifier = value.send facet.find_by
5
+ selected = facet.selected? identifier
6
+ %>
7
+ <li>
8
+ <% if selected %><b><% end %>
9
+ <%= link_to value, @facets.path_for(facet, identifier) %>
10
+ <% if selected %></b><% end %>
11
+ </li>
12
+ <% end %>
13
+ </ul>
@@ -0,0 +1,17 @@
1
+ <% value = params[:facets][facet.name] if params[:facets] && params[:facets].has_key?(facet.name) %>
2
+ <form action="" class="form-inline"">
3
+ <% facet.facets.list.each do |f| %>
4
+ <% next if f == facet %>
5
+ <% next if f.params.blank? %>
6
+ <input type="hidden"
7
+ name="facets[<%= f.name %>]"
8
+ value="<%= f.params %>" />
9
+ <% end %>
10
+ <input type="search"
11
+ name="facets[<%= facet.name %>]"
12
+ value="<%= value %>"
13
+ class="form-control" />
14
+ <input type="submit"
15
+ class="btn btn-light"
16
+ value="OK" />
17
+ </form>
@@ -0,0 +1,7 @@
1
+ require "faceted_search/railtie"
2
+
3
+ module FacetedSearch
4
+ module Rails
5
+ require "faceted_search/engine"
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ class FacetedSearch::Rails::Engine < ::Rails::Engine
2
+ end
@@ -0,0 +1,4 @@
1
+ module FacetedSearch
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module FacetedSearch
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :faceted_search do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faceted_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Arnaud Levy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simple_form
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bootstrap
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: kaminari
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: All you need to create a faceted search, as simply as possible
84
+ email:
85
+ - alevy@lespoupees.paris
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - app/models/faceted_search/facets.rb
94
+ - app/models/faceted_search/facets/default.rb
95
+ - app/models/faceted_search/facets/filter.rb
96
+ - app/models/faceted_search/facets/search.rb
97
+ - app/views/faceted_search/_facets.html.erb
98
+ - app/views/faceted_search/facets/_filter.html.erb
99
+ - app/views/faceted_search/facets/_search.html.erb
100
+ - lib/faceted_search.rb
101
+ - lib/faceted_search/engine.rb
102
+ - lib/faceted_search/railtie.rb
103
+ - lib/faceted_search/version.rb
104
+ - lib/tasks/faceted_search_tasks.rake
105
+ homepage: http://github.com/lespoupeesrusses/faceted_search
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.7.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Faceted search with Active Record
129
+ test_files: []