Skip to content

Commit

Permalink
Only delegate necessary methods to the source record
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Sep 27, 2016
1 parent e7d7cb8 commit 6eff23b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 24 deletions.
25 changes: 2 additions & 23 deletions app/models/concerns/blacklight/document.rb
Expand Up @@ -38,28 +38,15 @@ module Blacklight::Document

attr_reader :response, :_source
alias_method :solr_response, :response

delegate :[], :key?, :keys, :to_h, to: :_source

def initialize(source_doc={}, response=nil)
@_source = ActiveSupport::HashWithIndifferentAccess.new(source_doc).freeze
@response = response
apply_extensions
end

# the wrapper method to the @_source object.
# If a method is missing, it gets sent to @_source
# with all of the original params and block
def method_missing(m, *args, &b)
if _source_responds_to?(m)
_source.send(m, *args, &b)
else
super
end
end

def respond_to_missing? *args
_source_responds_to?(*args) || super
end

# Helper method to check if value/multi-values exist for a given key.
# The value can be a string, or a RegExp
# Multiple "values" can be given; only one needs to match.
Expand Down Expand Up @@ -87,8 +74,6 @@ def has?(k, *values)
end
end
alias has_field? has?

delegate :key?, to: :_source
alias has_key? key?

def fetch key, *default
Expand Down Expand Up @@ -132,10 +117,4 @@ def unique_key
@unique_key ||= 'id'
end
end

private

def _source_responds_to? *args
_source && self != _source && _source.respond_to?(*args)
end
end
2 changes: 1 addition & 1 deletion spec/controllers/blacklight/search_helper_spec.rb
Expand Up @@ -80,7 +80,7 @@ def params

expect(Set.new(mash.keys)).to eq Set.new(solr_document.keys)

mash.each_key do |key|
mash.keys.each do |key|
expect(mash[key]).to eq solr_document[key]
end
end
Expand Down

0 comments on commit 6eff23b

Please sign in to comment.