Skip to content

Commit 41b28cc

Browse files
authored
add created_at filter and orders to talks api (#645)
1 parent 1dcc960 commit 41b28cc

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

app/controllers/talks_controller.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def index
1818
@talks = @talks.for_event(params[:event]) if params[:event].present?
1919
@talks = @talks.for_speaker(params[:speaker]) if params[:speaker].present?
2020
@talks = @talks.where(kind: talk_kind) if talk_kind.present?
21+
@talks = @talks.where("created_at >= ?", created_after) if created_after
2122
@talks = @talks.order(order_by) if order_by
22-
@pagy, @talks = pagy(@talks, items: 20, page: params[:page]&.to_i || 1)
23+
@pagy, @talks = pagy(@talks, **pagy_params)
2324
end
2425

2526
# GET /talks/1
@@ -50,7 +51,9 @@ def order_by
5051
return if params[:s].present? && params[:order_by].blank?
5152
order_by_options = {
5253
"date_desc" => "talks.date DESC",
53-
"date_asc" => "talks.date ASC"
54+
"date_asc" => "talks.date ASC",
55+
"created_at_desc" => "talks.created_at DESC",
56+
"created_at_asc" => "talks.created_at ASC"
5457
}
5558

5659
@order_by ||= begin
@@ -60,6 +63,19 @@ def order_by
6063
end
6164
end
6265

66+
def created_after
67+
Date.parse(params[:created_after]) if params[:created_after].present?
68+
rescue ArgumentError
69+
nil
70+
end
71+
72+
def pagy_params
73+
{
74+
limit: params[:limit]&.to_i,
75+
page: params[:page]&.to_i
76+
}.compact_blank
77+
end
78+
6379
def talk_kind
6480
@talk_kind ||= params[:kind].presence_in(Talk.kinds.keys)
6581
end

test/controllers/talks_controller_test.rb

+28
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,39 @@ class TalksControllerTest < ActionDispatch::IntegrationTest
122122
assert_equal talk.slug, json_response["talks"].first["slug"]
123123
end
124124

125+
test "should get index as JSON with a custom per_page" do
126+
assert Talk.watchable.count > 2
127+
get talks_url(format: :json, limit: 2)
128+
assert_response :success
129+
130+
json_response = JSON.parse(response.body)
131+
132+
assert_equal 2, json_response["talks"].size
133+
end
134+
125135
test "should get show as JSON" do
126136
get talk_url(@talk, format: :json)
127137
assert_response :success
128138

129139
json_response = JSON.parse(response.body)
130140
assert_equal @talk.slug, json_response["talk"]["slug"]
131141
end
142+
143+
test "should get index with created_after" do
144+
talk = Talk.create!(title: "test", description: "test", date: "2023-01-01", created_at: "2023-01-01", video_provider: "youtube")
145+
talk_2 = Talk.create!(title: "test 2", description: "test", date: "2025-01-01", created_at: "2025-01-01", video_provider: "youtube")
146+
147+
get talks_url(created_after: "2024-01-01")
148+
assert_response :success
149+
assert assigns(:talks).size.positive?
150+
refute assigns(:talks).ids.include?(talk.id)
151+
assert assigns(:talks).ids.include?(talk_2.id)
152+
assert assigns(:talks).all? { |talk| talk.created_at >= Date.parse("2024-01-01") }
153+
end
154+
155+
test "should get index with invalide created_after" do
156+
get talks_url(created_after: "wrong-date")
157+
assert_response :success
158+
assert assigns(:talks).size.positive?
159+
end
132160
end

test/fixtures/talks.yml

+9
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ two:
7777
slug: talk-title-2
7878
kind: "talk"
7979
date: "2025-01-31"
80+
video_provider: youtube
81+
82+
three:
83+
title: talk title 3
84+
description: talk descritpion 3
85+
slug: talk-title-3
86+
kind: "talk"
87+
date: "2025-01-31"
88+
video_provider: youtube
8089

8190
brightonruby_2024_one:
8291
title: Getting to Two Million Users as a One Woman Dev Team

test/jobs/recurring/youtube_video_statistics_job_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class Recurring::YoutubeVideoStatisticsJobTest < ActiveJob::TestCase
44
test "should update view_count and like_count for youtube talks" do
55
VCR.use_cassette("recurring_youtube_statistics_job", match_requests_on: [:method]) do
6-
talk = Talk.youtube.first
6+
talk = talks(:one)
77
assert_not talk.view_count.positive?
88
assert_not talk.like_count.positive?
99
Recurring::YoutubeVideoStatisticsJob.new.perform

0 commit comments

Comments
 (0)