Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended talks #64

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?
yshmarov marked this conversation as resolved.
Show resolved Hide resolved
@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)
yshmarov marked this conversation as resolved.
Show resolved Hide resolved
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">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah first time I really use it but very helpful here to avoid a page blink

<%= 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