Skip to content

Commit

Permalink
Allow defining attributes on SolrDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Apr 3, 2017
1 parent 700f176 commit 509c0ac
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
16 changes: 16 additions & 0 deletions app/models/concerns/blacklight/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,21 @@ module ClassMethods
def unique_key
@unique_key ||= 'id'
end

# Define an attribute reader on a document model
# @Example
# class SolrDocument
# include Blacklight::Solr::Document
# attribute :title, Blacklight::Types::String, 'title_tesim'
# end
#
# doc = SolrDocument.new(title_tesim: ["One flew over the cuckoo's nest"])
# doc.title
# #=> "One flew over the cuckoo's nest"
def attribute(name, type, field)
define_method name do
type.coerce(self[field])
end
end
end
end
28 changes: 28 additions & 0 deletions app/values/blacklight/types.rb
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
37 changes: 30 additions & 7 deletions spec/models/solr_document_spec.rb
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

0 comments on commit 509c0ac

Please sign in to comment.