Skip to content

Commit

Permalink
5#random podcast by day (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
krillan49 authored Jan 20, 2025
1 parent e898da0 commit 0c8b145
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class HomeController < ApplicationController
allow_unauthenticated_access

def index
@random_podcast = Podcast.offset(rand(Podcast.count)).first # TODO: use cron
@random_podcast = Podcast.random_by_day
@last_podcast = Podcast.last
end
end
9 changes: 9 additions & 0 deletions app/models/podcast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ class Podcast < ApplicationRecord

validates :title, presence: true
validates :title, length: { minimum: 2 }

def self.random_by_day
return unless exists?

cached_random_podcast_id = Rails.cache.fetch("random_podcast_id", expires_in: 1.days) do
offset(rand(Podcast.count)).first.id
end
find_by(id: cached_random_podcast_id)
end
end
14 changes: 11 additions & 3 deletions app/views/home/_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<% if Podcast.any? %>
<div class="card-group">
<div class="card">
<img src="https://mir-s3-cdn-cf.behance.net/project_modules/max_1200/9abc28136491393.61fa8bcfee8ce.png" class="card-img-top" alt="...">
<% if @last_podcast.photo.attached? %>
<%= image_tag @last_podcast.photo, class: "card-img-top", alt: "last podcast" %>
<% else %>
<img src="https://mir-s3-cdn-cf.behance.net/project_modules/max_1200/9abc28136491393.61fa8bcfee8ce.png" class="card-img-top" alt="last podcast">
<% end %>
<div class="card-body">
<h5 class="card-title">
<%= link_to @last_podcast.title, @last_podcast,
Expand All @@ -14,8 +18,12 @@
<small class="text-body-secondary"><%= @last_podcast.created_at %></small>
</div>
</div>
<div class="card">
<img src="https://mir-s3-cdn-cf.behance.net/project_modules/max_1200/c4c965136491393.6336af6811b4b.jpg" class="card-img-top" alt="...">
<div class="card" id="random_podcast">
<% if @random_podcast.photo.attached? %>
<%= image_tag @random_podcast.photo, class: "card-img-top", alt: "random podcast" %>
<% else %>
<img src="https://mir-s3-cdn-cf.behance.net/project_modules/max_1200/c4c965136491393.6336af6811b4b.jpg" class="card-img-top" alt="random podcast">
<% end %>
<div class="card-body">
<h5 class="card-title">
<%= link_to @random_podcast.title, @random_podcast,
Expand Down
32 changes: 32 additions & 0 deletions spec/features/home_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@
10.times { FactoryBot.create(:podcast) }
end

context "random podcast" do
# connect the cache to the tests only in this block
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
let(:cache) { Rails.cache }

before do
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
end

scenario "by default" do
visit root_path

random_podcast_id = Rails.cache.read("random_podcast_id")

expect(random_podcast_id).to be_present
expect(Podcast.exists?(id: random_podcast_id)).to be_truthy
expect(page).to have_selector("#random_podcast")
end

scenario "forced" do
Rails.cache.write("random_podcast_id", 6, expires_in: 1.days)
visit root_path

random_podcast_id = Rails.cache.read("random_podcast_id")

expect(random_podcast_id).to eq(6)
expect(Podcast.exists?(id: 6)).to be_truthy
expect(page).to have_selector("#random_podcast")
end
end

context do
before { visit root_path }

Expand Down

0 comments on commit 0c8b145

Please sign in to comment.