Skip to content

Commit

Permalink
Recommended talks (#64)
Browse files Browse the repository at this point in the history
* add a dedicated recommended talks using turbo frame

* restrict this route to turbo frame requests only

* remove @talks form talk#show

* add a basic test

* try to remove flaky
  • Loading branch information
adrienpoly authored Oct 23, 2023
1 parent 38a7b1b commit f3b0f80
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 12 deletions.
15 changes: 15 additions & 0 deletions app/controllers/talks/recommendations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Talks::RecommendationsController < ApplicationController
skip_before_action :authenticate_user!
before_action :set_talk, only: %i[index]

def index
redirect_to talk_path(@talk) unless turbo_frame_request?
@talks = @talk.related_talks
end

private

def set_talk
@talk = Talk.find_by(slug: params[:talk_slug])
end
end
1 change: 0 additions & 1 deletion app/controllers/talks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def index
def show
speaker_slug = params[:speaker_slug]
@back_path = speaker_slug.present? ? speaker_path(speaker_slug, page: session[:talks_page]) : talks_path(page: session[:talks_page])
@talks = Talk.order("RANDOM()").excluding(@talk).limit(6)
set_meta_tags(@talk)
end

Expand Down
3 changes: 1 addition & 2 deletions app/javascript/controllers/transition_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTransition } from 'stimulus-use'

export default class extends Controller {
static values = {
enterActive: { type: String, default: 'transition ease-in duration-500' },
enterActive: { type: String, default: 'transition ease-in duration-300' },
enterFrom: { type: String, default: 'transform opacity-0' },
enterTo: { type: String, default: 'transform opacity-100' },
leaveActive: { type: String, default: 'transition ease-in duration-300' },
Expand Down Expand Up @@ -36,7 +36,6 @@ export default class extends Controller {

if (this.enterAfterValue >= 0) {
setTimeout(() => {
console.log('enter')
this.enter()
}, this.enterAfterValue)
}
Expand Down
4 changes: 4 additions & 0 deletions app/models/talk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ def thumbnail_lg
def thumbnail_xl
self[:thumbnail_xl].presence || "https://i.ytimg.com/vi/#{video_id}/maxresdefault.jpg"
end

def related_talks(limit: 6)
Talk.order("RANDOM()").excluding(self).limit(limit)
end
end
2 changes: 1 addition & 1 deletion app/views/talks/_card_horizontal.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div class="w-full items-center" id="<%= dom_id(talk, :card_horizontal) %>">
<div class="w-full items-center" id="<%= dom_id(talk, :card_horizontal) %>" data-talk-horizontal-card>

<%= link_to talk_path(talk),
class: "flex aspect-video shrink-0 relative" do %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/talks/recommendations/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= turbo_frame_tag "recommended_talks" do %>
<div data-turbo-temporary data-controller="transition" class="hidden" data-transition-enter-after-value="0">
<%= render partial: "talks/card_horizontal", collection: @talks, as: :talk, locals: {compact: true} %>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/talks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<% end %>
</div>
<div class="gap-4 w-60 flex-shrink-0 hidden md:flex md:flex-col px-4">
<%= render partial: "talks/card_horizontal", collection: @talks, as: :talk, locals: {compact: true} %>
<%= turbo_frame_tag "recommended_talks", target: "_top", src: talk_recommendations_path(@talk) %>
</div>
</div>
</div>
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
get :daily_visits
end
end
resources :talks, param: :slug, only: [:index, :show, :update, :edit]
resources :talks, param: :slug, only: [:index, :show, :update, :edit] do
scope module: :talks do
resources :recommendations, only: [:index]
end
end
resources :speakers, param: :slug, only: [:index, :show, :update, :edit]
resources :events, param: :slug, only: [:index, :show, :update, :edit]
namespace :speakers do
Expand Down
19 changes: 19 additions & 0 deletions test/controllers/talks/recommended_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "test_helper"

class Talks::RecommendationsControllerTest < ActionDispatch::IntegrationTest
def setup
@talk = talks(:one)
end

test "should get index with a turbo stream request" do
get talk_recommendations_url(@talk), headers: {"Turbo-Frame" => "true"}
assert_response :success
assert_equal assigns(:talk).id, @talk.id
assert_not_nil assigns(:talks)
end

test "a none turbo stream request should redirect to the talk" do
get talk_recommendations_url(@talk)
assert_redirected_to talk_url(@talk)
end
end
8 changes: 2 additions & 6 deletions test/system/speakers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ class SpeakersTest < ApplicationSystemTestCase
@speaker = speakers(:one)
end

# Contrary to "Talks", there is currently no "Speakers" heading
# test "visiting the index" do
# visit speakers_url
# assert_selector "h1", text: "Speakers"
# end

test "should update Speaker" do
visit speaker_url(@speaker)
click_on "Edit", match: :first

assert_text "Editing speaker"

fill_in "Bio", with: @speaker.bio
fill_in "Github", with: @speaker.github
fill_in "Name", with: @speaker.name
Expand Down
7 changes: 7 additions & 0 deletions test/system/talks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ class TalksTest < ApplicationSystemTestCase

assert_text "Your suggestion was successfully created and will be reviewed soon."
end

test "renders some related talks" do
visit talk_url(@talk)

assert_selector "#recommended_talks"
assert_selector "[data-talk-horizontal-card]", count: [Talk.excluding(@talk).count, 6].min
end
end

0 comments on commit f3b0f80

Please sign in to comment.