blacklight_facet_extras 0.0.1pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/README.rdoc +93 -0
  2. data/Rakefile +5 -0
  3. data/VERSION +1 -0
  4. data/app/views/catalog/_facet_limit.html.erb +35 -0
  5. data/app/views/catalog/_facet_partials/_pivot.html.erb +47 -0
  6. data/app/views/catalog/_facet_partials/_query.html.erb +35 -0
  7. data/app/views/catalog/_facet_partials/_range.html.erb +40 -0
  8. data/app/views/catalog/_hierarchical_constraints_element.html.erb +39 -0
  9. data/assets/javascripts/blacklight_facet_extras.js +0 -0
  10. data/assets/stylesheets/blacklight_facet_extras.css +0 -0
  11. data/blacklight_facet_extras.gemspec +24 -0
  12. data/lib/blacklight_facet_extras/controller_extension.rb +9 -0
  13. data/lib/blacklight_facet_extras/engine.rb +14 -0
  14. data/lib/blacklight_facet_extras/facet_item.rb +11 -0
  15. data/lib/blacklight_facet_extras/filter/controller_extension.rb +19 -0
  16. data/lib/blacklight_facet_extras/filter/view_helper_extension.rb +18 -0
  17. data/lib/blacklight_facet_extras/filter.rb +4 -0
  18. data/lib/blacklight_facet_extras/hierarchy/controller_extension.rb +39 -0
  19. data/lib/blacklight_facet_extras/hierarchy/view_helper_extension.rb +43 -0
  20. data/lib/blacklight_facet_extras/hierarchy.rb +6 -0
  21. data/lib/blacklight_facet_extras/pivot/controller_extension.rb +35 -0
  22. data/lib/blacklight_facet_extras/pivot/view_helper_extension.rb +53 -0
  23. data/lib/blacklight_facet_extras/pivot.rb +19 -0
  24. data/lib/blacklight_facet_extras/query/controller_extension.rb +41 -0
  25. data/lib/blacklight_facet_extras/query/view_helper_extension.rb +24 -0
  26. data/lib/blacklight_facet_extras/query.rb +4 -0
  27. data/lib/blacklight_facet_extras/range/controller_extension.rb +43 -0
  28. data/lib/blacklight_facet_extras/range/view_helper_extension.rb +39 -0
  29. data/lib/blacklight_facet_extras/range.rb +19 -0
  30. data/lib/blacklight_facet_extras/route_sets.rb +3 -0
  31. data/lib/blacklight_facet_extras/tag/controller_extension.rb +41 -0
  32. data/lib/blacklight_facet_extras/tag/view_helper_extension.rb +3 -0
  33. data/lib/blacklight_facet_extras/tag.rb +4 -0
  34. data/lib/blacklight_facet_extras/version.rb +10 -0
  35. data/lib/blacklight_facet_extras/view_helper_extension.rb +14 -0
  36. data/lib/blacklight_facet_extras.rb +34 -0
  37. metadata +105 -0
