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

add event, speaker, kind, topics talks filter and asc/desc order_by #433

Merged
merged 3 commits into from
Nov 23, 2024
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
32 changes: 24 additions & 8 deletions app/controllers/talks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ class TalksController < ApplicationController

# GET /talks
def index
if params[:q].present?
talks = Talk.with_essential_card_data.pagy_search(params[:q])
@pagy, @talks = pagy_meilisearch(talks, limit: 20, page: params[:page]&.to_i || 1)
elsif params[:s].present?
@pagy, @talks = pagy(Talk.with_essential_card_data.ft_search(params[:s]).with_snippets.ranked, items: 20, page: params[:page]&.to_i || 1)
else
@pagy, @talks = pagy(Talk.all.with_essential_card_data.order(date: :desc), items: 20)
end
@talks = Talk.with_essential_card_data.order(order_by)
@talks = @talks.ft_search(params[:s]).with_snippets.ranked if params[:s].present?
@talks = @talks.for_topic(params[:topic]) if params[:topic].present?
@talks = @talks.for_event(params[:event]) if params[:event].present?
@talks = @talks.for_speaker(params[:speaker]) if params[:speaker].present?
@talks = @talks.where(kind: talk_kind) if talk_kind.present?
@pagy, @talks = pagy(@talks, items: 20, page: params[:page]&.to_i || 1)
end

# GET /talks/1
Expand All @@ -38,6 +37,23 @@ def update

private

def order_by
order_by_options = {
"date_desc" => "talks.date DESC",
"date_asc" => "talks.date ASC"
}

@order_by ||= begin
order = params[:order_by].presence_in(order_by_options.keys) || "date_desc"

order_by_options[order]
end
end

def talk_kind
@talk_kind ||= params[:kind].presence_in(Talk.kinds.keys)
end

# Use callbacks to share common setup or constraints between actions.
def set_talk
@talk = Talk.includes(:approved_topics, speakers: :user, event: :organisation).find_by(slug: params[:slug])
Expand Down
6 changes: 5 additions & 1 deletion app/models/talk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# enhanced_transcript :text default(#<Transcript:0x0000000120e51930 @cues=[]>), not null
# external_player :boolean default(FALSE), not null
# external_player_url :string default(""), not null
# kind :string default("talk"), not null
# kind :string default("talk"), not null, indexed
# language :string default("en"), not null
# like_count :integer
# raw_transcript :text default(#<Transcript:0x0000000120e51a98 @cues=[]>), not null
Expand All @@ -34,6 +34,7 @@
#
# index_talks_on_date (date)
# index_talks_on_event_id (event_id)
# index_talks_on_kind (kind)
# index_talks_on_slug (slug)
# index_talks_on_title (title)
# index_talks_on_updated_at (updated_at)
Expand Down Expand Up @@ -130,6 +131,9 @@ def analyze_talk_topics!
scope :without_summary, -> { where("summary IS NULL OR summary = ''") }
scope :without_topics, -> { where.missing(:talk_topics) }
scope :with_topics, -> { joins(:talk_topics) }
scope :for_topic, ->(topic_slug) { joins(:topics).where(topics: {slug: topic_slug}) }
scope :for_speaker, ->(speaker_slug) { joins(:speakers).where(speakers: {slug: speaker_slug}) }
scope :for_event, ->(event_slug) { joins(:event).where(events: {slug: event_slug}) }

scope :with_essential_card_data, -> do
select(:id, :slug, :title, :date, :thumbnail_sm, :thumbnail_lg, :video_id, :video_provider, :event_id, :language)
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241122163052_add_index_on_kind_for_talks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIndexOnKindForTalks < ActiveRecord::Migration[8.0]
def change
add_index :talks, :kind
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/controllers/talks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ class TalksControllerTest < ActionDispatch::IntegrationTest
assert_select "h1", /search results for "rails"/i
end

test "should get index with topic" do
get talks_url(topic: "activerecord")
assert_response :success
assert assigns(:talks).size.positive?
assert assigns(:talks).all? { |talk| talk.topics.map(&:slug).include?("activerecord") }
end

test "should get index with event" do
get talks_url(event: "rails-world-2023")
assert_response :success
assert assigns(:talks).size.positive?
assert assigns(:talks).all? { |talk| talk.event.slug == "rails-world-2023" }
end

test "should get index with speaker" do
get talks_url(speaker: "yaroslav-shmarov")
assert_response :success
assert assigns(:talks).size.positive?
assert assigns(:talks).all? { |talk| talk.speakers.map(&:slug).include?("yaroslav-shmarov") }
end

test "should show talk" do
get talk_url(@talk)
assert_response :success
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/speaker_talks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
# rubocop:enable Layout/LineLength

one:
speaker: one
speaker: yaroslav
talk: one
7 changes: 7 additions & 0 deletions test/fixtures/speakers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ michael:
bio: Author of the @railstutorial, founder of @learnenough, @softcover, and Tau Day.
website: https://www.michaelhartl.com/
slug: michael-hartl
yaroslav:
name: Yaroslav Shmarov
twitter: yarotheslav
github: yshmarov
bio: "Source code for My Ruby on Rails tutorials: @corsego"
website: https://superails.com/
slug: yaroslav-shmarov
3 changes: 2 additions & 1 deletion test/fixtures/talks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# enhanced_transcript :text default(#<Transcript:0x0000000120e51930 @cues=[]>), not null
# external_player :boolean default(FALSE), not null
# external_player_url :string default(""), not null
# kind :string default("talk"), not null
# kind :string default("talk"), not null, indexed
# language :string default("en"), not null
# like_count :integer
# raw_transcript :text default(#<Transcript:0x0000000120e51a98 @cues=[]>), not null
Expand All @@ -34,6 +34,7 @@
#
# index_talks_on_date (date)
# index_talks_on_event_id (event_id)
# index_talks_on_kind (kind)
# index_talks_on_slug (slug)
# index_talks_on_title (title)
# index_talks_on_updated_at (updated_at)
Expand Down
5 changes: 5 additions & 0 deletions test/models/talk_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,9 @@ class TalkTest < ActiveSupport::TestCase
assert_match %r{^/assets/events/brightonruby/brightonruby-2024/poster-.*.webp$}, talk.thumbnail
assert_match %r{^/assets/events/brightonruby/brightonruby-2024/poster-.*.webp$}, talk.thumbnail(:thumbnail_xl)
end

test "for_topic" do
talk = talks(:one)
assert_includes Talk.for_topic("activerecord"), talk
end
end