Skip to content

Commit 0ba1f45

Browse files
committed
Fix issue reindexing collection with content. Fixes #841
1 parent 3c6f119 commit 0ba1f45

3 files changed

Lines changed: 76 additions & 15 deletions

File tree

app/models/collection.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ class Collection < ApplicationRecord
1616
# Remove trailing and squeezes (:squish option) white spaces inside the string (before_validation):
1717
# e.g. "James Bond " => "James Bond"
1818
auto_strip_attributes :title, :description, :image_url, :squish => false
19-
after_save do
20-
index_items if saved_change_to_title?
21-
end
22-
after_destroy do
23-
index_items
24-
end
19+
20+
after_commit :index_items, if: :title_previously_changed?
2521

2622
validates :title, presence: true
2723

@@ -97,6 +93,6 @@ def scientific_topics
9793
def index_items
9894
return unless TeSS::Config.solr_enabled
9995

100-
items.each(&:solr_index)
96+
items.each(&:reindex_resource)
10197
end
10298
end

app/models/collection_item.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ class CollectionItem < ApplicationRecord
88

99
before_create :set_order
1010
after_save :log_activity
11-
after_create :solr_index
12-
after_destroy :solr_index
11+
after_commit :reindex_resource, on: [:create, :destroy]
1312

1413
def log_activity
1514
self.collection.create_activity(:add_item, owner: User.current_user,
@@ -21,15 +20,15 @@ def log_activity
2120
collection_title: self.collection.title })
2221
end
2322

23+
def reindex_resource
24+
# we should consider doing this in a background job if it turns out to be slow
25+
# when curating large collections
26+
resource.solr_index if TeSS::Config.solr_enabled
27+
end
28+
2429
private
2530

2631
def set_order
2732
self.order ||= (collection.items.maximum(:order) || 0) + 1
2833
end
29-
30-
def solr_index
31-
# we should consider doing this in a background job if it turns out to be slow
32-
# when curating large collections
33-
resource.solr_index if TeSS::Config.solr_enabled
34-
end
3534
end

test/models/collection_test.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
require 'test_helper'
22

33
class CollectionTest < ActiveSupport::TestCase
4+
teardown do
5+
DummyMaterial.clear_index!
6+
end
7+
8+
class DummyMaterial < ::Material
9+
def self.index
10+
(@index ||= Hash.new).values.flatten.uniq
11+
end
12+
13+
def self.add_to_index(m)
14+
index
15+
@index[m.id] = m.reload.collections.to_a
16+
end
17+
18+
def self.clear_index!
19+
@index = Hash.new
20+
end
21+
22+
def solr_index
23+
self.class.add_to_index(self)
24+
end
25+
end
426

527
test 'visibility scope' do
628
assert_not_includes Collection.visible_by(nil), collections(:secret_collection)
@@ -92,4 +114,48 @@ class CollectionTest < ActiveSupport::TestCase
92114
assert collection.destroy
93115
end
94116
end
117+
118+
test 'index collection resources when collection created or destroyed' do
119+
user = users(:regular_user)
120+
material = materials(:good_material).becomes(DummyMaterial)
121+
with_settings(solr_enabled: true) do
122+
collection = user.collections.create!(title: 'test 123', materials: [material])
123+
124+
assert collection
125+
assert_includes DummyMaterial.index, collection
126+
assert_equal 1, DummyMaterial.index.length
127+
128+
assert collection.destroy
129+
assert_equal 0, DummyMaterial.index.length
130+
end
131+
end
132+
133+
test 'index collection resources when collection renamed' do
134+
user = users(:regular_user)
135+
material = materials(:good_material).becomes(DummyMaterial)
136+
collection = user.collections.create!(title: 'test 123', materials: [material])
137+
assert collection
138+
139+
with_settings(solr_enabled: true) do
140+
assert_equal 0, DummyMaterial.index.length
141+
142+
collection.update!(title: 'Hello world')
143+
assert_includes DummyMaterial.index, collection
144+
assert_equal 1, DummyMaterial.index.length
145+
end
146+
end
147+
148+
test 'do not index collection resources when collection updated without renaming' do
149+
user = users(:regular_user)
150+
material = materials(:good_material).becomes(DummyMaterial)
151+
collection = user.collections.create!(title: 'test 123', materials: [material])
152+
assert collection
153+
154+
with_settings(solr_enabled: true) do
155+
assert_equal 0, DummyMaterial.index.length
156+
157+
collection.update!(description: 'hello')
158+
assert_equal 0, DummyMaterial.index.length
159+
end
160+
end
95161
end

0 commit comments

Comments
 (0)