Created
June 20, 2012 12:49
-
-
Save danhorst/2959739 to your computer and use it in GitHub Desktop.
Example Rails model that is indexed into Solr with Blacklight
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
class Microfilm < ActiveRecord::Base | |
belongs_to :library | |
belongs_to :location | |
belongs_to :collection | |
after_save :update_solr unless ENV['DO_NOT_INDEX'] | |
after_destroy :remove_from_solr | |
scope :with_shelf_mark, lambda { |*shelf_mark| | |
where("#{quoted_table_name}.`shelf_mark` LIKE ?", "%#{shelf_mark.flatten.first}%") unless shelf_mark.flatten.first.blank? | |
} | |
def solr_id | |
"microfilm-#{id}" | |
end | |
def as_solr | |
{ | |
:id => solr_id, | |
:title_display => solr_id, | |
:format => self.class.name, | |
:library_facet => library.name, | |
:city_facet => library.city_name, | |
:shelf_mark_display => shelf_mark, | |
:mss_name_display => mss_name, | |
:mss_note_display => mss_note, | |
:collection_facet => collection.name, | |
:reel_display => reel, | |
:hesburgh_location_display => location.name | |
}.reject{|key, value| value.blank?} | |
end | |
def to_solr | |
Blacklight.solr.add as_solr | |
end | |
def update_solr | |
to_solr | |
Blacklight.solr.commit | |
end | |
private | |
def remove_from_solr | |
Blacklight.solr.delete_by_id solr_id | |
Blacklight.solr.commit | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment