Skip to content

Commit

Permalink
catalog#index should attempt to render document export formats if non…
Browse files Browse the repository at this point in the history
…e of the explicit formats match
  • Loading branch information
cbeer committed Apr 8, 2014
1 parent 5b7d1ce commit ed5490b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lib/blacklight/catalog.rb
Expand Up @@ -21,7 +21,6 @@ module Blacklight::Catalog

# get search results from the solr index
def index

(@response, @document_list) = get_search_results

respond_to do |format|
Expand All @@ -33,6 +32,7 @@ def index
end

additional_response_formats(format)
document_export_formats(format)
end
end

Expand Down Expand Up @@ -170,6 +170,8 @@ def has_search_parameters?
# non-routable methods ->
#

##
# Render additional response formats, as provided by the blacklight configuration
def additional_response_formats format
blacklight_config.index.respond_to.each do |key, config|
format.send key do
Expand All @@ -189,6 +191,32 @@ def additional_response_formats format
end
end

##
# Try to render a response from the document export formats available
def document_export_formats format
format.any do
format_name = params.fetch(:format, '').to_sym

if @response.export_formats.include? format_name
render_document_export_format format_name
else
raise ActionController::UnknownFormat.new
end
end
end

##
# Render the document export formats for a response
# First, try to render an appropriate template (e.g. index.endnote.erb)
# If that fails, just concatenate the document export responses with a newline.
def render_document_export_format format_name
begin
render
rescue ActionView::MissingTemplate
render text: @response.documents.map { |x| x.export_as(format_name) }.join("\n"), layout: false
end
end

# override this method to change the JSON response from #index
def render_search_results_as_json
{response: {docs: @document_list, facets: search_facets_as_json, pages: pagination_info(@response)}}
Expand Down
4 changes: 4 additions & 0 deletions lib/blacklight/solr_response.rb
Expand Up @@ -76,6 +76,10 @@ def grouped?
self.has_key? "grouped"
end

def export_formats
documents.map { |x| x.export_formats.keys }.flatten.uniq
end

private

def force_to_utf8(value)
Expand Down
45 changes: 45 additions & 0 deletions spec/features/search_formats_spec.rb
@@ -0,0 +1,45 @@
# -*- encoding : utf-8 -*-

require 'spec_helper'

describe "Search Formats" do
before do
# Get all the fields from Solr, so the #index documents have the MARC field (to trigger appropriate
# export formats)
CatalogController.blacklight_config.default_solr_params[:fl] = '*'
end

it "should have an RSS XML response" do
visit "/catalog.rss?q="
expect(page).to have_content "Blacklight Search Results"
doc = Nokogiri::XML(page.body)
expect(doc.xpath("//item")).to have(10).items
end

it "should have an ATOM XML response" do
visit "/catalog.atom?q="
expect(page).to have_content "Blacklight Search Results"
doc = Nokogiri::XML(page.body)
expect(doc.xpath("//atom:entry", atom: "http://www.w3.org/2005/Atom")).to have(10).entries
end

it "should return new-line separated RIS records" do
visit "/catalog.endnote?q="
expect(page.status_code).to eq 200
rmt_regex = /^%0/
expect(page).to have_content "%A Bstan-ʼdzin-rgya-mtsho,"
expect(page).to have_content "%@ 9789573908678"
expect(page.body).to match rmt_regex
expect(page.body.scan(rmt_regex).length).to eq 10
end

it "should return new-line separated MARC records" do
visit "/catalog.refworks_marc_txt?q="
expect(page.status_code).to eq 200
rmt_regex = /LEADER .+\n(\d\d\d .. .+\n)+/
expect(page).to have_content "LEADER 01221cam a22002534a 4500001"
expect(page).to have_content "LEADER 01127cam a22002895a 4500001"
expect(page.body).to match rmt_regex
expect(page.body.scan(rmt_regex).length).to eq 10
end
end
8 changes: 8 additions & 0 deletions spec/lib/blacklight/solr_response_spec.rb
Expand Up @@ -183,6 +183,14 @@ def create_response
r.stub(:total => 0)
expect(r).to be_empty
end

