repertoire-faceting 0.5 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/INSTALL +200 -3
- data/TODO +10 -7
- data/lib/repertoire-faceting/relation/calculations.rb +1 -1
- data/lib/repertoire-faceting/version.rb +1 -1
- data/lib/repertoire-faceting.rb +2 -0
- data/public/javascripts/rep.faceting/context.js +2 -1
- metadata +2 -1
data/INSTALL
CHANGED
@@ -1,7 +1,204 @@
|
|
1
|
-
|
1
|
+
=== Installing Repertoire Faceting
|
2
|
+
|
3
|
+
== Short version. (Assuming you have a working Rails 3 app, with a model, controller, and a partial to show to model)
|
4
|
+
|
5
|
+
{ in Gemfile }
|
6
|
+
gem 'repertoire-faceting' // 1
|
7
|
+
|
8
|
+
{ install native bitset extensions }
|
9
|
+
$ bundle install
|
10
|
+
$ rake faceting:postgres:install { & provide sudo your password }
|
11
|
+
$ createlang plpgsql <database> -U<username>
|
12
|
+
$ psql -f $(pg_config --sharedir)/contrib/signature.sql -U<username> <database>
|
13
|
+
|
14
|
+
{ in ./app/models/some_model.rb }
|
15
|
+
class SomeModel
|
16
|
+
include Repertoire::Faceting::Model // 2
|
17
|
+
facet :some_column // 3
|
18
|
+
end
|
19
|
+
|
20
|
+
{ in ./app/controllers/some_controller]
|
21
|
+
class SomeController
|
22
|
+
include Repertoire::Faceting::Controller // 4
|
23
|
+
def base; return SomeModel; end // 5
|
24
|
+
end
|
25
|
+
|
26
|
+
{ in ./config/routes.rb }
|
27
|
+
SomeApp::Application.routes.draw do
|
28
|
+
faceting_for :some_model // 6
|
29
|
+
end
|
30
|
+
|
31
|
+
{ in ./public/javascripts/application.js }
|
32
|
+
//= require <rep.faceting> // 7
|
33
|
+
|
34
|
+
{ in ./app/views/some_controller/index.html.erb }
|
35
|
+
<script language="javascript">
|
36
|
+
$().ready(function() {
|
37
|
+
$('#paintings').facet_context(function() { return { } }); // 8
|
38
|
+
$('.facet').facet(); // 9
|
39
|
+
$('#results').results(); // 10
|
40
|
+
});
|
41
|
+
</script>
|
42
|
+
<div id='paintings'>
|
43
|
+
<div id='genre' class='facet'></div>
|
44
|
+
<div id='results'></div>
|
45
|
+
</div>
|
46
|
+
|
47
|
+
... that's a complete faceted browser in only 10 new lines of code in your app!
|
48
|
+
|
49
|
+
In production, you will want to start compressing the facet widgets:
|
50
|
+
|
51
|
+
{ in ./config/environments/production.rb }
|
52
|
+
config.repertoire_assets.compress = true
|
53
|
+
|
54
|
+
And to add facet indexing (scalable to a million items out of the box):
|
55
|
+
|
56
|
+
$ rails generate migration add_facet_index
|
57
|
+
|
58
|
+
|
59
|
+
== Detailed version.
|
2
60
|
|
3
61
|
Start with a working Rails 3 application with a PostgreSQL database.
|
4
62
|
|
5
|
-
*
|
63
|
+
* Require repertoire-faceting in Gemfile.
|
64
|
+
|
65
|
+
gem 'repertoire-faceting'
|
66
|
+
|
67
|
+
* Make sure you use Rails version 3.0.2+, which adopted Arel 2.0.1. If necessary you can pull rails, arel, and rack from git.
|
68
|
+
|
69
|
+
gem 'rails', :git => 'git://github.com/rails/rails.git'
|
70
|
+
gem 'arel', :git => 'git://github.com/rails/arel.git'
|
71
|
+
gem "rack", :git => "git://github.com/rack/rack.git"
|
72
|
+
|
73
|
+
* At the command line, bundle everything into your application:
|
74
|
+
|
75
|
+
$ bundle install
|
76
|
+
|
77
|
+
* From your application root, build and install the repertoire-faceting native extensions to PostgreSQL. These provide
|
78
|
+
a bitwise signature type used to index facets.
|
79
|
+
|
80
|
+
$ rake faceting:postgres:install { sudo will prompt you for your password }
|
81
|
+
|
82
|
+
* Load the extension into application database. Change depending on your postgres configuration:
|
83
|
+
|
84
|
+
$ createlang plpgsql <database> -U<username>
|
85
|
+
$ psql -f $(pg_config --sharedir)/contrib/signature.sql -U<username> <database>
|
86
|
+
|
87
|
+
You can confirm the module installed as follows. The database server may need to be restarted first.
|
88
|
+
|
89
|
+
$ psql -c "SELECT count('101010101'::signature);" -U<username> <database>
|
90
|
+
|
91
|
+
If the module installed correctly, psql will respond with "5".
|
92
|
+
|
93
|
+
* Install the faceting mixin in your Rails model and declare a facet on an existing database column. (See the README for complete
|
94
|
+
configuration options for facets.) [ ./app/models/painting.rb ]
|
95
|
+
|
96
|
+
class Painting
|
97
|
+
include Repertoire::Faceting::Model
|
98
|
+
facet :genre
|
99
|
+
end
|
100
|
+
|
101
|
+
* Test doing facet count and result queries:
|
102
|
+
|
103
|
+
$ rails c
|
104
|
+
> Painting.count(:genre)
|
105
|
+
=> {"Impressionist"=>2, "Medieval"=>2}
|
106
|
+
> Painting.refine(:genre => 'Impressionist')
|
107
|
+
=> [#<Painting id: 1, title: "Moonlight Shimmers", painter: "Monet", genre: "Impressionist">,
|
108
|
+
#<Painting id: 2, title: "Nude Lunch in Garden", painter: "Manet", genre: "Impressionist">]
|
109
|
+
|
110
|
+
Or, with a base query as well:
|
111
|
+
|
112
|
+
> Painting.where(["title like ?", 'Moon%']).count(:genre)
|
113
|
+
=> {"Impressionist"=>1}
|
114
|
+
|
115
|
+
* Add faceting webservices to your controller and define base() to indicate which model to base queries on [ ./app/controllers/paintings_controller]
|
116
|
+
|
117
|
+
class PaintingsController
|
118
|
+
include Repertoire::Faceting::Controller
|
119
|
+
|
120
|
+
def base
|
121
|
+
search = "%#{params[:search]}%"
|
122
|
+
Painting.where(["title like ?", search])
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
* Add faceting routes to your application. [ ./config/routes.rb ]
|
127
|
+
|
128
|
+
PaintingsApp::Application.routes.draw do
|
129
|
+
faceting_for :paintings # NB must be BEFORE any resources!
|
130
|
+
...
|
131
|
+
end
|
132
|
+
|
133
|
+
Confirm they load:
|
134
|
+
|
135
|
+
$ rake routes
|
136
|
+
...
|
137
|
+
/paintings/counts/:facet(.:format) {:controller=>"paintings", :action=>"counts"}
|
138
|
+
paintings_results /paintings/results(.:format) {:controller=>"paintings", :action=>"results"}
|
139
|
+
...
|
140
|
+
|
141
|
+
* Load the javascript facet widgets in your main javascript file. The repertoire-assets subsystem will locate the
|
142
|
+
widgets in the repertoire-faceting rubygem and load them as appropriate.
|
143
|
+
|
144
|
+
[ ./public/javascripts/application.js ]
|
145
|
+
...
|
146
|
+
//= require <rep.faceting>
|
147
|
+
...
|
148
|
+
|
149
|
+
* Add facet count and result widgets to your HTML page. The facet context div collects widgets that affect the same
|
150
|
+
query together. (For complete options, see the README )
|
151
|
+
|
152
|
+
[ ./app/views/paintings/index.html.erb ]
|
153
|
+
|
154
|
+
<script language="javascript">
|
155
|
+
$().ready(function() {
|
156
|
+
$('#paintings').facet_context(function() { return { } });
|
157
|
+
$('.facet').facet();
|
158
|
+
$('#results').results();
|
159
|
+
});
|
160
|
+
</script>
|
161
|
+
<div id='paintings'>
|
162
|
+
<div id='genre' class='facet'></div>
|
163
|
+
<div id='results'></div>
|
164
|
+
</div>
|
165
|
+
|
166
|
+
* If you don't already have one, create a partial for displaying your model in results lists.
|
167
|
+
|
168
|
+
[ ./app/views/paintings/_painting.html.erb ]
|
169
|
+
|
170
|
+
<div class='painting' style='width:235px; margin-bottom:5px; padding:2px; border:dotted 1px;'>
|
171
|
+
<div>Title: <%= painting.title %></div>
|
172
|
+
<div>Painter: <%= painting.painter %></div>
|
173
|
+
<div>Genre: <%= painting.genre %></div>
|
174
|
+
</div>
|
175
|
+
|
176
|
+
* Configure the repertoire-assets module that loads the javascript faceting widgets to compress in
|
177
|
+
production. [ ./config/environments/production.rb ]
|
178
|
+
|
179
|
+
PaintingsApp::Application.configure do
|
180
|
+
...
|
181
|
+
config.repertoire_assets.compress = true
|
182
|
+
...
|
183
|
+
end
|
184
|
+
|
185
|
+
* [ Optional ] Add bitset indexes to some facets on your model. The module will automatically
|
186
|
+
use facet indexes when they are available.
|
187
|
+
|
188
|
+
class AddFacetIndex < ActiveRecord::Migration
|
189
|
+
def self.up
|
190
|
+
Painting.update_indexed_facets([:genre])
|
191
|
+
end
|
192
|
+
|
193
|
+
def self.down
|
194
|
+
Painting.update_indexed_facets
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
* [ Optional ] Periodically update indexes via a crontab task. [ ./lib/tasks/update_facets.rake ]
|
199
|
+
|
200
|
+
task :reindex_facets => :environment do
|
201
|
+
Painting.update_indexed_facets
|
202
|
+
end
|
6
203
|
|
7
|
-
|
204
|
+
... And then configure crontab to execute 'rake reindex_facets' at appropriate intervals.
|
data/TODO
CHANGED
@@ -2,19 +2,22 @@ TODO
|
|
2
2
|
|
3
3
|
- README
|
4
4
|
* recipe for running tests
|
5
|
-
* recipe for adding facets to new app
|
6
|
-
* declaring facets
|
7
|
-
* faceting db api
|
8
|
-
* installing postgresql extensions
|
9
|
-
* migrations for indexing
|
10
|
-
* updating indices (a) postgresql-crontab (b) rake crontab
|
5
|
+
* recipe for adding facets to new app DONE
|
6
|
+
* declaring facets DONE
|
7
|
+
* faceting db api DONE
|
8
|
+
* installing postgresql extensions DONE
|
9
|
+
* migrations for indexing DONE
|
10
|
+
* updating indices (a) postgresql-crontab (b) rake crontab DONE
|
11
11
|
- generate routes that don't conflict with resource routes
|
12
|
-
(rails thinks /nobelists/results is the nobelist named 'results')
|
12
|
+
(rails thinks /nobelists/results is the nobelist named 'results') NOT TO DO
|
13
13
|
- FAQ
|
14
14
|
* "not grouped error"
|
15
15
|
* migrations and facet decls
|
16
16
|
* facet plugin registration (and multiple claims)
|
17
17
|
|
18
|
+
- repertoire-assets loaded automatically DONE
|
19
|
+
- figure out the faceting context state default DONE
|
20
|
+
|
18
21
|
- get rid of annoying load warnings on test KNOWN ISSUE
|
19
22
|
- cannot refine on null values in facets KNOWN ISSUE
|
20
23
|
|
@@ -7,7 +7,7 @@ module Repertoire
|
|
7
7
|
|
8
8
|
# Construct and execute a count over the specified facet.
|
9
9
|
def count(name = nil, options = {})
|
10
|
-
if name.present? && facet?(name)
|
10
|
+
if name.present? && @klass.facet?(name)
|
11
11
|
name = name.to_sym
|
12
12
|
facet = @klass.facets[name].merge(self)
|
13
13
|
state = refine_value[name] || []
|
data/lib/repertoire-faceting.rb
CHANGED
@@ -92,7 +92,8 @@ repertoire.facet_context = function(context_name, state_fn, options) {
|
|
92
92
|
// with any context-specific additions
|
93
93
|
//
|
94
94
|
self.params = function() {
|
95
|
-
|
95
|
+
var state = state_fn ? state_fn() : {};
|
96
|
+
return $.extend({}, { filter: self.refinements() }, state);
|
96
97
|
};
|
97
98
|
|
98
99
|
//
|