data/README.rdoc ADDED
@@ -0,0 +1,93 @@
1
+ BlacklightFacetExtras: Blacklight plugin that exposes Solr facet parameters (range, query, tagging + exclusions, etc) to Blacklight
2
+
3
+ = Description
4
+
5
+ BlacklightFacetExtras adds some ugly parsing and manipulating to the Blacklight solr configuration and solr parameter parsing, but seems to get the job done, more or less.
6
+
7
+ = Requirements
8
+
9
+ A Rails app using the Blacklight plugin (tested against post-version 2.7).
10
+
11
+ = Installation
12
+
13
+ This is a plugin, not a gem (because the structure was copied from existing plugins; in theory, it should be possible to make this a gem in the future).
14
+
15
+ A couple different ways to get the source into your vendor/plugins directory.
16
+
17
+ Go into your application directory, and run:
18
+
19
+ ./script/plugin install git://github.com/cbeer/blacklight_facet_extras.git
20
+
21
+ Later you can run ./script/plugin update blacklight_facet_extras if you like.
22
+
23
+ Requires git installed on your system. There are other ways to get the plugin in there too.
24
+
25
+ OR
26
+ cd $your_app/vendor/plugins
27
+ git clone git://github.com/cbeer/blacklight_facet_extras.git
28
+
29
+ = Configuration
30
+
31
+ config[:facets] = {
32
+ :field_names => [
33
+ "format",
34
+ "dc_date_year_i",
35
+ "timestamp_query",
36
+ "author"
37
+ ],
38
+ :labels => {
39
+ "format" => "display_partial",
40
+ "dc_date_year_i" => "Year",
41
+ "timestamp_query" => "Published"
42
+ }
43
+ :rangex => {
44
+ "dc_date_year_i" => { # turns dc_date_year_i into a range facet
45
+ :start => 1940,
46
+ :end => 2010,
47
+ :gap => 10
48
+ }
49
+ },
50
+ :tag => {
51
+ "format" => { # turns format into a OR'ed set of facets
52
+ :ex => "format"
53
+ }
54
+ },
55
+ :query => {
56
+ "timestamp_query" => { # the facet "Published" may contain two values, the results of these queries:
57
+ 'this week' => 'timestamp:[NOW-7DAY TO *]'
58
+ 'this week' => 'timestamp:[NOW-1MONTH TO *]'
59
+ }
60
+ },
61
+ :pivot => {
62
+ "author" => ["author", "title"]
63
+ },
64
+ :filter => {
65
+ "subject" => lambda { |value| value =~ /--/ },
66
+ "date" => lambda { |value| Chronic.parse(value) rescue value }
67
+ }
68
+ }
69
+
70
+ == Injection
71
+
72
+ This plugin assumes it is in a Blacklight Rails app, uses Blacklight methods, Rails methods, and standard ruby module includes to inject it's behaviors into the app.
73
+
74
+ You can turn off this injection if you like, although it will make the plugin less (or non-) functional unless you manually do similar injection. See lib/blacklight_facet_extras.rb#inject! to see exactly what's going on.
75
+
76
+ In any initializer, you can set:
77
+
78
+ BlacklightFacetExtras.omit_inject = true
79
+
80
+ to turn off all injection. The plugin will be completely non-functional if you do this, of course. But perhaps you could try to re-use some of it's classes in a non-Blacklight, highly hacked Blacklight, or even non-Rails application this way.
81
+
82
+ You can also turn off injection of individual components, which could be more useful:
83
+
84
+ BlacklightFacetExtras.omit_inject = {
85
+ :view_helpers => false,
86
+ :controller_mixin => false
87
+ }
88
+
89
+ = Tests
90
+
91
+ There are none. This is bad I know, sorry.
92
+
93
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rake'
2
+ require 'bundler'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1pre1
@@ -0,0 +1,35 @@
1
+ <%- # required to pass in local :solr_field
2
+ # optional :display_heading parameter, sometimes set to false
3
+ # by plugins that might re-use this inside something else.
4
+ display_heading = true unless local_assigns.has_key?(:display_heading)
5
+ -%>
6
+
7
+ <% display_facet = facet_values_for(solr_field) %>
8
+ <% if display_facet && display_facet.items.length > 0 %>
9
+ <% if display_heading %>
10
+ <h3><%= facet_field_labels[solr_field] -%></h3>
11
+ <% end %>
12
+ <ul>
13
+ <% paginator =
14
+ Blacklight::Solr::FacetPaginator.new(display_facet.items,
15
+ :limit => facet_limit_for(solr_field))
16
+ %>
17
+
18
+ <% paginator.items.each do |item| -%>
19
+ <li>
20
+ <% if facet_in_params?( solr_field, item.value ) %>
21
+ <%= render_selected_facet_value(solr_field, item) %>
22
+ <% else %>
23
+ <%= render_facet_value(solr_field, item) %>
24
+ <% end -%>
25
+ </li>
26
+
27
+ <% end %>
28
+
29
+ <% if(paginator.has_next?) %>
30
+ <li class="more_facets_link"><%= link_to('more »', params.merge(:id => solr_field, :action=>"facet"), :class => "more_facets_link") %></li>
31
+ <% end %>
32
+
33
+ </ul>
34
+
35
+ <% end %>
@@ -0,0 +1,47 @@
1
+ <%- # required to pass in local :solr_field
2
+ # optional :display_heading parameter, sometimes set to false
3
+ # by plugins that might re-use this inside something else.
4
+ display_heading = true unless local_assigns.has_key?(:display_heading)
5
+ -%>
6
+
7
+ <% display_facet = solr_pivot_to_a(solr_field) %>
8
+ <% if display_facet && display_facet.items.length > 0 %>
9
+ <% if display_heading %>
10
+ <h3><%= facet_field_labels[solr_field] -%></h3>
11
+ <% end %>
12
+ <ul>
13
+ <% paginator =
14
+ Blacklight::Solr::FacetPaginator.new(display_facet.items,
15
+ :limit => facet_limit_for(solr_field))
16
+ %>
17
+ <% paginator.items.each do |item| -%>
18
+ <li>
19
+ <% if facet_in_params?( solr_field, item.value ) %>
20
+ <%= render_selected_facet_value(solr_field, item) %>
21
+ <% else %>
22
+ <%= render_facet_value(solr_field, item) %>
23
+ <% end -%>
24
+
25
+ <% if item.facets.length > 0 %>
26
+ <ul>
27
+ <% item.facets.each do |pivot| %>
28
+ <li>
29
+ <% if facet_in_params?( pivot.field, pivot.value ) %>
30
+ <%= render_selected_facet_value(pivot.field, pivot) %>
31
+ <% else %>
32
+ <%= render_facet_value(pivot.field, pivot) %>
33
+ <% end -%>
34
+ </li>
35
+ <% end %>
36
+ </ul>
37
+ <% end %>
38
+ </li>
39
+ <% end %>
40
+
41
+ <% if(paginator.has_next?) %>
42
+ <li class="more_facets_link"><%= link_to('more »', params.merge(:id => solr_field, :action=>"facet"), :class => "more_facets_link") %></li>
43
+ <% end %>
44
+
45
+ </ul>
46
+ <% end %>
47
+
@@ -0,0 +1,35 @@
1
+ <%- # required to pass in local :solr_field
2
+ # optional :display_heading parameter, sometimes set to false
3
+ # by plugins that might re-use this inside something else.
4
+ display_heading = true unless local_assigns.has_key?(:display_heading)
5
+ -%>
6
+
7
+ <% display_facet = solr_query_to_a(solr_field) %>
8
+ <% if display_facet && display_facet.items.length > 0 %>
9
+ <% if display_heading %>
10
+ <h3><%= facet_field_labels[solr_field] -%></h3>
11
+ <% end %>
12
+ <ul>
13
+ <% paginator =
14
+ Blacklight::Solr::FacetPaginator.new(display_facet.items,
15
+ :limit => facet_limit_for(solr_field))
16
+ %>
17
+
18
+ <% paginator.items.each do |item| -%>
19
+ <li>
20
+ <% if facet_in_params?( solr_field, item.value ) %>
21
+ <%= render_selected_facet_value(solr_field, item) %>
22
+ <% else %>
23
+ <%= render_facet_value(solr_field, item) %>
24
+ <% end -%>
25
+ </li>
26
+
27
+ <% end %>
28
+
29
+ <% if(paginator.has_next?) %>
30
+ <li class="more_facets_link"><%= link_to('more »', params.merge(:id => solr_field, :action=>"facet"), :class => "more_facets_link") %></li>
31
+ <% end %>
32
+
33
+ </ul>
34
+
35
+ <% end %>
@@ -0,0 +1,40 @@
1
+ <%- # required to pass in local :solr_field
2
+ # optional :display_heading parameter, sometimes set to false
3
+ # by plugins that might re-use this inside something else.
4
+ display_heading = true unless local_assigns.has_key?(:display_heading)
5
+ -%>
6
+
7
+ <% display_facet = solr_range_to_a(solr_field) %>
8
+ <% if (display_facet && display_facet.items.length > 0) or (params[:f] and params[:f][solr_field]) %>
9
+ <% if display_heading %>
10
+ <h3><%= facet_field_labels[solr_field] -%></h3>
11
+ <% end %>
12
+ <ul>
13
+ <% paginator =
14
+ Blacklight::Solr::FacetPaginator.new(display_facet.items,
15
+ :limit => facet_limit_for(solr_field))
16
+ %>
17
+
18
+ <% params[:f][solr_field].select { |x| x =~ /\[(\d+) TO (\d+)\]/ }.map { |x| r = x.scan(/\[(\d+) TO (\d+)\]/).first; BlacklightFacetExtras::Range::FacetItem.new("#{r.first} - #{r.last}", @response.total, { :from => r.first, :to => r.last}) }.each do |item| -%>
19
+ <li>
20
+ <%= render_selected_facet_value(solr_field, item) %>
21
+ </li>
22
+ <% end if params[:f] and params[:f][solr_field] -%>
23
+ <% paginator.items.each do |item| -%>
24
+ <li>
25
+ <% if facet_in_params?( solr_field, item.value ) %>
26
+ <%= render_selected_facet_value(solr_field, item) %>
27
+ <% else %>
28
+ <%= render_facet_value(solr_field, item) %>
29
+ <% end -%>
30
+ </li>
31
+
32
+ <% end %>
33
+
34
+ <% if(paginator.has_next?) %>
35
+ <li class="more_facets_link"><%= link_to('more »', params.merge(:id => solr_field, :action=>"facet"), :class => "more_facets_link") %></li>
36
+ <% end %>
37
+
38
+ </ul>
39
+
40
+ <% end %>
@@ -0,0 +1,39 @@
1
+ <%- # local params:
2
+ # label
3
+ # value
4
+ # options =>
5
+ # :remove => url for a remove constraint link
6
+ # :classes => array of classes to add to container span
7
+ options ||= {}
8
+ options[:escape_label] = true unless options.has_key?(:escape_label)
9
+ options[:escape_value] = true unless options.has_key?(:escape_value)
10
+ -%>
11
+
12
+ <span class="appliedFilter constraint <%= options[:classes].join(" ") if options[:classes] %>">
13
+ <%- unless label.blank? -%>
14
+ <span class="filterName"><%= options[:escape_label] ? h(label) : label %></span>
15
+ <%- end -%>
16
+
17
+ <% asdf = [] %>
18
+ <% value.split("/").each do |v| %>
19
+ <%- unless v.blank? -%>
20
+ <span class="filterValue"><%= options[:escape_value] ? h(v) : v %></span>
21
+ <%- end -%>
22
+ <%- unless options[:remove].blank? -%>
23
+ <% accessible_remove_label =
24
+ if label.blank?
25
+ "Remove constraint #{options[:escape_value] ? h(v) : v}"
26
+ else
27
+ "Remove constraint #{options[:escape_value] ? h(v) : v}: #{options[:escape_value] ? h(v) : v}"
28
+ end
29
+ %>
30
+ <%= link_to(accessible_remove_label,
31
+ options[:remove].gsub(/=(#{Regexp.escape(CGI::escape(asdf.join("/")))})[^&]+/, "=#{CGI::escape(asdf.join("/"))}"), # FIXME: Ugh.
32
+ :class=>'btnRemove imgReplace',
33
+ :alt=>'remove'
34
+ ) %>
35
+ <% asdf << v %>
36
+ <% end %>
37
+ <%- end -%>
38
+ </span>
39
+
File without changes
File without changes
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), "lib/blacklight_facet_extras/version")
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "blacklight_facet_extras"
6
+ s.version = BlacklightFacetExtras::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Chris Beer"]
9
+ s.email = ["chris_beer@wgbh.org"]
10
+ s.homepage = "http://projectblacklight.org/"
11
+ s.summary = "Blacklight facet extras plugin"
12
+
13
+ s.rubyforge_project = "blacklight"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+
21
+ s.add_dependency "rails", "~> 3.0"
22
+ s.add_dependency "blacklight"
23
+ end
24
+
@@ -0,0 +1,9 @@
1
+ module BlacklightFacetExtras::ControllerExtension
2
+ def self.included(some_class)
3
+ some_class.helper_method :blacklight_facet_config
4
+ some_class.helper BlacklightFacetExtras::ViewHelperExtension
5
+ end
6
+ def blacklight_facet_config(solr_field)
7
+ {}
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require 'blacklight'
2
+ require 'blacklight_facet_extras'
3
+ require 'rails'
4
+
5
+ module BlacklightFacetExtras
6
+ class Engine < Rails::Engine
7
+ config.to_prepare do
8
+ unless BlacklightFacetExtras.omit_inject[:routes]
9
+ Blacklight::Routes.send(:include, BlacklightFacetExtras::RouteSets)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,11 @@
1
+ module BlacklightFacetExtras
2
+ class FacetItem < RSolr::Ext::Response::Facets::FacetItem
3
+ attr_accessor :display_label
4
+
5
+ def initialize value, hits, opts = {}
6
+ super(value, hits)
7
+ @display_label = opts[:display_label] || value
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ # Meant to be applied on top of a controller that implements
2
+ # Blacklight::SolrHelper. Will inject filter limiting behaviors
3
+ # to solr parameters creation.
4
+ module BlacklightFacetExtras::Filter::ControllerExtension
5
+ def self.included(some_class)
6
+ some_class.send :include,BlacklightFacetExtras::ControllerExtension
7
+ some_class.helper_method :facet_filter_config
8
+ some_class.helper BlacklightFacetExtras::Filter::ViewHelperExtension
9
+ end
10
+ def facet_filter_config(solr_field)
11
+ config = blacklight_filter_config[solr_field] || false
12
+ config = {} if config == true
13
+ config
14
+ end
15
+
16
+ def blacklight_filter_config
17
+ Blacklight.config[:facet][:filter] || {}
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module BlacklightFacetExtras::Filter::ViewHelperExtension
2
+ def facet_values_for(solr_field)
3
+ config = facet_filter_config(solr_field)
4
+ facet_field = super(solr_field)
5
+ return facet_field unless config
6
+
7
+ items = facet_field.items.map do |i|
8
+ value = config.call(i.value)
9
+ value = i.value if value === true
10
+ next unless value
11
+ BlacklightFacetExtras::FacetItem.new(i.value, i.hits, :display_label => value)
12
+ end
13
+
14
+ facet_field.items.replace(items)
15
+
16
+ facet_field
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module BlacklightFacetExtras::Filter
2
+ autoload :ControllerExtension, 'blacklight_facet_extras/filter/controller_extension'
3
+ autoload :ViewHelperExtension, 'blacklight_facet_extras/filter/view_helper_extension'
4
+ end
@@ -0,0 +1,39 @@
1
+ # Meant to be applied on top of a controller that implements
2
+ # Blacklight::SolrHelper. Will inject hierarchy limiting behaviors
3
+ # to solr parameters creation.
4
+ module BlacklightFacetExtras::Hierarchy::ControllerExtension
5
+ def self.included(some_class)
6
+ some_class.send :include,BlacklightFacetExtras::ControllerExtension
7
+ some_class.helper_method :facet_hierarchy_config
8
+ some_class.helper_method :blacklight_hierarchy_config
9
+ some_class.solr_search_params_logic << :add_hierarchy_facets_to_solr
10
+ some_class.helper BlacklightFacetExtras::Hierarchy::ViewHelperExtension
11
+ end
12
+ def add_hierarchy_facets_to_solr(solr_parameters, user_parameters)
13
+ blacklight_hierarchy_config.each do |k, config|
14
+
15
+ fq = (solr_parameters[:fq] || []).select { |x| x.starts_with? "{!raw f=#{k}}" }.first.to_s
16
+
17
+ value = fq.gsub("{!raw f=#{k}}", "")
18
+ solr_parameters[:fq].delete(fq)
19
+
20
+ if value.blank?
21
+ solr_parameters[:"f.#{k}.facet.prefix"] ||= "1/"
22
+ else
23
+ solr_parameters[:fq] << "{!raw f=#{k}}#{value.count("/") + 1}/#{value}"
24
+ solr_parameters[:"f.#{k}.facet.prefix"] ||= "#{value.count("/") + 2}/#{value}/"
25
+ end
26
+ end
27
+
28
+ solr_parameters
29
+ end
30
+ def facet_hierarchy_config(solr_field)
31
+ config = blacklight_hierarchy_config[solr_field] || false
32
+ config = {} if config == true
33
+ config
34
+ end
35
+
36
+ def blacklight_hierarchy_config
37
+ Blacklight.config[:facet][:hierarchy] || {}
38
+ end
39
+ end
@@ -0,0 +1,43 @@
1
+ module BlacklightFacetExtras::Hierarchy::ViewHelperExtension
2
+ def facet_values_for(solr_field)
3
+ config = facet_hierarchy_config(solr_field)
4
+ facet_field = super(solr_field)
5
+ return facet_field unless config
6
+
7
+ items = facet_field.items.map do |i|
8
+ value = i.value.split('/').last
9
+ BlacklightFacetExtras::FacetItem.new(i.value.split("/", 2).last, i.hits, :display_label => value)
10
+ end
11
+
12
+ facet_field.items.replace(items)
13
+
14
+ facet_field
15
+ end
16
+
17
+ def add_facet_params field, value
18
+ p = super(field, value)
19
+ p[:f][field] = [p[:f][field].last] if facet_hierarchy_config(field)
20
+ p
21
+ end
22
+
23
+ def remove_facet_params(field, value, source_params=params)
24
+ p = super(field, value, source_params)
25
+
26
+ if facet_hierarchy_config(field)
27
+ hierarchy = value.split("/")
28
+ hierarchy.pop
29
+ unless hierarchy.empty?
30
+ p[:f][field] ||= []
31
+ p[:f][field] << hierarchy.join("/")
32
+ end
33
+ end
34
+ p
35
+ end
36
+
37
+ def render_constraint_element(label, value, options = {})
38
+ return super(label, value, options) unless options[:classes].any? { |x| blacklight_hierarchy_config.keys.map { |y| "filter-#{y.parameterize}" }.include? x }
39
+
40
+ return ''.html_safe if value.blank?
41
+ render(:partial => "catalog/hierarchical_constraints_element", :locals => {:label => label, :value => value, :options => options})
42
+ end
43
+ end
@@ -0,0 +1,6 @@
1
+ module BlacklightFacetExtras
2
+ module Hierarchy
3
+ autoload :ControllerExtension, 'blacklight_facet_extras/hierarchy/controller_extension'
4
+ autoload :ViewHelperExtension, 'blacklight_facet_extras/hierarchy/view_helper_extension'
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+
2
+ # Meant to be applied on top of a controller that implements
3
+ # Blacklight::SolrHelper. Will inject pivot limiting behaviors
4
+ # to solr parameters creation.
5
+ module BlacklightFacetExtras::Pivot::ControllerExtension
6
+ def self.included(some_class)
7
+ some_class.send :include,BlacklightFacetExtras::ControllerExtension
8
+ some_class.helper_method :facet_pivot_config
9
+ some_class.solr_search_params_logic << :add_pivot_facets_to_solr
10
+ some_class.helper BlacklightFacetExtras::Pivot::ViewHelperExtension
11
+ end
12
+ def add_pivot_facets_to_solr(solr_parameters, user_parameters)
13
+
14
+ blacklight_pivot_config.each do |k, config|
15
+ solr_parameters[:"facet.field"].select { |x| x == k }.each do |x|
16
+ solr_parameters[:"facet.field"].delete x
17
+ end if solr_parameters[:"facet.field"]
18
+
19
+ solr_parameters[:"facet.pivot"] ||= []
20
+ solr_parameters[:"facet.pivot"] << config.join(",")
21
+ end
22
+
23
+ solr_parameters
24
+ end
25
+
26
+ def facet_pivot_config(solr_field)
27
+ config = blacklight_pivot_config[solr_field] || false
28
+ config = {} if config == true
29
+ config
30
+ end
31
+
32
+ def blacklight_pivot_config
33
+ Blacklight.config[:facet][:pivot] || {}
34
+ end
35
+ end
@@ -0,0 +1,53 @@
1
+ module BlacklightFacetExtras::Pivot::ViewHelperExtension
2
+
3
+ def render_facet_limit(solr_field)
4
+ config = facet_pivot_config(solr_field)
5
+ if ( config )
6
+ render(:partial => "catalog/_facet_partials/pivot", :locals=> {:solr_field => solr_field })
7
+ else
8
+ super(solr_field)
9
+ end
10
+ end
11
+ def solr_pivot_to_a(solr_field)
12
+ config = facet_pivot_config(solr_field)
13
+ return RSolr::Ext::Response::Facets::FacetField.new(solr_field, []) unless config and @response and @response["facet_counts"] and @response["facet_counts"]["facet_pivot"] and @response["facet_counts"]["facet_pivot"]
14
+
15
+ arr = []
16
+ pivot_field = config.join(",")
17
+
18
+ data = @response["facet_counts"]["facet_pivot"][pivot_field]
19
+ return RSolr::Ext::Response::Facets::FacetField.new(solr_field, []) unless data
20
+
21
+ data.each do |parent|
22
+ item = BlacklightFacetExtras::Pivot::FacetItem.new(parent['value'], parent['count'], :field => solr_field)
23
+
24
+ item.facets ||= []
25
+ parent['pivot'].each do |child|
26
+ label = child['value'].split(" / ").last
27
+
28
+
29
+ item.facets << BlacklightFacetExtras::Pivot::FacetItem.new(child['value'], child['count'], :display_label => label, :field => child['field'], :parent => item)
30
+ end
31
+
32
+
33
+ arr << item
34
+ end
35
+
36
+ RSolr::Ext::Response::Facets::FacetField.new(solr_field, arr)
37
+ end
38
+
39
+ def render_facet_value(facet_solr_field, item, options ={})
40
+ if item.is_a? BlacklightFacetExtras::Pivot::FacetItem
41
+ p = add_facet_params_and_redirect(item.field || facet_solr_field, item.value)
42
+
43
+ if item.parent
44
+ p[:f][item.parent.field] ||= []
45
+ p[:f][item.parent.field].push(item.parent.value)
46
+ end
47
+
48
+ (link_to_unless(options[:suppress_link], item.display_label || item.value , p, :class=>"facet_select label") + " " + render_facet_count(item.hits)).html_safe
49
+ else
50
+ super(facet_solr_field, item, options ={})
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ module BlacklightFacetExtras::Pivot
2
+ autoload :ControllerExtension, 'blacklight_facet_extras/pivot/controller_extension'
3
+ autoload :ViewHelperExtension, 'blacklight_facet_extras/pivot/view_helper_extension'
4
+ class FacetItem < RSolr::Ext::Response::Facets::FacetItem
5
+ attr_accessor :display_label
6
+ attr_accessor :facets
7
+ attr_accessor :field
8
+ attr_accessor :parent
9
+
10
+ def initialize value, hits, opts = {}
11
+ super(value, hits)
12
+ @display_label = opts[:display_label]
13
+ @field = opts[:field]
14
+ @parent = opts[:parent]
15
+ @facets = opts[:facets]
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,41 @@
1
+
2
+ # Meant to be applied on top of a controller that implements
3
+ # Blacklight::SolrHelper. Will inject query limiting behaviors
4
+ # to solr parameters creation.
5
+ module BlacklightFacetExtras::Query::ControllerExtension
6
+ def self.included(some_class)
7
+ some_class.send :include,BlacklightFacetExtras::ControllerExtension
8
+ some_class.solr_search_params_logic << :add_query_facets_to_solr
9
+ some_class.helper_method :facet_query_config
10
+ some_class.helper BlacklightFacetExtras::Query::ViewHelperExtension
11
+ end
12
+ def add_query_facets_to_solr(solr_parameters, user_parameters)
13
+ blacklight_query_config.each do |k, config|
14
+ solr_parameters[:fq].select { |x| x.starts_with?("{!raw f=#{k}}") }.each do |x|
15
+ v = solr_parameters[:fq].delete x
16
+ value = v.gsub("{!raw f=#{k}}", "")
17
+ value = config[value] if config[value]
18
+ solr_parameters[:fq] << value
19
+ end if solr_parameters[:fq]
20
+
21
+ solr_parameters[:"facet.field"].select { |x| x == k }.each do |x|
22
+ solr_parameters[:"facet.field"].delete x
23
+ end if solr_parameters[:"facet.field"]
24
+
25
+ solr_parameters[:"facet.query"] ||= []
26
+ config.each do |label, query|
27
+ solr_parameters[:"facet.query"] << query
28
+ end
29
+ end
30
+ end
31
+
32
+ def facet_query_config(solr_field)
33
+ config = blacklight_query_config[solr_field] || false
34
+ config = {} if config == true
35
+ config
36
+ end
37
+
38
+ def blacklight_query_config
39
+ Blacklight.config[:facet][:query] || {}
40
+ end
41
+ end
@@ -0,0 +1,24 @@
1
+ module BlacklightFacetExtras::Query::ViewHelperExtension
2
+
3
+ def render_facet_limit(solr_field)
4
+ config = facet_query_config(solr_field)
5
+ if ( config )
6
+ render(:partial => "catalog/_facet_partials/query", :locals=> {:solr_field => solr_field })
7
+ else
8
+ super(solr_field)
9
+ end
10
+ end
11
+
12
+ def solr_query_to_a(solr_field)
13
+ config = facet_query_config(solr_field)
14
+ return RSolr::Ext::Response::Facets::FacetField.new(solr_field, []) unless config and @response and @response["facet_counts"] and @response["facet_counts"]["facet_queries"] and @response["facet_counts"]["facet_queries"]
15
+
16
+ arr = []
17
+ config.each do |display_label, query|
18
+ next unless @response["facet_counts"]["facet_queries"][query] and @response["facet_counts"]["facet_queries"][query] > 0 and @response["facet_counts"]["facet_queries"][query] < @response.total
19
+ arr << RSolr::Ext::Response::Facets::FacetItem.new(display_label,@response["facet_counts"]["facet_queries"][query])
20
+ end
21
+
22
+ RSolr::Ext::Response::Facets::FacetField.new(solr_field, arr)
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module BlacklightFacetExtras::Query
2
+ autoload :ControllerExtension, 'blacklight_facet_extras/query/controller_extension'
3
+ autoload :ViewHelperExtension, 'blacklight_facet_extras/query/view_helper_extension'
4
+ end
@@ -0,0 +1,43 @@
1
+
2
+ # Meant to be applied on top of a controller that implements
3
+ # Blacklight::SolrHelper. Will inject range limiting behaviors
4
+ # to solr parameters creation.
5
+ module BlacklightFacetExtras::Range::ControllerExtension
6
+ def self.included(some_class)
7
+ some_class.send :include,BlacklightFacetExtras::ControllerExtension
8
+ some_class.solr_search_params_logic << :add_range_facets_to_solr
9
+ some_class.helper_method :facet_range_config
10
+ some_class.helper BlacklightFacetExtras::Range::ViewHelperExtension
11
+ end
12
+ def add_range_facets_to_solr(solr_parameters, user_parameters)
13
+ solr_parameters['facet.range.other'] = 'all'
14
+ blacklight_range_config.each do |k, config|
15
+ solr_parameters['facet.range'] = k
16
+
17
+ solr_parameters[:fq] ||= []
18
+ fq = solr_parameters[:fq].select { |x| x.starts_with?("{!raw f=#{k}}") and x =~ /\[.* TO .*\]/ }.map { |x| solr_parameters[:fq].delete(x) }.map { |x| x.gsub("{!raw f=#{k}}", "") }.map { |x| x.scan(/\[(.*) TO (.*)\]/).first }
19
+
20
+ range_start = fq.map { |x| x.first }.max
21
+ range_end = fq.map { |x| x.last }.min
22
+ range_gap = ((range_end.to_i - range_start.to_i) / (facet_limit_for(k) || 10)) if range_start and range_end
23
+
24
+
25
+ solr_parameters["f.#{k}.facet.range.start"] = range_start || config[:start]
26
+ solr_parameters["f.#{k}.facet.range.end"] = range_end || config[:end]
27
+ solr_parameters["f.#{k}.facet.range.gap"] = range_gap || config[:gap]
28
+ solr_parameters["f.#{k}.facet.mincount"] = config[:mincount] if config[:mincount]
29
+
30
+ solr_parameters[:fq] << "#{k}:[#{range_start} TO #{range_end}]" if range_start and range_end
31
+ end
32
+ end
33
+
34
+ def facet_range_config(solr_field)
35
+ config = blacklight_range_config[solr_field] || false
36
+ config = {} if config == true
37
+ config
38
+ end
39
+
40
+ def blacklight_range_config
41
+ Blacklight.config[:facet][:rangex] || {}
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ module BlacklightFacetExtras::Range::ViewHelperExtension
2
+
3
+ def render_facet_limit(solr_field)
4
+ config = facet_range_config(solr_field)
5
+ if ( config )
6
+ render(:partial => "catalog/_facet_partials/range", :locals=> {:solr_field => solr_field })
7
+ else
8
+ super(solr_field)
9
+ end
10
+ end
11
+ def solr_range_to_a(solr_field)
12
+ config = facet_range_config(solr_field)
13
+ return RSolr::Ext::Response::Facets::FacetField.new(solr_field,[]) unless config and @response and @response["facet_counts"] and @response["facet_counts"]["facet_ranges"] and @response["facet_counts"]["facet_ranges"][solr_field]
14
+
15
+ data = @response["facet_counts"]["facet_ranges"][solr_field]
16
+
17
+ arr = []
18
+
19
+ arr << BlacklightFacetExtras::Range::FacetItem.new("before", data[:before], :from => '*', :to => data[:start]) if data[:before] > 0
20
+
21
+ last = 0
22
+ range = data[:counts].each_slice(2).map { |value, hits| BlacklightFacetExtras::Range::FacetItem.new(value,hits) }
23
+
24
+ if range.length > 1
25
+
26
+ range.each_cons(2) do |item, peek|
27
+ item.from = item.value
28
+ item.to = peek.value
29
+ item.display_label = "#{item.from} - #{item.to}"
30
+ arr << item
31
+ end
32
+
33
+ arr << range.last.tap { |x| x.from = x.value; x.to = data[:end]; x.display_label = "#{x.from} - #{x.to}" }
34
+ end
35
+
36
+ arr << BlacklightFacetExtras::Range::FacetItem.new("after", data[:after], :from => data[:end], :to => '*') if data[:after] > 0
37
+ RSolr::Ext::Response::Facets::FacetField.new(solr_field, arr)
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ module BlacklightFacetExtras::Range
2
+ autoload :ControllerExtension, 'blacklight_facet_extras/range/controller_extension'
3
+ autoload :ViewHelperExtension, 'blacklight_facet_extras/range/view_helper_extension'
4
+ class FacetItem < BlacklightFacetExtras::FacetItem
5
+ attr_accessor :from, :to
6
+
7
+ def initialize value, hits, opts = {}
8
+ super(value, hits, opts = {})
9
+ @from = opts[:from]
10
+ @to = opts[:to]
11
+ end
12
+
13
+ def value
14
+ return"[#{self.from} TO #{self.to}]" if self.from and self.to
15
+ super
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,3 @@
1
+ module BlacklightFacetExtras::RouteSets
2
+
3
+ end
@@ -0,0 +1,41 @@
1
+
2
+ # Meant to be applied on top of a controller that implements
3
+ # Blacklight::SolrHelper. Will inject tag limiting behaviors
4
+ # to solr parameters creation.
5
+ module BlacklightFacetExtras::Tag::ControllerExtension
6
+ def self.included(some_class)
7
+ some_class.send :include,BlacklightFacetExtras::ControllerExtension
8
+ some_class.solr_search_params_logic << :add_tag_facets_to_solr
9
+ some_class.helper_method :facet_tag_config
10
+ some_class.helper BlacklightFacetExtras::Tag::ViewHelperExtension
11
+ end
12
+ def add_tag_facets_to_solr(solr_parameters, user_parameters)
13
+ blacklight_tag_config.each do |k, config|
14
+
15
+
16
+ values = []
17
+ if solr_parameters[:fq]
18
+ solr_parameters[:fq].select { |x| x.starts_with?("{!raw f=#{k}}") }.each do |x|
19
+ values << solr_parameters[:fq].delete(x)
20
+ end
21
+ solr_parameters[:fq] << "{!tag=#{config[:ex]}} #{values.map { |x| "_query_:\"#{x}\""}.join(" OR ") }" unless values.empty?
22
+ end
23
+
24
+ solr_parameters[:"facet.field"].each_with_index.select { |value, index| value == k }.each do |value, index|
25
+ solr_parameters[:"facet.field"][index] = "{!ex=#{config[:ex]}}#{value}"
26
+ end if solr_parameters[:"facet.field"]
27
+ end
28
+
29
+ solr_parameters
30
+ end
31
+
32
+ def facet_tag_config(solr_field)
33
+ config = blacklight_tag_config[solr_field] || false
34
+ config = {} if config == true
35
+ config
36
+ end
37
+
38
+ def blacklight_tag_config
39
+ Blacklight.config[:facet][:tag] || {}
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module BlacklightFacetExtras::Tag::ViewHelperExtension
2
+
3
+ end
@@ -0,0 +1,4 @@
1
+ module BlacklightFacetExtras::Tag
2
+ autoload :ControllerExtension, 'blacklight_facet_extras/tag/controller_extension'
3
+ autoload :ViewHelperExtension, 'blacklight_facet_extras/tag/view_helper_extension'
4
+ end
@@ -0,0 +1,10 @@
1
+ module BlacklightFacetExtras
2
+ unless BlacklightFacetExtras.const_defined? :VERSION
3
+ def self.version
4
+ @version ||= File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION')).chomp
5
+ end
6
+
7
+ VERSION = self.version
8
+ end
9
+ end
10
+
@@ -0,0 +1,14 @@
1
+ module BlacklightFacetExtras::ViewHelperExtension
2
+ def render_facet_value(facet_solr_field, item, options ={})
3
+ if item.respond_to? :display_label
4
+ (link_to_unless(options[:suppress_link], item.display_label || item.value , add_facet_params_and_redirect(facet_solr_field, item.value), :class=>"facet_select label") + " " + render_facet_count(item.hits)).html_safe
5
+ else
6
+ super(facet_solr_field, item, options ={})
7
+ end
8
+ end
9
+
10
+ def facet_values_for(solr_field)
11
+ @response.facets.detect {|f| f.name == solr_field}
12
+ end
13
+ end
14
+
@@ -0,0 +1,34 @@
1
+ # BlacklightFacetExtras
2
+
3
+ module BlacklightFacetExtras
4
+ autoload :ControllerExtension, 'blacklight_facet_extras/controller_extension'
5
+ autoload :ViewHelperExtension, 'blacklight_facet_extras/view_helper_extension'
6
+ autoload :RouteSets, 'blacklight_facet_extras/route_sets'
7
+
8
+ autoload :FacetItem, 'blacklight_facet_extras/facet_item'
9
+ autoload :Filter, 'blacklight_facet_extras/filter'
10
+ autoload :Hierarchy, 'blacklight_facet_extras/hierarchy'
11
+ autoload :Pivot, 'blacklight_facet_extras/pivot'
12
+ autoload :Range, 'blacklight_facet_extras/range'
13
+ autoload :Query, 'blacklight_facet_extras/query'
14
+ autoload :Tag, 'blacklight_facet_extras/tag'
15
+
16
+ require 'blacklight_facet_extras/version'
17
+ require 'blacklight_facet_extras/engine'
18
+
19
+ @omit_inject = {}
20
+ def self.omit_inject=(value)
21
+ value = Hash.new(true) if value == true
22
+ @omit_inject = value
23
+ end
24
+ def self.omit_inject ; @omit_inject ; end
25
+
26
+ def self.inject!
27
+ end
28
+
29
+ # Add element to array only if it's not already there
30
+ def self.safe_arr_add(array, element)
31
+ array << element unless array.include?(element)
32
+ end
33
+
34
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blacklight_facet_extras
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1pre1
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Chris Beer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-06 00:00:00.000000000 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: &2153125440 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2153125440
26
+ - !ruby/object:Gem::Dependency
27
+ name: blacklight
28
+ requirement: &2153124860 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2153124860
37
+ description:
38
+ email:
39
+ - chris_beer@wgbh.org
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - README.rdoc
45
+ - Rakefile
46
+ - VERSION
47
+ - app/views/catalog/_facet_limit.html.erb
48
+ - app/views/catalog/_facet_partials/_pivot.html.erb
49
+ - app/views/catalog/_facet_partials/_query.html.erb
50
+ - app/views/catalog/_facet_partials/_range.html.erb
51
+ - app/views/catalog/_hierarchical_constraints_element.html.erb
52
+ - assets/javascripts/blacklight_facet_extras.js
53
+ - assets/stylesheets/blacklight_facet_extras.css
54
+ - blacklight_facet_extras.gemspec
55
+ - lib/blacklight_facet_extras.rb
56
+ - lib/blacklight_facet_extras/controller_extension.rb
57
+ - lib/blacklight_facet_extras/engine.rb
58
+ - lib/blacklight_facet_extras/facet_item.rb
59
+ - lib/blacklight_facet_extras/filter.rb
60
+ - lib/blacklight_facet_extras/filter/controller_extension.rb
61
+ - lib/blacklight_facet_extras/filter/view_helper_extension.rb
62
+ - lib/blacklight_facet_extras/hierarchy.rb
63
+ - lib/blacklight_facet_extras/hierarchy/controller_extension.rb
64
+ - lib/blacklight_facet_extras/hierarchy/view_helper_extension.rb
65
+ - lib/blacklight_facet_extras/pivot.rb
66
+ - lib/blacklight_facet_extras/pivot/controller_extension.rb
67
+ - lib/blacklight_facet_extras/pivot/view_helper_extension.rb
68
+ - lib/blacklight_facet_extras/query.rb
69
+ - lib/blacklight_facet_extras/query/controller_extension.rb
70
+ - lib/blacklight_facet_extras/query/view_helper_extension.rb
71
+ - lib/blacklight_facet_extras/range.rb
72
+ - lib/blacklight_facet_extras/range/controller_extension.rb
73
+ - lib/blacklight_facet_extras/range/view_helper_extension.rb
74
+ - lib/blacklight_facet_extras/route_sets.rb
75
+ - lib/blacklight_facet_extras/tag.rb
76
+ - lib/blacklight_facet_extras/tag/controller_extension.rb
77
+ - lib/blacklight_facet_extras/tag/view_helper_extension.rb
78
+ - lib/blacklight_facet_extras/version.rb
79
+ - lib/blacklight_facet_extras/view_helper_extension.rb
80
+ has_rdoc: true
81
+ homepage: http://projectblacklight.org/
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>'
97
+ - !ruby/object:Gem::Version
98
+ version: 1.3.1
99
+ requirements: []
100
+ rubyforge_project: blacklight
101
+ rubygems_version: 1.6.2
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Blacklight facet extras plugin
105
+ test_files: []