-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from projecthydra/field_to_solr_name
Add a way to get the primary solr name for a field
- Loading branch information
Showing
8 changed files
with
131 additions
and
48 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
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,61 @@ | ||
module ActiveFedora | ||
# Represents the mapping between a model attribute and a field in a datastream | ||
class DatastreamAttribute | ||
|
||
attr_accessor :dsid, :field, :klass, :at, :reader, :writer, :multiple | ||
|
||
def initialize(field, dsid, klass, args={}) | ||
self.field = field | ||
self.dsid = dsid | ||
self.klass = klass | ||
self.multiple = args[:multiple].nil? ? false : args[:multiple] | ||
self.at = args[:at] | ||
|
||
initialize_reader! | ||
initialize_writer! | ||
end | ||
|
||
# Gives the primary solr name for a column. If there is more than one indexer on the field definition, it gives the first | ||
def primary_solr_name | ||
if klass.respond_to?(:primary_solr_name) | ||
klass.primary_solr_name(dsid, field) | ||
else | ||
raise IllegalOperation, "the class '#{klass}' doesn't respond to 'primary_solr_name'" | ||
end | ||
end | ||
|
||
private | ||
|
||
def initialize_writer! | ||
this = self | ||
self.writer = lambda do |v| | ||
ds = datastream_for_attribute(this.dsid) | ||
mark_as_changed(this.field) if value_has_changed?(this.field, v) | ||
if ds.kind_of?(ActiveFedora::RDFDatastream) | ||
ds.send("#{this.field}=", v) | ||
else | ||
terminology = this.at || [this.field] | ||
ds.send(:update_indexed_attributes, {terminology => v}) | ||
end | ||
end | ||
end | ||
|
||
def initialize_reader! | ||
this = self | ||
self.reader = lambda do |*opts| | ||
ds = datastream_for_attribute(this.dsid) | ||
if ds.kind_of?(ActiveFedora::RDFDatastream) | ||
ds.send(this.field) | ||
else | ||
terminology = this.at || [this.field] | ||
if terminology.length == 1 && opts.present? | ||
ds.send(terminology.first, *opts) | ||
else | ||
ds.send(:term_values, *terminology) | ||
end | ||
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
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,26 @@ | ||
require 'spec_helper' | ||
|
||
describe "An object with RDF backed attributes" do | ||
|
||
before do | ||
class TestOne < ActiveFedora::Base | ||
class MyMetadata < ActiveFedora::NtriplesRDFDatastream | ||
map_predicates do |map| | ||
map.title(in: RDF::DC) do |index| | ||
index.as :stored_searchable | ||
end | ||
end | ||
end | ||
has_metadata 'descMetadata', type: MyMetadata | ||
has_attributes :title, datastream: 'descMetadata' | ||
end | ||
end | ||
|
||
after do | ||
Object.send(:remove_const, :TestOne) | ||
end | ||
|
||
it "should be able to grab the solr name" do | ||
expect(TestOne.defined_attributes[:title].primary_solr_name).to eq 'desc_metadata__title_tesim' | ||
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