@@ -2,45 +2,39 @@ class Bookmark < ApplicationRecord
22 belongs_to :user
33 belongs_to :bookmarkable , polymorphic : true
44
5+ BOOKMARKABLE_MODELS = [ "CommunityNews" , "Event" , "Facilitator" , "Project" , "Resource" , "Story" , "StoryIdea" ,
6+ "Workshop" , "WorkshopIdea" , "WorkshopLog" , "WorkshopVariation" ]
7+
58 scope :for_workshops , -> { where ( bookmarkable_type : 'Workshop' ) }
69 scope :bookmarkable_type , -> ( bookmarkable_type ) { bookmarkable_type . present? ? where ( bookmarkable_type : bookmarkable_type ) : all }
710 scope :bookmarkable_attributes , -> ( bookmarkable_type , bookmarkable_id ) {
811 bookmarkable_type . present? && bookmarkable_id . present? ? where ( bookmarkable_type : bookmarkable_type ,
912 bookmarkable_id : bookmarkable_id ) : all }
1013
14+ scope :sort_by_newest , -> { order ( created_at : :desc ) }
15+ scope :sort_by_popularity , -> {
16+ select ( "bookmarks.*, COUNT(all_b.id) as popularity" )
17+ . joins ( "LEFT JOIN bookmarks all_b ON all_b.bookmarkable_id = bookmarks.bookmarkable_id AND
18+ all_b.bookmarkable_type = bookmarks.bookmarkable_type" )
19+ . group ( "bookmarks.id" )
20+ . order ( "popularity DESC" ) }
21+
22+
1123 def self . search ( params , user : nil )
1224 bookmarks = user ? user . bookmarks : self . all
1325 bookmarks = bookmarks . filter_by_params ( params )
26+ bookmarks = bookmarks . sorted ( params [ :sort ] )
27+ bookmarks
28+ end
1429
15- sort = params [ :sort ] . presence || "title"
16-
17- case sort
18- when "title"
19- bookmarks = bookmarks
20- . joins ( <<~SQL )
21- LEFT JOIN workshops ON bookmarks.bookmarkable_type = 'Workshop' AND workshops.id = bookmarks.bookmarkable_id
22- -- LEFT JOIN stories ON bookmarks.bookmarkable_type = 'Story' AND stories.id = bookmarks.bookmarkable_id
23- LEFT JOIN resources ON bookmarks.bookmarkable_type = 'Resource' AND resources.id = bookmarks.bookmarkable_id
24- LEFT JOIN events ON bookmarks.bookmarkable_type = 'Event' AND events.id = bookmarks.bookmarkable_id
25- SQL
26- . order ( Arel . sql ( "COALESCE(workshops.title, resources.title, events.title) ASC" ) ) # stories.title,
27- when "led"
28- bookmarks = bookmarks . where ( bookmarkable_type : "Workshop" )
29- . joins ( "INNER JOIN workshops ON bookmarks.bookmarkable_id = workshops.id" )
30- . order ( "workshops.led_count DESC" )
31- when "bookmark_count"
32- counts = bookmarks . group ( :bookmarkable_type , :bookmarkable_id )
33- . select ( :bookmarkable_type , :bookmarkable_id , "COUNT(*) AS total_bookmarks" )
34- bookmarks = bookmarks
35- . joins ( "LEFT JOIN (#{ counts . to_sql } ) AS counts
36- ON counts.bookmarkable_type = bookmarks.bookmarkable_type
37- AND counts.bookmarkable_id = bookmarks.bookmarkable_id" )
38- . order ( Arel . sql ( "COALESCE(counts.total_bookmarks,0) DESC" ) )
39- when "created"
40- bookmarks = bookmarks . order ( created_at : :desc )
30+ def self . sorted ( sort_by = nil ) # sort and sort_by are namespaced
31+ sort_by ||= "newest"
32+ case sort_by
33+ when "newest" then self . sort_by_newest
34+ when "title" then self . sort_by_title
35+ when "popularity" then self . sort_by_popularity
36+ else self . sort_by_newest
4137 end
42-
43- bookmarks
4438 end
4539
4640 def self . filter_by_params ( params = { } )
@@ -49,26 +43,74 @@ def self.filter_by_params(params={})
4943 bookmarks = bookmarks . bookmarkable_type ( params [ :bookmarkable_type ] )
5044 bookmarks = bookmarks . bookmarkable_attributes ( params [ :bookmarkable_type ] ,
5145 params [ :bookmarkable_id ] )
52- bookmarks = bookmarks . title ( params [ :title ] )
53- bookmarks = bookmarks . user_name ( params [ :user_name ] )
54- bookmarks = bookmarks . windows_type ( params [ :windows_type ] )
46+ bookmarks = bookmarks . title ( params [ :title ] ) if params [ :title ] . present?
47+ bookmarks = bookmarks . user_name ( params [ :user_name ] ) if params [ :user_name ] . present?
48+ bookmarks = bookmarks . windows_type ( params [ :windows_type ] ) if params [ :windows_type ] . present?
5549
5650 bookmarks
5751 end
5852
53+ def self . sort_by_title
54+ bookmarks = self . joins ( <<~SQL )
55+ LEFT JOIN community_news ON community_news.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'CommunityNews'
56+ LEFT JOIN events ON events.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Event'
57+ LEFT JOIN facilitators ON facilitators.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Facilitator'
58+ LEFT JOIN projects ON projects.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Project'
59+ LEFT JOIN resources ON resources.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Resource'
60+ LEFT JOIN stories ON stories.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Story'
61+ LEFT JOIN story_ideas ON story_ideas.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'StoryIdea'
62+ LEFT JOIN workshops ON workshops.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Workshop'
63+ LEFT JOIN workshop_ideas ON workshop_ideas.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'WorkshopIdea'
64+ LEFT JOIN workshop_logs ON workshop_logs.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'WorkshopLog'
65+ LEFT JOIN workshop_variations ON workshop_variations.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'WorkshopVariation'
66+ SQL
67+ bookmarks . order ( Arel . sql ( <<~SQL . squish )
68+ LOWER(
69+ COALESCE(
70+ community_news.title,
71+ events.title,
72+ CONCAT(facilitators.first_name, ' ', facilitators.last_name),
73+ projects.name,
74+ resources.title,
75+ stories.title,
76+ story_ideas.title,
77+ workshops.title,
78+ workshop_ideas.title,
79+ DATE_FORMAT(workshop_logs.date, '%Y-%m-%d'),
80+ workshop_variations.name
81+ )
82+ ) ASC,
83+ bookmarks.created_at DESC
84+ SQL
85+ )
86+ end
87+
5988 def self . title ( title )
6089 return all unless title . present?
6190
6291 bookmarks = self . all
6392 bookmarks = bookmarks . joins ( <<~SQL )
64- LEFT JOIN workshops ON workshops.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Workshop'
65- -- LEFT JOIN stories ON stories.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Story'
66- LEFT JOIN resources ON resources.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Resource'
67- LEFT JOIN events ON events.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Event'
93+ LEFT JOIN community_news ON community_news.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'CommunityNews'
94+ LEFT JOIN events ON events.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Event'
95+ LEFT JOIN facilitators ON facilitators.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Facilitator'
96+ LEFT JOIN projects ON projects.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Project'
97+ LEFT JOIN resources ON resources.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Resource'
98+ LEFT JOIN stories ON stories.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Story'
99+ LEFT JOIN story_ideas ON story_ideas.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'StoryIdea'
100+ LEFT JOIN workshops ON workshops.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Workshop'
101+ LEFT JOIN workshop_ideas ON workshop_ideas.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'WorkshopIdea'
102+ LEFT JOIN workshop_logs ON workshop_logs.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'WorkshopLog'
103+ LEFT JOIN workshop_variations ON workshop_variations.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'WorkshopVariation'
68104 SQL
69105
70106 bookmarks . where (
71- "workshops.title LIKE :title OR events.title LIKE :title OR resources.title LIKE :title" , # OR stories.title LIKE :title
107+ "community_news.title LIKE :title OR events.title LIKE :title OR facilitators.first_name LIKE :title OR
108+ facilitators.last_name LIKE :title OR projects.name LIKE :title OR resources.title LIKE :title OR
109+ stories.title LIKE :title OR workshops.title LIKE :title OR workshop_ideas.title LIKE :title OR
110+ story_ideas.body LIKE :title OR -- searching body for story ideas (title exists but isn't used in UI)
111+ DATE_FORMAT(workshop_logs.date, '%Y-%m-%d') LIKE :title OR -- no title on workshop_logs
112+ workshop_variations.name LIKE :title -- searching name for workshop variations (title doesn't exist)
113+ " ,
72114 title : "%#{ title } %"
73115 )
74116 end
@@ -143,13 +185,5 @@ def self.user_name(user_name)
143185 )
144186 end
145187
146- def bookmarkable_image_url ( fallback : 'missing.png' )
147- if bookmarkable . respond_to? ( :images ) && bookmarkable . images . first &.file &.attached?
148- Rails . application . routes . url_helpers . rails_blob_path ( bookmarkable . images . first . file , only_path : true )
149- elsif bookmarkable_type == "Workshop"
150- ActionController ::Base . helpers . asset_path ( "workshop_default.jpg" )
151- else
152- ActionController ::Base . helpers . asset_path ( fallback )
153- end
154- end
188+
155189end
0 commit comments