Skip to content

Commit 6dba394

Browse files
authored
Split led_count from bookmarks_count (and add sorting by bookmarks_count) (#591)
1 parent f8e0e6e commit 6dba394

File tree

11 files changed

+104
-50
lines changed

11 files changed

+104
-50
lines changed

app/controllers/bookmarks_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def personal
4040
def create
4141
@bookmark = current_user.bookmarks.find_or_create_by(bookmark_params)
4242
@bookmarkable = @bookmark.bookmarkable
43-
@bookmarkable.update(led_count: @bookmarkable.led_count + 1) if @bookmarkable.has_attribute?(:led_count)
4443
respond_to do |format|
4544
format.html {
4645
redirect_to authenticated_root_path, notice: "#{@bookmark.bookmarkable_type} added to your bookmarks."
@@ -63,7 +62,6 @@ def destroy
6362
if @bookmark
6463
@bookmark.destroy
6564
@bookmarkable = @bookmark.bookmarkable
66-
@bookmarkable.update(led_count: @bookmarkable.led_count - 1) if @bookmarkable.has_attribute?(:led_count)
6765
respond_to do |format|
6866
format.html {
6967
redirect_to authenticated_root_path, notice: "Bookmark has been deleted."

app/models/resource.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ def custom_label_list
103103
"#{self.title} (#{self.kind.upcase})" unless self.kind.nil?
104104
end
105105

106-
# Methods
107-
def led_count
108-
0
109-
end
110-
111106
def name
112107
title || id
113108
end

app/models/workshop.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class Workshop < ApplicationRecord
8787
) #{sort_order == "asc" ? "ASC" : "DESC"}
8888
SQL
8989
}
90+
scope :with_bookmarks_count, -> {
91+
left_joins(:bookmarks)
92+
.select("workshops.*, COUNT(bookmarks.id) AS bookmarks_count")
93+
.group("workshops.id")
94+
}
9095

9196
# Search Cop
9297
include SearchCop

app/services/workshop_search_service.rb

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ class WorkshopSearchService
55
def initialize(params = {}, super_user: false)
66
@params = params
77
@super_user = super_user
8-
@workshops = Workshop.all
98
@sort = default_sort
9+
@workshops =
10+
if @sort == "popularity"
11+
Workshop.with_bookmarks_count
12+
else
13+
Workshop.all
14+
end
1015
end
1116

1217
# Main entry point
@@ -196,45 +201,55 @@ def normalize_published_param
196201

197202
def order_by_params
198203
case sort
199-
when 'created'
200-
# order by year/month desc, then created_at desc, then title asc
204+
when "created"
201205
@workshops = @workshops.order(
202206
Arel.sql(<<~SQL.squish)
203-
CASE WHEN year IS NOT NULL AND month IS NOT NULL THEN 1 ELSE 2 END ASC,
204-
year DESC,
205-
month DESC,
206-
created_at DESC,
207-
title ASC
208-
SQL
207+
CASE WHEN year IS NOT NULL AND month IS NOT NULL THEN 1 ELSE 2 END ASC,
208+
year DESC,
209+
month DESC,
210+
created_at DESC,
211+
title ASC
212+
SQL
209213
)
210-
when 'led'
214+
when "led"
211215
@workshops = @workshops.order(led_count: :desc, title: :asc)
212-
when 'title'
216+
when "popularity"
217+
@workshops = @workshops.order(
218+
Arel.sql("bookmarks_count DESC, title ASC")
219+
)
220+
when "title"
213221
@workshops = @workshops.order(title: :asc)
214-
when 'keywords'
215-
# already ordered in filter_by_query
222+
when "keywords"
223+
# already ordered
216224
else
217225
@workshops = @workshops.order(created_at: :asc, title: :asc)
218226
end
219227
end
220228

229+
221230
# --- Handle distinct + order by FIELD(id, ...) for complex joins ---
222231
def resolve_ids_order
223-
return if sort == 'keywords' # ordering is already handled if this is the case
224-
225-
# Determine sort columns for select
226-
sort_columns = case sort
227-
when 'created' then [:id, :created_at, :year, :month, :title]
228-
when 'led' then [:id, :led_count, :title]
229-
when 'title' then [:id, :title]
230-
else [:id, :title]
231-
end
232-
233-
workshop_ids = @workshops
234-
.select(*sort_columns)
235-
.order(sort_columns)
236-
.map(&:id)
237-
@workshops = Workshop.where(id: workshop_ids)
238-
.order(Arel.sql("FIELD(id, #{workshop_ids.join(',')})"))
232+
return if sort == "keywords"
233+
234+
sort_columns =
235+
case sort
236+
when "created" then [:id, :created_at, :year, :month, :title]
237+
when "led" then [:id, :led_count, :title]
238+
when "popularity" then [:id, :bookmarks_count, :title]
239+
when "title" then [:id, :title]
240+
else [:id, :title]
241+
end
242+
243+
workshop_ids =
244+
@workshops
245+
.select(*sort_columns)
246+
.order(sort_columns)
247+
.map(&:id)
248+
249+
@workshops =
250+
Workshop
251+
.where(id: workshop_ids)
252+
.order(Arel.sql("FIELD(id, #{workshop_ids.join(',')})"))
239253
end
254+
240255
end

app/views/bookmarks/_flex.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
</div>
148148

149149
<div class="text-sm text-gray-600">
150-
Bookmarked:
150+
Led:
151151
<%= workshop.led_count %>
152152
<%= "time".pluralize(workshop.led_count) %>
153153
</div>

app/views/bookmarks/tally.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<table class="w-full border border-gray-200 bg-gray-300 rounded-lg shadow-sm">
4646
<thead>
4747
<tr>
48-
<th colspan="2" class="bg-gray-400 px-4 py-2 text-white">Workshop Led Counts</th>
48+
<th colspan="2" class="bg-gray-400 px-4 py-2 text-white">Workshops led (via logs)</th>
4949
</tr>
5050
<tr class="bg-gray-400">
5151
<th class="px-4 py-2 text-left">Count</th>
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
<%= turbo_stream.replace dom_id(@bookmarkable, :bookmark_icon), partial: "editable_bookmark_icon", locals: {resource: @bookmarkable} %>
22
<%= turbo_stream.replace dom_id(@bookmarkable, :bookmark_button), partial: "editable_bookmark_button", locals: {resource: @bookmarkable} %>
33

4-
<% if @bookmarkable.has_attribute?(:led_count) %>
5-
<%= turbo_stream.update dom_id(@bookmarkable, :bookmark_count) do %>
6-
Bookmarked:
7-
<%= @bookmarkable.led_count %>
8-
<%= "time".pluralize(@bookmarkable.led_count) %>
9-
<% end %>
10-
<%= turbo_stream.update dom_id(@bookmarkable, :bookmark_count_facilitator) do %>
11-
(Bookmarked by
12-
<%= pluralize(@bookmarkable.led_count, "facilitator") %>)
13-
<% end %>
4+
<%= turbo_stream.update dom_id(@bookmarkable, :led_count) do %>
5+
Led:
6+
<%= @bookmarkable.led_count %>
7+
<%= "time".pluralize(@bookmarkable.led_count) %>)
8+
<% end %>
9+
10+
<%= turbo_stream.update dom_id(@bookmarkable, :bookmark_count) do %>
11+
Bookmarked:
12+
<%= @bookmarkable.bookmarks_count %>
13+
<%= "time".pluralize(@bookmarkable.bookmarks_count) %>
14+
<% end %>
15+
16+
<%= turbo_stream.update dom_id(@bookmarkable, :bookmark_count_facilitator) do %>
17+
(Bookmarked by
18+
<%= pluralize(@bookmarkable.bookmarks_count, "facilitator") %>,
1419
<% end %>
1520

1621
<%= turbo_stream.replace "flash_now", partial: "shared/flash_messages" %>

app/views/workshops/_index_row.html.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@
6565
<!-- Bookmarked count -->
6666
<%= tag.div id: dom_id(workshop, :bookmark_count),
6767
class: "text-sm text-gray-600 leading-tight" do %>
68-
Bookmarked: <%= workshop.led_count %> <%= "time".pluralize(workshop.led_count) %>
68+
Bookmarked: <%= workshop.bookmarks_count %> <%= "time".pluralize(workshop.bookmarks_count.to_i) %>
69+
<% end %>
70+
71+
<!-- Led count -->
72+
<%= tag.div id: dom_id(workshop, :led_count),
73+
class: "text-sm text-gray-600 leading-tight" do %>
74+
Led: <%= workshop.led_count %> <%= "time".pluralize(workshop.led_count.to_i) %>
6975
<% end %>
7076

7177
<!-- Age ranges -->

app/views/workshops/_show_actions_row.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
<%= render "bookmarks/editable_bookmark_button", resource: workshop.object %>
66
<%= tag.span id: dom_id(workshop, :bookmark_count_facilitator), class: "text-gray-600 text-sm" do %>
77
(Bookmarked by
8-
<%= pluralize(workshop.led_count, "facilitator") %>)
8+
<%= pluralize(workshop.bookmarks.count, "facilitator") %>,
9+
<% end %>
10+
<%= tag.span id: dom_id(workshop, :led_count), class: "text-gray-600 text-sm" do %>
11+
Led:
12+
<%= pluralize(workshop.led_count, "time") %>)
913
<% end %>
1014
</div>
1115

app/views/workshops/_sort_by_options.html.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
<label class="flex items-center gap-1 cursor-pointer">
1919
<%= radio_button_tag :sort, "led",
2020
@sort == "led" %>
21+
<span class="text-sm">Most led</span>
22+
</label>
23+
</div>
24+
25+
<div class="flex items-center gap-1">
26+
<label class="flex items-center gap-1 cursor-pointer">
27+
<%= radio_button_tag :sort, "popularity",
28+
@sort == "popularity" %>
2129
<span class="text-sm">Most bookmarked</span>
2230
</label>
2331
</div>

0 commit comments

Comments
 (0)