Skip to content

Commit 1515012

Browse files
committed
Extract Blacklight::Document::ActiveModelShim from Blacklight::Document
1 parent 3962847 commit 1515012

File tree

3 files changed

+64
-43
lines changed

3 files changed

+64
-43
lines changed

lib/blacklight/document.rb

+1-43
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# transformation formats.
1414
#
1515
module Blacklight::Document
16+
autoload :ActiveModelShim, 'blacklight/document/active_model_shim'
1617
autoload :SchemaOrg, 'blacklight/document/schema_org'
1718
autoload :DublinCore, 'blacklight/document/dublin_core'
1819
autoload :Email, 'blacklight/document/email'
@@ -42,14 +43,6 @@ def initialize(source_doc={}, response=nil)
4243
apply_extensions
4344
end
4445

45-
def to_model
46-
self
47-
end
48-
49-
def persisted?
50-
true
51-
end
52-
5346
# the wrapper method to the @_source object.
5447
# If a method is missing, it gets sent to @_source
5548
# with all of the original params and block
@@ -65,14 +58,6 @@ def respond_to_missing? *args
6558
_source_responds_to?(*args) || super
6659
end
6760

68-
def [] *args
69-
_source.send :[], *args
70-
end
71-
72-
def _read_attribute(attr)
73-
self[attr]
74-
end
75-
7661
# Helper method to check if value/multi-values exist for a given key.
7762
# The value can be a string, or a RegExp
7863
# Multiple "values" can be given; only one needs to match.
@@ -138,30 +123,10 @@ def first key
138123
Array(self[key]).first
139124
end
140125

141-
def id
142-
self[self.class.unique_key]
143-
end
144-
145-
def to_param
146-
id.to_s
147-
end
148-
149-
def as_json(options = nil)
150-
_source.as_json(options)
151-
end
152-
153126
def to_partial_path
154127
'catalog/document'
155128
end
156129

157-
def destroyed?
158-
false
159-
end
160-
161-
def new_record?
162-
false
163-
end
164-
165130
def has_highlight_field? k
166131
false
167132
end
@@ -186,13 +151,6 @@ def unique_key
186151
@unique_key ||= 'id'
187152
end
188153

189-
def primary_key
190-
unique_key
191-
end
192-
193-
def base_class
194-
self
195-
end
196154
end
197155

198156
private
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'active_model/conversion'
2+
3+
module Blacklight::Document
4+
module ActiveModelShim
5+
extend ActiveSupport::Concern
6+
7+
include ::ActiveModel::Conversion
8+
9+
module ClassMethods
10+
def primary_key
11+
unique_key
12+
end
13+
14+
def base_class
15+
self
16+
end
17+
end
18+
19+
##
20+
# Unique ID for the document
21+
def id
22+
self[self.class.unique_key]
23+
end
24+
25+
##
26+
# accessors for reading attribute
27+
def [] *args
28+
_source.send :[], *args
29+
end
30+
31+
def _read_attribute(attr)
32+
self[attr]
33+
end
34+
35+
def as_json(options = nil)
36+
_source.as_json(options)
37+
end
38+
39+
##
40+
# ActiveRecord::Persistence method stubs to get non-AR objects to
41+
# play nice with e.g. Blacklight's bookmarks
42+
def persisted?
43+
true
44+
end
45+
46+
def destroyed?
47+
false
48+
end
49+
50+
def new_record?
51+
false
52+
end
53+
54+
##
55+
# #to_partial_path is also defined in Blacklight::Document, but
56+
# ActiveModel::Conversion (included above) will overwrite that..
57+
def to_partial_path
58+
'catalog/document'
59+
end
60+
61+
end
62+
end

lib/blacklight/solr/document.rb

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Blacklight::Solr::Document
2626

2727
extend ActiveSupport::Concern
2828
include Blacklight::Document
29+
include Blacklight::Document::ActiveModelShim
2930
include Blacklight::Solr::Document::MoreLikeThis
3031

3132
def has_highlight_field? k

0 commit comments

Comments
 (0)