Skip to content

Commit

Permalink
Add game schedule list to media home.
Browse files Browse the repository at this point in the history
  • Loading branch information
yamat47 committed Aug 11, 2024
1 parent c53be6c commit 5baaf99
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/app/assets/stylesheets/media/hero.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

.hero-container {
width: 100vw;
height: 50vw;
height: 33vw;
position: relative;

@media (max-width: 768px) {
Expand Down
15 changes: 14 additions & 1 deletion app/app/assets/stylesheets/media/home.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
.media-home {
width: 90vw;
padding-block: 2rem;
width: 96vw;
margin-inline: auto;
display: flex;
flex-direction: column;
gap: 4rem;

@media (min-width: 768px) {
padding-block: 4rem;
Expand All @@ -14,6 +18,15 @@
gap: 2rem;
}

.media-home-section__title {
width: 90vw;
margin-inline: auto;

@media (min-width: 768px) {
width: 48rem;
}
}

.media-home-section-button {
display: flex;
justify-content: flex-end;
Expand Down
4 changes: 2 additions & 2 deletions app/app/assets/stylesheets/media/notice-list.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
gap: 1.0rem;

&:hover {
outline: 2px solid var(--color-purple-500);
outline-offset: -2px;
cursor: pointer;
background-color: var(--color-gray-100);
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/app/controllers/media/homes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def show
@notices = Notice.only_published
.published_at_ordered
.limit(NOTICES_LIMIT)

@game_schedules = GameSchedule.next_scheduled_games
end
end
end
12 changes: 12 additions & 0 deletions app/app/models/game_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GameSchedule < ApplicationRecord

validate :home_team_and_visitor_team_must_be_different

scope :from_today, ->(now: Time.current) { where(start_at: now.beginning_of_day..) }
scope :start_at_ordered, -> { order(start_at: :asc) }
scope :game_field_ordered, -> { order(game_field_id: :asc) }

Expand All @@ -30,6 +31,17 @@ class GameSchedule < ApplicationRecord
SQL
}

# Returns all games scheduled on the same day as the next upcoming game.
scope :next_scheduled_games, lambda { |now: Time.current|
next_scheduled_datetime = from_today(now:).start_at_ordered
.first
&.start_at

return none unless next_scheduled_datetime

where(start_at: next_scheduled_datetime.all_day)
}

delegate :name, to: :home_team, prefix: true, allow_nil: true
delegate :name, to: :visitor_team, prefix: true, allow_nil: true
delegate :name, to: :game_field, prefix: true, allow_nil: true
Expand Down
48 changes: 47 additions & 1 deletion app/app/views/media/homes/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.media-home-section
.media-home-section__title
.media-subheader
最新のお知らせ
お知らせ
.media-home-section__content
.media-notice-list
- @notices.each do |notice|
Expand All @@ -27,3 +27,49 @@
= link_to notices_path, class: 'media-button' do
%span 一覧を見る
= image_tag 'icon-right-arrow.svg'

.media-home__section
.media-home-section
.media-home-section__title
.media-subheader
次回の試合
.media-home-section__content
.media-game-schedule-list
.media-game-schedule-list__content
%table.media-game-schedule-content
%tr.media-game-schedule-content__header
%th
= t('view.media.game_schedules.index.game_field')
%th
= t('view.media.game_schedules.index.start_at')
%th
= t('view.media.game_schedules.index.tournament')
%th{ colspan: 3 }
= t('view.media.game_schedules.index.teams')
- @game_schedules.each do |game_schedule|
%tr.media-game-schedule-content__item
%td
= link_to game_schedule.game_field_name,
game_fields_path(anchor: media_game_field_anchor(game_schedule.game_field)),
class: 'common-text-link'
%td
= l game_schedule.start_at, format: :only_time
%td
= game_schedule.tournament_name
%td
= game_schedule.home_team_name
%td.is-centered
.media-game-schedule-score
.media-game-schedule-score-home
= game_schedule.game_result_home_team_score
.media-game-schedule-score-separator
.media-game-schedule-score-visitor
= game_schedule.game_result_visitor_team_score
%td
= game_schedule.visitor_team_name

.media-home-section__button
.media-home-section-button
= link_to game_schedules_path, class: 'media-button' do
%span 一覧を見る
= image_tag 'icon-right-arrow.svg'
2 changes: 1 addition & 1 deletion app/spec/factories/teams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

FactoryBot.define do
factory :team do
name { Faker::Sports::Football.team }
sequence(:name) { |n| "Team #{n}" }

after :build do |team|
image = URI.parse('https://picsum.photos/500').open
Expand Down
2 changes: 1 addition & 1 deletion app/spec/factories/tournaments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :tournament do
name { Faker::Sports::Football.competition }
sequence(:name) { |n| "Tournament #{n}" }
end
end
51 changes: 51 additions & 0 deletions app/spec/models/game_schedule_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe GameSchedule do
describe '.from_today' do
let!(:game_schedule_today) { create(:game_schedule, start_at: Time.current) }
let!(:game_schedule_tomorrow) { create(:game_schedule, start_at: 1.day.from_now) }

before do
create(:game_schedule, start_at: 1.day.ago)
end

context 'when argument is given' do
subject { described_class.from_today(now: 1.day.from_now) }

it { is_expected.to eq([game_schedule_tomorrow]) }
end

context 'when argument is not given' do
subject { described_class.from_today }

it { is_expected.to eq([game_schedule_today, game_schedule_tomorrow]) }
end
end

describe '.next_scheduled_games' do
let!(:game_schedule_today_am) { create(:game_schedule, start_at: Time.current.beginning_of_day + 10.hours) }
let!(:game_schedule_today_pm) { create(:game_schedule, start_at: Time.current.end_of_day - 10.hours) }
let!(:game_schedule_tomorrow_am) { create(:game_schedule, start_at: 1.day.from_now.beginning_of_day + 10.hours) }
let!(:game_schedule_tomorrow_pm) { create(:game_schedule, start_at: 1.day.from_now.end_of_day - 10.hours) }

context 'when argument is given' do
subject { described_class.next_scheduled_games(now: 1.day.from_now) }

it { is_expected.to contain_exactly(game_schedule_tomorrow_am, game_schedule_tomorrow_pm) }
end

context 'when argument is given but no game is scheduled' do
subject { described_class.next_scheduled_games(now: 2.days.from_now) }

it { is_expected.to be_empty }
end

context 'when argument is not given' do
subject { described_class.next_scheduled_games }

it { is_expected.to contain_exactly(game_schedule_today_am, game_schedule_today_pm) }
end
end
end

0 comments on commit 5baaf99

Please sign in to comment.