Skip to content

Commit 2682192

Browse files
committed
Merge pull request #1175 from projectblacklight/am-shim
ActiveModel shimming
2 parents bf2c98a + 1515012 commit 2682192

9 files changed

+79
-51
lines changed

app/helpers/blacklight/catalog_helper_behavior.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ def page_entries_info(collection, options = {})
4848
#
4949
# @param [Integer] document index
5050
# @return [Integer]
51-
def document_counter_with_offset idx
51+
def document_counter_with_offset idx, offset = nil
52+
offset ||= @response.start if @response
53+
offset ||= 0
54+
5255
unless render_grouped_response?
53-
idx + 1 + @response.params[:start].to_i
56+
idx + 1 + offset
5457
end
5558
end
5659

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

lib/blacklight/solr_response.rb

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def params
3434
header['params'] || request_params
3535
end
3636

37+
def start
38+
params[:start].to_i
39+
end
40+
3741
def rows
3842
params[:rows].to_i
3943
end

spec/helpers/blacklight_helper_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def mock_document_app_helper_url *args
462462

463463
before do
464464
allow(helper).to receive(:blacklight_config).and_return(CatalogController.blacklight_config)
465-
assign(:response, double("SolrResponse", grouped?: false, params: {}))
465+
assign(:response, double("SolrResponse", grouped?: false, start: 0))
466466
allow(helper).to receive(:link_to_document).and_return('<a/>')
467467
allow(helper).to receive(:render_index_doc_actions).and_return('<div/>')
468468
end

spec/helpers/catalog_helper_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,19 @@ def render_grouped_response?
247247

248248
describe "document_counter_with_offset" do
249249
it "should render the document index with the appropriate offset" do
250-
assign(:response, double(:params => { :start => 0 }, :grouped? => false))
250+
assign(:response, double(start: 0, grouped?: false))
251251
expect(helper.document_counter_with_offset(0)).to be(1)
252252
expect(helper.document_counter_with_offset(1)).to be(2)
253253
end
254254

255255
it "should render the document index with the appropriate offset" do
256-
assign(:response, double(:params => { :start => 10 }, :grouped? => false))
256+
assign(:response, double(start: 10, grouped?: false))
257257
expect(helper.document_counter_with_offset(0)).to be(11)
258258
expect(helper.document_counter_with_offset(1)).to be(12)
259259
end
260260

261261
it "should not provide a counter for grouped responses" do
262-
assign(:response, double(:params => { :start => 10 }, :grouped? => true))
262+
assign(:response, double(start: 10, grouped?: true))
263263
expect(helper.document_counter_with_offset(0)).to be_nil
264264
end
265265
end

spec/views/catalog/_index_header_default.html.erb_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
end
1111

1212
before do
13-
assign :response, double(params: {})
13+
assign :response, double(start: 0)
1414
allow(view).to receive(:render_grouped_response?).and_return false
1515
allow(view).to receive(:blacklight_config).and_return(blacklight_config)
1616
allow(view).to receive(:current_search_session).and_return nil

spec/views/catalog/_thumbnail_default.erb_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
end
1818

1919
before do
20-
assign :response, double(:params => {})
20+
assign :response, double(start: 0)
2121
allow(view).to receive(:render_grouped_response?).and_return false
2222
allow(view).to receive(:blacklight_config).and_return(blacklight_config)
2323
allow(view).to receive(:current_search_session).and_return nil

0 commit comments

Comments
 (0)