Interface taken from Sunspot::Rails::StubSessionProxy::PaginatesCollection
of
sunspot-rails.
It adds the option to inject records to mimic actual search results (and yes, thus making fun of the "Null" gem name).
Add this line to your application's Gemfile:
gem 'sunspot-null_result'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sunspot-null_result
RSpec examples:
RSpec.describe 'stubbed Solr result' do
let(:search) { Sunspot::NullResult.new(records) }
before { allow(MyModel).to receive(:search).and_return(search) }
context 'with no records' do
let(:records) { [] }
# …
it 'supports grouping' do
# …
expect(search.group(:my_attribute)).to eql []
end
end
context 'with some records' do
let(:records) { [MyModel.new, MyModel.new] }
# …
it 'supports grouping' do
# …
expect(search.group(:my_attribute)).not_to be_empty
end
end
end
Rescue case for unavailable Solr server, e.g. Websolr having issues.
class ApplicationController < ActionController::Base
rescue_from RSolr::Error::Http do |exception|
# Handle exception so that a complete server shutdown
# doesn't go unnoticed. Your app may still work using:
failover_records = MyModel.last(10)
Sunspot::NullResult.new(failover_records)
end
# …
end
- Support kaminari-paginated enumerables
- Build on newer MRI version
- Add Sunspot::NullResult#total and #facet
- Use frozen_string_literal magic comment for MRI 2.3.x
- Minor fix for accessing previous result page
- Extend sunspot groups by useful details, e.g. pagination information
- Support Sunspot grouping
- Allow setting of pagination options and calculate dependent values accordingly
- Initial working release
The gem is available as free and open source software under the terms of the MIT License.