Skip to content

Commit

Permalink
Add database-backed repository
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Mar 25, 2015
1 parent 4bf8e0a commit 0642b03
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/models/document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Document < ActiveRecord::Base
include Blacklight::Document

def to_partial_path
'catalog/document'
end

def key? k
attributes.has_key? k.to_s
end

def _source
self
end
end
48 changes: 48 additions & 0 deletions db/migrate/2015_create_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- encoding : utf-8 -*-
class CreateDocument < ActiveRecord::Migration
def self.up
create_table :documents do |t|

t.string :lc_1letter_facet
t.string :author_t
t.string :marc_display
t.string :published_display
t.string :author_display
t.string :lc_callnum_display
t.string :title_t
t.string :pub_date
t.string :pub_date_sort
t.string :format
t.string :material_type_display
t.string :lc_b4cutter_facet
t.string :title_display
t.string :title_sort
t.string :author_sort
t.string :title_addl_t
t.string :author_addl_t
t.string :lc_alpha_facet
t.string :language_facet
t.string :subtitle_display
t.string :author_vern_display
t.string :subject_addl_t
t.string :subject_era_facet
t.string :isbn_t
t.string :subject_geo_facet
t.string :subject_topic_facet
t.string :title_series_t
t.string :subtitle_t
t.string :title_vern_display
t.string :published_vern_display
t.string :subtitle_vern_display
t.string :subject_t
t.string :title_added_entry_t
t.string :url_suppl_display

t.timestamps
end
end

def self.down
drop_table :documents
end
end
3 changes: 3 additions & 0 deletions lib/blacklight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Blacklight
autoload :Facet, 'blacklight/facet'

autoload :Elasticsearch, 'blacklight/elasticsearch'
autoload :Ar, 'blacklight/ar'

extend SearchFields
extend Deprecation
Expand Down Expand Up @@ -85,6 +86,8 @@ def self.repository_class
Blacklight::Elasticsearch::Repository
when "solr"
Blacklight::SolrRepository
when "ar"
Blacklight::Ar::Repository
else
raise "No connection adapter found"
end
Expand Down
145 changes: 145 additions & 0 deletions lib/blacklight/ar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
module Blacklight
module Ar
class Repository < Blacklight::AbstractRepository

class SingleDocumentResponse
def initialize doc
@doc = doc
end

def documents
[@doc]
end
end

class FacetResponse
attr_reader :name, :counts

def initialize name, counts
@name = name
@counts = counts
end

def items
counts.map do |key, count|
OpenStruct.new(name: name, value: key, hits: count)
end
end

def sort; end
def offset; end
def limit; end
end

class Aggregations
def initialize response
@response = response
end

def [] key
FacetResponse.new(key, @response.response.group(key).count)
end
end

class SearchResponse
attr_reader :response, :params

include Kaminari::PageScopeMethods
include Kaminari::ConfigurationMethods::ClassMethods

def initialize response, params
@response = response
@params = params
end

def grouped?
false
end

def results
response
end

def total
response.total_count
end

alias_method :documents, :results

delegate :empty?, to: :results

def aggregations
Aggregations.new(self)
end

alias_method :total_count, :total

def start
params.start
end
alias_method :offset_value, :start

def limit_value
params.per
end
alias_method :rows, :limit_value

def sort
params.send(:sort)
end

def spelling
nil
end

end

##
# Find a single document result (by id) using the document configuration
# @param [String] document's unique key value
def find id, params = {}
response = SingleDocumentResponse.new(blacklight_config.document_model.find(id))
response
end

##
# Execute a search query
# @param [Hash] elastic search query parameters
def search params = {}
Rails.logger.info "AR parameters: #{params.inspect}"
SearchResponse.new(params.search(blacklight_config.document_model), params)
end
end

class SearchBuilder < Blacklight::SearchBuilder
self.default_processor_chain = []

attr_accessor :scopes

def initialize *args
super
self.scopes = []
end

def query *args
self
end

def search model
scope = model.page(page).per(per).padding(start % per)

if sort
scope = scope.order(sort)
end

(blacklight_params[:f] || {}).each do |field, values|
scope = scope.where(field => Array(values))
end

scope.all
end


end
end
end
4 changes: 4 additions & 0 deletions lib/railties/blacklight.rake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace :blacklight do
docs = YAML::load(File.open(args[:file]))

case Blacklight.default_index
when Blacklight::Ar::Repository
docs.each do |h|
Document.create!(h.except('timestamp'))
end
when Blacklight::Elasticsearch::Repository
ElasticsearchDocument.create_index! force: true
docs.each do |h|
Expand Down

0 comments on commit 0642b03

Please sign in to comment.