Skip to content

Commit

Permalink
Use polymorphic relationship for sidecars <=> documents
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 18, 2015
1 parent 3e7e413 commit 1bb1550
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/models/concerns/spotlight/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module SolrDocument
extend ActsAsTaggableOn::Taggable

acts_as_taggable
has_many :sidecars, class_name: 'Spotlight::SolrDocumentSidecar'
has_many :sidecars, class_name: 'Spotlight::SolrDocumentSidecar', as: :document

before_save :save_owned_tags
after_save :reindex
Expand Down
17 changes: 15 additions & 2 deletions app/models/spotlight/solr_document_sidecar.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Spotlight
class SolrDocumentSidecar < ActiveRecord::Base
belongs_to :exhibit
belongs_to :solr_document
belongs_to :document, polymorphic: true
serialize :data, Hash

delegate :has_key?, to: :data

def to_solr
{ blacklight_config.solr_document_model.unique_key.to_sym => solr_document_id, visibility_field => public? }.merge(data_to_solr)
{ document.class.unique_key.to_sym => document.id, visibility_field => public? }.merge(data_to_solr)
end

def private!
Expand All @@ -18,6 +18,19 @@ def public!
update public: true
end

# Roll our own polymorphism because our documents are not AREL-able
def document
document_type.new document_type.unique_key => document_id
end

def document_type
(super.constantize if defined?(super)) || default_document_type
end

def default_document_type
SolrDocument
end

protected

def visibility_field
Expand Down
3 changes: 2 additions & 1 deletion app/serializers/spotlight/exhibit_export_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class PageExportSerializer < Spotlight::ExportSerializer(Spotlight::Page)
class SolrDocumentSerializer < Spotlight::ExportSerializer(Spotlight::SolrDocumentSidecar)
def filter keys
keys = super
keys += [:solr_document_id]
keys += [:document_id]
keys += [:document_type]
end
end

Expand Down
26 changes: 26 additions & 0 deletions db/migrate/20150217111511_add_polymorphic_document_to_sidecars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class AddPolymorphicDocumentToSidecars < ActiveRecord::Migration
def change
add_column :spotlight_solr_document_sidecars, :document_id, :string
add_column :spotlight_solr_document_sidecars, :document_type, :string

remove_column :spotlight_solr_document_sidecars, :solr_document_id

reversible do |dir|
dir.up do
Spotlight::SolrDocumentSidecar.find_each do |e|
e.document = SolrDocument.new(id: e.solr_document_id)
e.save!
end
end

dir.down do
Spotlight::SolrDocumentSidecar.find_each do |e|
e.solr_document_id = e.document_id
e.save!
end
end
end

add_index :bookmarks, [:document_type, :document_id]
end
end
2 changes: 1 addition & 1 deletion spec/models/solr_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

describe "#to_solr" do
before do
Spotlight::SolrDocumentSidecar.create! solr_document: subject, exhibit: exhibit,
Spotlight::SolrDocumentSidecar.create! document: subject, exhibit: exhibit,
data: {'a_tesim' => 1, 'b_tesim' => 2, 'c_tesim' => 3 }
end

Expand Down
4 changes: 2 additions & 2 deletions spec/models/spotlight/resources/csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@

it "should create sidecar files for custom fields" do
expect { subject.save! }.to change { Spotlight::SolrDocumentSidecar.count }.by(2)
row = subject.exhibit.solr_document_sidecars.where(solr_document_id: "1").first
row = subject.exhibit.solr_document_sidecars.where(document_id: "1").first
expect(row.public).to be_truthy
expect(row.data[custom_field.field]).to eq "x"
row = subject.exhibit.solr_document_sidecars.where(solr_document_id: "2").first
row = subject.exhibit.solr_document_sidecars.where(document_id: "2").first
expect(row.public).to be_falsey
expect(row.data[custom_field.field]).to eq "w"
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/spotlight/solr_document_sidecar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:exhibit) { FactoryGirl.create(:exhibit) }
before do
allow(subject).to receive_messages exhibit: exhibit
allow(subject).to receive_messages solr_document_id: 'doc_id'
allow(subject).to receive_messages document: SolrDocument.new(id: 'doc_id')
end

describe "#to_solr" do
Expand Down
4 changes: 2 additions & 2 deletions spec/serializers/spotlight/exhibit_export_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
end

it "should have solr document sidecars" do
source_exhibit.solr_document_sidecars.create! solr_document_id: 1, public: false
source_exhibit.solr_document_sidecars.create! document: SolrDocument.new(id: 1), public: false
expect(subject["solr_document_sidecars_attributes"]).to have_at_least(1).item
expect(subject["solr_document_sidecars_attributes"]).to have(source_exhibit.solr_document_sidecars.count).items

expect(subject["solr_document_sidecars_attributes"].first).to include('solr_document_id', 'public')
expect(subject["solr_document_sidecars_attributes"].first).to include('document_id', 'public')
expect(subject["solr_document_sidecars_attributes"].first).to_not include 'id'
end

Expand Down

0 comments on commit 1bb1550

Please sign in to comment.