Skip to content

Commit 8498100

Browse files
maebealeclaude
andauthored
Make FAQs bookmarkable (#1357)
Add bookmark support to FAQs using the existing polymorphic bookmark pattern — association, SQL joins for title search/sort, bookmark icon in the FAQ accordion, and FAQ as a dropdown option on bookmark indexes. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b28b96d commit 8498100

4 files changed

Lines changed: 10 additions & 3 deletions

File tree

app/models/bookmark.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Bookmark < ApplicationRecord
22
belongs_to :user
33
belongs_to :bookmarkable, polymorphic: true
44

5-
BOOKMARKABLE_MODELS = %w[CommunityNews Event Organization Person Report Resource Story StoryIdea
5+
BOOKMARKABLE_MODELS = %w[CommunityNews Event Faq Organization Person Report Resource Story StoryIdea
66
Tutorial Workshop WorkshopIdea WorkshopLog WorkshopVariation WorkshopVariationIdea].freeze
77

88
DROPDOWN_MODELS = (BOOKMARKABLE_MODELS - %w[Report]).freeze
@@ -62,6 +62,7 @@ def self.sort_by_title
6262
bookmarks = self.joins(<<~SQL)
6363
LEFT JOIN community_news AS st_cn ON st_cn.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'CommunityNews'
6464
LEFT JOIN events AS st_ev ON st_ev.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Event'
65+
LEFT JOIN faqs AS st_faq ON st_faq.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Faq'
6566
LEFT JOIN organizations AS st_org ON st_org.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Organization'
6667
LEFT JOIN people AS st_ppl ON st_ppl.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Person'
6768
LEFT JOIN resources AS st_res ON st_res.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Resource'
@@ -80,6 +81,7 @@ def self.sort_by_title
8081
COALESCE(
8182
st_cn.title,
8283
st_ev.title,
84+
st_faq.question,
8385
CONCAT(st_ppl.first_name, ' ', st_ppl.last_name),
8486
st_org.name,
8587
st_rpt.type,
@@ -106,6 +108,7 @@ def self.title(title)
106108
bookmarks = bookmarks.joins(<<~SQL)
107109
LEFT JOIN community_news ON community_news.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'CommunityNews'
108110
LEFT JOIN events ON events.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Event'
111+
LEFT JOIN faqs ON faqs.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Faq'
109112
LEFT JOIN organizations ON organizations.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Organization'
110113
LEFT JOIN people ON people.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Person'
111114
LEFT JOIN resources ON resources.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Resource'
@@ -121,7 +124,7 @@ def self.title(title)
121124
SQL
122125

123126
bookmarks.where(
124-
"community_news.title LIKE :title OR events.title LIKE :title OR people.first_name LIKE :title OR
127+
"community_news.title LIKE :title OR events.title LIKE :title OR faqs.question LIKE :title OR people.first_name LIKE :title OR
125128
people.last_name LIKE :title OR organizations.name LIKE :title OR resources.title LIKE :title OR
126129
reports.type LIKE :title OR
127130
stories.title LIKE :title OR workshops.title LIKE :title OR workshop_ideas.title LIKE :title OR

app/models/faq.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ class Faq < ApplicationRecord
22
include Publishable
33
positioned
44

5+
has_many :bookmarks, as: :bookmarkable, dependent: :destroy
6+
57
# Validations
68
validates_presence_of :question, :answer
79

app/views/faqs/_faq.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
</div>
101101
<% end %>
102102

103+
<%= render "bookmarks/editable_bookmark_icon", resource: faq %>
104+
103105
<button
104106
type="button"
105107
class="py-5 font-medium text-left cursor-pointer"

spec/models/faq_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# let(:faq) { build(:faq) } # Keep if needed
55

66
describe "associations" do
7-
# Add association tests if any
7+
it { should have_many(:bookmarks).dependent(:destroy) }
88
end
99

1010
describe "validations" do

0 commit comments

Comments
 (0)