Skip to content

Commit

Permalink
Add CrossRef specific sorts and some specs for both PLOS and CrossRef…
Browse files Browse the repository at this point in the history
… sorting. Also fixes the repeated sort parameter in URL, by using the queryString module and changing the sort parameter instead of appending it. Closes #59.
  • Loading branch information
jure committed Oct 12, 2014
1 parent 56b205f commit b3c11ba
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
//= require jquery-ui
//= require jquery.placeholder
//= require jquery.uniform
//= require query-string
//= require script
//= require advancedSearch
4 changes: 3 additions & 1 deletion app/assets/javascripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,9 @@ if (jQuery.fn.uniform) {
jQuery(function(d, $){
$('#search_results_sort_order_select').change(function() {
var sort_param = this.options[this.selectedIndex].value;
window.location.href = window.location.href + "&sort=" + encodeURIComponent(sort_param);
var query = queryString.parse(location.search);
query.sort = sort_param;
location.search = queryString.stringify(query);
});
}(document, jQuery));

Expand Down
6 changes: 2 additions & 4 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ class Report < ActiveRecord::Base
# Sort order is determined by the position in the array. This object must have
# already been saved to the DB before this method is called.
def add_all_dois(dois)

# Since reports can have many DOIs, for performance we do a batch insert.
# Active Record won't do this on its own.
report_dois = dois.map.with_index do |doi, index|
{
doi: doi,
sort_order: index
}
end
self.report_dois.create(report_dois)

# Since reports can have many DOIs, for performance we do a batch insert.
# Active Record won't do this on its own.
# TEMP DISABLE, PRETTY BAD.
# sql = "INSERT report_dois(doi, report_id, sort_order, created_at, updated_at) VALUES "
# dois.each_with_index {|doi, i| sql << "('#{doi}', #{self.id}, #{i}, NOW(), NOW()), "}
Expand Down
9 changes: 9 additions & 0 deletions app/models/search_crossref.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
class SearchCrossref

SORTS = {
"Relevance" => "",
"Date, newest first" => "published desc",
"Date, oldest first" => "published asc",
}

def initialize(query, opts = {})
@query = query[:everything]
@filter = [query[:filter], "from-pub-date:2013-01-01"].compact.join(",")
@page = query[:current_page] || 1
@rows = query[:rows] || APP_CONFIG["results_per_page"]
@sort, @order = query[:sort].try(:split)
end

def run
Expand All @@ -26,6 +34,7 @@ def request
}
request.merge!({ query: @query }) if @query.present?
request.merge!({ filter: @filter }) if @filter.present?
request.merge!({ sort: @sort, order: @order }) if @sort && @order
request
end

Expand Down
6 changes: 4 additions & 2 deletions app/views/search/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
span.add-select-label Sort by
= select_tag( \
:sort, \
options_for_select(SolrRequest::SORTS.to_a, params[:sort]), \
options_for_select( \
(Search.plos? ? SolrRequest : SearchCrossref)::SORTS.to_a, \
params[:sort] \
), \
class: "styled", \
id: "search_results_sort_order_select" \
)

p.select-articles-message.text-light-normal.invisible
span#select-articles-message-text
span#select-all-articles-message-text
Expand Down
65 changes: 65 additions & 0 deletions spec/cassettes/sorting_results/sorts_CrossRef.yml

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions spec/features/sorting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'

describe "sorting results", type: :feature, vcr: true do
if Search.plos?
it "sorts PLOS", js: true do
visit "/"
fill_in "everything", with: "cancer"
click_button "Search"
page.should have_select("sort", options: SolrRequest::SORTS.keys)
end
elsif Search.crossref?
it "sorts CrossRef", js: true do
visit "/"
fill_in "everything", with: "cancer"
click_button "Search"
page.should have_select("sort", options: SearchCrossref::SORTS.keys)
end
end
end
6 changes: 6 additions & 0 deletions spec/models/search_crossref_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
search = SearchCrossref.new(everything: "biology", current_page: 2)
search.offset.should eq(25)
end

it "handles :sort parameter" do
search = SearchCrossref.new(sort: "published desc")
search.request[:sort].should eq("published")
search.request[:order].should eq("desc")
end
end
66 changes: 66 additions & 0 deletions vendor/assets/javascripts/query-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*!
query-string
Parse and stringify URL query strings
https://github.com/sindresorhus/query-string
by Sindre Sorhus
MIT License
*/
(function () {
'use strict';
var queryString = {};

queryString.parse = function (str) {
if (typeof str !== 'string') {
return {};
}

str = str.trim().replace(/^(\?|#)/, '');

if (!str) {
return {};
}

return str.trim().split('&').reduce(function (ret, param) {
var parts = param.replace(/\+/g, ' ').split('=');
var key = parts[0];
var val = parts[1];

key = decodeURIComponent(key);
// missing `=` should be `null`:
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
val = val === undefined ? null : decodeURIComponent(val);

if (!ret.hasOwnProperty(key)) {
ret[key] = val;
} else if (Array.isArray(ret[key])) {
ret[key].push(val);
} else {
ret[key] = [ret[key], val];
}

return ret;
}, {});
};

queryString.stringify = function (obj) {
return obj ? Object.keys(obj).map(function (key) {
var val = obj[key];

if (Array.isArray(val)) {
return val.map(function (val2) {
return encodeURIComponent(key) + '=' + encodeURIComponent(val2);
}).join('&');
}

return encodeURIComponent(key) + '=' + encodeURIComponent(val);
}).join('&') : '';
};

if (typeof define === 'function' && define.amd) {
define(function() { return queryString; });
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = queryString;
} else {
window.queryString = queryString;
}
})();

0 comments on commit b3c11ba

Please sign in to comment.