Skip to content

Commit 2e04432

Browse files
committed
Guard search indexing with searchable? check
Only index cards if they are `published?`, using a new method `searchable?` that is implemented on both Card and Comment. This prevents draft cards from being indexed. Note that comments can only be created on published cards, so `Comment#searchable` returns `true`. When drafts are published, the existing `update_in_search_index` callback creates the record via `upsert!`, or else ensures unpublished records are removed from the index.
1 parent 66a148c commit 2e04432

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

app/models/card/searchable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ def search_card_id
2525
def search_board_id
2626
board_id
2727
end
28+
29+
def searchable?
30+
published?
31+
end
2832
end

app/models/comment/searchable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ def search_card_id
2020
def search_board_id
2121
card.board_id
2222
end
23+
24+
def searchable?
25+
true
26+
end
2327
end

app/models/concerns/searchable.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ def reindex
1515

1616
private
1717
def create_in_search_index
18-
search_record_class.create!(search_record_attributes)
18+
if searchable?
19+
search_record_class.create!(search_record_attributes)
20+
end
1921
end
2022

2123
def update_in_search_index
22-
search_record_class.upsert!(search_record_attributes)
24+
if searchable?
25+
search_record_class.upsert!(search_record_attributes)
26+
else
27+
remove_from_search_index
28+
end
2329
end
2430

2531
def remove_from_search_index

test/models/card/searchable_test.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,46 @@ class Card::SearchableTest < ActiveSupport::TestCase
7878
assert_equal 0, fts_count, "FTS entry should be deleted"
7979
end
8080
end
81+
82+
test "updating a draft card does not index it" do
83+
search_record_class = Search::Record.for(@user.account_id)
84+
85+
card = @board.cards.create!(title: "Draft card", creator: @user, status: "drafted")
86+
assert_nil search_record_class.find_by(searchable_type: "Card", searchable_id: card.id)
87+
88+
card.update!(title: "Updated draft card")
89+
assert_nil search_record_class.find_by(searchable_type: "Card", searchable_id: card.id),
90+
"Draft card should not be indexed after update"
91+
92+
results = Card.mentioning("Updated", user: @user)
93+
assert_not_includes results, card
94+
end
95+
96+
test "publishing a draft card indexes it" do
97+
search_record_class = Search::Record.for(@user.account_id)
98+
99+
card = @board.cards.create!(title: "Draft to publish", creator: @user, status: "drafted")
100+
assert_nil search_record_class.find_by(searchable_type: "Card", searchable_id: card.id)
101+
102+
card.publish
103+
search_record = search_record_class.find_by(searchable_type: "Card", searchable_id: card.id)
104+
assert_not_nil search_record, "Published card should be indexed"
105+
assert_equal card.id, search_record.card_id
106+
107+
results = Card.mentioning("publish", user: @user)
108+
assert_includes results, card
109+
end
110+
111+
test "unpublishing a draft card removes it from the search index" do
112+
search_record_class = Search::Record.for(@user.account_id)
113+
114+
card = @board.cards.create!(title: "Draft to publish", creator: @user, status: "published")
115+
assert_not_nil search_record_class.find_by(searchable_type: "Card", searchable_id: card.id)
116+
117+
card.update!(status: "drafted")
118+
119+
assert_nil search_record_class.find_by(searchable_type: "Card", searchable_id: card.id)
120+
results = Card.mentioning("publish", user: @user)
121+
assert_not_includes results, card
122+
end
81123
end

test/models/search_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class SearchTest < ActiveSupport::TestCase
1515
results = Search::Record.for(@user.account_id).search("overflowing", user: @user)
1616
assert results.find { |it| it.card_id == comment_card.id && it.searchable_type == "Comment" }
1717

18+
# Drafted cards are excluded from search results
19+
drafted_card = @board.cards.create!(title: "drafted searchable content", creator: @user, status: "drafted")
20+
results = Search::Record.for(@user.account_id).search("drafted", user: @user)
21+
assert_not results.find { |it| it.card_id == drafted_card.id }
22+
1823
# Don't include inaccessible boards
1924
other_user = User.create!(name: "Other User", account: @account)
2025
inaccessible_board = Board.create!(name: "Inaccessible Board", account: @account, creator: other_user)

0 commit comments

Comments
 (0)