-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow defining attributes on SolrDocument
- Loading branch information
Showing
3 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Blacklight | ||
# These are data types that blacklight can use to coerce values from the index | ||
module Types | ||
class Array | ||
def self.coerce(input) | ||
::Array.wrap(input) | ||
end | ||
end | ||
|
||
class String | ||
def self.coerce(input) | ||
::Array.wrap(input).first | ||
end | ||
end | ||
|
||
class Date | ||
def self.coerce(input) | ||
field = String.coerce(input) | ||
return if field.blank? | ||
begin | ||
::Date.parse(field) | ||
rescue ArgumentError | ||
Rails.logger.info "Unable to parse date: #{field.first.inspect}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
describe SolrDocument do | ||
|
||
before(:each) do | ||
|
||
@solrdoc = SolrDocument.new :id => '00282214', :format => ['Book'], :title_display => 'some-title' | ||
|
||
end | ||
|
||
describe "new" do | ||
it "takes a Hash as the argument" do | ||
expect { SolrDocument.new(:id => 1) }.not_to raise_error | ||
end | ||
end | ||
|
||
describe "access methods" do | ||
|
||
it "has the right value for title_display" do | ||
expect(@solrdoc[:title_display]).not_to be_nil | ||
end | ||
|
||
it "has the right value for format" do | ||
expect(@solrdoc[:format][0]).to eq 'Book' | ||
end | ||
|
||
it "provides the item's solr id" do | ||
expect(@solrdoc.id).to eq '00282214' | ||
end | ||
end | ||
end | ||
|
||
describe '.attribute' do | ||
subject(:title) { document.title } | ||
let(:doc_class) do | ||
Class.new(SolrDocument) do | ||
attribute :title, Blacklight::Types::String, 'title_tesim' | ||
attribute :author, Blacklight::Types::Array, 'author_tesim' | ||
attribute :date, Blacklight::Types::Date, 'date_dtsi' | ||
|
||
end | ||
end | ||
let(:document) do | ||
doc_class.new(id: '123', | ||
title_tesim: ['Good Omens'], | ||
author_tesim: ['Neil Gaiman', 'Terry Pratchett'], | ||
date_dtsi: '1990-01-01T00:00:00Z') | ||
end | ||
it "casts the attributes" do | ||
expect(document.title).to eq 'Good Omens' | ||
expect(document.author).to eq ['Neil Gaiman', 'Terry Pratchett'] | ||
expect(document.date).to eq Date.new(1990) | ||
end | ||
end | ||
end |