describe "#export_formats" do
it "should collect the unique export formats for the current response" do
r = Blacklight::SolrResponse.new({}, {})
r.stub(documents: [double(:export_formats => { a: 1, b: 2}), double(:export_formats => { b: 1, c: 2})])
expect(r.export_formats).to include :a, :b
end
end

def mock_query_response
%({'responseHeader'=>{'status'=>0,'QTime'=>5,'params'=>{'facet.limit'=>'10','wt'=>'ruby','rows'=>'11','facet'=>'true','facet.field'=>['cat','manu'],'echoParams'=>'EXPLICIT','q'=>'*:*','facet.sort'=>'true'}},'response'=>{'numFound'=>26,'start'=>0,'docs'=>[{'id'=>'SP2514N','inStock'=>true,'manu'=>'Samsung Electronics Co. Ltd.','name'=>'Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133','popularity'=>6,'price'=>92.0,'sku'=>'SP2514N','timestamp'=>'2009-03-20T14:42:49.795Z','cat'=>['electronics','hard drive'],'spell'=>['Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133'],'features'=>['7200RPM, 8MB cache, IDE Ultra ATA-133','NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor']},{'id'=>'6H500F0','inStock'=>true,'manu'=>'Maxtor Corp.','name'=>'Maxtor DiamondMax 11 - hard drive - 500 GB - SATA-300','popularity'=>6,'price'=>350.0,'sku'=>'6H500F0','timestamp'=>'2009-03-20T14:42:49.877Z','cat'=>['electronics','hard drive'],'spell'=>['Maxtor DiamondMax 11 - hard drive - 500 GB - SATA-300'],'features'=>['SATA 3.0Gb/s, NCQ','8.5ms seek','16MB cache']},{'id'=>'F8V7067-APL-KIT','inStock'=>false,'manu'=>'Belkin','name'=>'Belkin Mobile Power Cord for iPod w/ Dock','popularity'=>1,'price'=>19.95,'sku'=>'F8V7067-APL-KIT','timestamp'=>'2009-03-20T14:42:49.937Z','weight'=>4.0,'cat'=>['electronics','connector'],'spell'=>['Belkin Mobile Power Cord for iPod w/ Dock'],'features'=>['car power adapter, white']},{'id'=>'IW-02','inStock'=>false,'manu'=>'Belkin','name'=>'iPod & iPod Mini USB 2.0 Cable','popularity'=>1,'price'=>11.5,'sku'=>'IW-02','timestamp'=>'2009-03-20T14:42:49.944Z','weight'=>2.0,'cat'=>['electronics','connector'],'spell'=>['iPod & iPod Mini USB 2.0 Cable'],'features'=>['car power adapter for iPod, white']},{'id'=>'MA147LL/A','inStock'=>true,'includes'=>'earbud headphones, USB cable','manu'=>'Apple Computer Inc.','name'=>'Apple 60 GB iPod with Video Playback Black','popularity'=>10,'price'=>399.0,'sku'=>'MA147LL/A','timestamp'=>'2009-03-20T14:42:49.962Z','weight'=>5.5,'cat'=>['electronics','music'],'spell'=>['Apple 60 GB iPod with Video Playback Black'],'features'=>['iTunes, Podcasts, Audiobooks','Stores up to 15,000 songs, 25,000 photos, or 150 hours of video','2.5-inch, 320x240 color TFT LCD display with LED backlight','Up to 20 hours of battery life','Plays AAC, MP3, WAV, AIFF, Audible, Apple Lossless, H.264 video','Notes, Calendar, Phone book, Hold button, Date display, Photo wallet, Built-in games, JPEG photo playback, Upgradeable firmware, USB 2.0 compatibility, Playback speed control, Rechargeable capability, Battery level indication']},{'id'=>'TWINX2048-3200PRO','inStock'=>true,'manu'=>'Corsair Microsystems Inc.','name'=>'CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail','popularity'=>5,'price'=>185.0,'sku'=>'TWINX2048-3200PRO','timestamp'=>'2009-03-20T14:42:49.99Z','cat'=>['electronics','memory'],'spell'=>['CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail'],'features'=>['CAS latency 2, 2-3-3-6 timing, 2.75v, unbuffered, heat-spreader']},{'id'=>'VS1GB400C3','inStock'=>true,'manu'=>'Corsair Microsystems Inc.','name'=>'CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail','popularity'=>7,'price'=>74.99,'sku'=>'VS1GB400C3','timestamp'=>'2009-03-20T14:42:50Z','cat'=>['electronics','memory'],'spell'=>['CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail']},{'id'=>'VDBDB1A16','inStock'=>true,'manu'=>'A-DATA Technology Inc.','name'=>'A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM','popularity'=>5,'sku'=>'VDBDB1A16','timestamp'=>'2009-03-20T14:42:50.004Z','cat'=>['electronics','memory'],'spell'=>['A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM'],'features'=>['CAS latency 3, 2.7v']},{'id'=>'3007WFP','inStock'=>true,'includes'=>'USB cable','manu'=>'Dell, Inc.','name'=>'Dell Widescreen UltraSharp 3007WFP','popularity'=>6,'price'=>2199.0,'sku'=>'3007WFP','timestamp'=>'2009-03-20T14:42:50.017Z','weight'=>401.6,'cat'=>['electronics','monitor'],'spell'=>['Dell Widescreen UltraSharp 3007WFP'],'features'=>['30" TFT active matrix LCD, 2560 x 1600, .25mm dot pitch, 700:1 contrast']},{'id'=>'VA902B','inStock'=>true,'manu'=>'ViewSonic Corp.','name'=>'ViewSonic VA902B - flat panel display - TFT - 19"','popularity'=>6,'price'=>279.95,'sku'=>'VA902B','timestamp'=>'2009-03-20T14:42:50.034Z','weight'=>190.4,'cat'=>['electronics','monitor'],'spell'=>['ViewSonic VA902B - flat panel display - TFT - 19"'],'features'=>['19" TFT active matrix LCD, 8ms response time, 1280 x 1024 native resolution']},{'id'=>'0579B002','inStock'=>true,'manu'=>'Canon Inc.','name'=>'Canon PIXMA MP500 All-In-One Photo Printer','popularity'=>6,'price'=>179.99,'sku'=>'0579B002','timestamp'=>'2009-03-20T14:42:50.062Z','weight'=>352.0,'cat'=>['electronics','multifunction printer','printer','scanner','copier'],'spell'=>['Canon PIXMA MP500 All-In-One Photo Printer'],'features'=>['Multifunction ink-jet color photo printer','Flatbed scanner, optical scan resolution of 1,200 x 2,400 dpi','2.5" color LCD preview screen','Duplex Copying','Printing speed up to 29ppm black, 19ppm color','Hi-Speed USB','memory card: CompactFlash, Micro Drive, SmartMedia, Memory Stick, Memory Stick Pro, SD Card, and MultiMediaCard']}]},'facet_counts'=>{'facet_queries'=>{},'facet_fields'=>{'cat'=>['electronics',14,'memory',3,'card',2,'connector',2,'drive',2,'graphics',2,'hard',2,'monitor',2,'search',2,'software',2],'manu'=>['inc',8,'apach',2,'belkin',2,'canon',2,'comput',2,'corp',2,'corsair',2,'foundat',2,'microsystem',2,'softwar',2]},'facet_dates'=>{}}})
Expand Down

0 comments on commit ed5490b

Please sign in to comment.