Skip to content

Instantly share code, notes, and snippets.

@danhorst
Created June 20, 2012 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danhorst/2959739 to your computer and use it in GitHub Desktop.
Save danhorst/2959739 to your computer and use it in GitHub Desktop.
Example Rails model that is indexed into Solr with Blacklight
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