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

TrackをModelに切り出し、ソートを可能にする #128

Merged
merged 7 commits into from
Apr 8, 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
2 changes: 1 addition & 1 deletion app/controllers/schedules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SchedulesController < ApplicationController
include ScheduleTable

def index
@schedules = Schedule.on_event(@event).includes(:speakers).order(:start_at)
@schedules = @event.schedules.includes(:speakers, :track).order(:start_at)
@schedule_table = Schedule::Tables.new(@schedules)
end

Expand Down
12 changes: 11 additions & 1 deletion app/javascript/controllers/locale_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class extends Controller {
static values = { current: { type: String, default: '' } };

connect () {
if (this.currentValue === '') {
if (this.currentValue === '' && !this.isTestEnvironment) {
Turbo.visit(this.nonSearchParamURL(window.location) + '?' + new URLSearchParams({ locale: dayjs.tz.guess() }));
}
}
Expand All @@ -25,4 +25,14 @@ export default class extends Controller {
nonSearchParamURL (location) {
return location.toString().split('?')[0];
}

get isTestEnvironment() {
const railsEnv = document.head.querySelector("meta[name=rails_env]")
if ( railsEnv === null ) {
return false;
} else {
return railsEnv.content === "test"
}

}
}
6 changes: 6 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

class Event < ApplicationRecord
has_one :event_theme, dependent: :destroy
has_many :tracks
has_many :speakers

validates :name, presence: true, length: { in: 1..32 }
validates :event_theme, presence: true

def schedules
Schedule.where(track: tracks)
end
end
5 changes: 1 addition & 4 deletions app/models/schedule.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# frozen_string_literal: true

class Schedule < ApplicationRecord
include Events

has_many :schedule_speakers
has_many :speakers, through: :schedule_speakers
has_many :plan_schedules

belongs_to :event
belongs_to :track

enum language: { en: 0, ja: 1, 'en & ja': 2 }

validates :title, presence: true, length: { in: 1..100 }
validates :description, length: { in: 0..1024 }
validates :track_name, presence: true, length: { in: 1..32 }
validates :language, presence: true
end
2 changes: 1 addition & 1 deletion app/models/schedule/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Table
attr_reader :track_list, :rows

def initialize(schedules)
@track_list = schedules.map(&:track_name).sort.uniq
@track_list = schedules.map(&:track).uniq.sort_by(&:position).map(&:name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

このあたりN+1発生するのでは?と思って処理追ってみましたが、app/controllers/schedules_controller.rb@schedules を使っているので多分大丈夫だな、となりました


grouped_schedules = schedules.group_by do |s|
start_at = I18n.l(s.start_at, format: :timetable)
Expand Down
2 changes: 1 addition & 1 deletion app/models/schedule/table/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(schedules)
@start_end = "#{start_at} - #{end_at}"
@timezone = schedules[0].end_at.strftime('%Z')
@schedules = schedules
@tracks = schedules.map { [_1.track_name, _1] }.to_h
@tracks = schedules.map { [_1.track.name, _1] }.to_h
@sort_key = schedules[0].start_at
end
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/track.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class Track < ApplicationRecord
belongs_to :event
has_many :schedules

validates :name, presence: true
validates :position, presence: true
end
21 changes: 16 additions & 5 deletions app/models/validators/event_equality_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ class EventEqualityValidator < ActiveModel::Validator

def validate(record)
RELATIONS.inject(nil) do |acc, relation|
next acc unless record.respond_to?(relation) && record.send(relation)&.event_id
next acc unless record.respond_to?(relation)

acc ||= record.send(relation).event_id
event_id = find_event_id(record.send(relation))

if acc && acc != record.send(relation).event_id
record.errors.add relation, 'Event id must be same in cross relations.'
end
next acc unless event_id
next event_id unless acc

record.errors.add relation, 'Event id must be same in cross relations.' if acc != event_id

acc
end
end

private

def find_event_id(relation)
if relation.respond_to?(:event_id)
relation.event_id
elsif relation.respond_to?(:track)
relation.track.event.id
end
end
end
end
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400;500&family=Roboto:wght@400;500&display=auto" rel="stylesheet">

<meta name="turbo-visit-control" content="reload">
<% if Rails.env.test? %>
<%= tag :meta, name: :rails_env, content: Rails.env %>
<% end %>
<%= yield(:head) %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
</head>
Expand Down
2 changes: 1 addition & 1 deletion app/views/schedules/_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<span class="text-[rgb(112,109,101)]">Lang: </span><span class="ml-2"><%= schedule.language %></span>
</div>
<div class="flex flex-row justify-center items-center px-3 mr-2 h-6 bg-[rgb(248,247,246)] border border-solid border-[rgb(214,211,208)] box-border rounded-[100px] text-sm text-[rgb(35,34,31)]">
<span class="text-[rgb(112,109,101)]">Track: </span><span class="ml-2"><%= schedule.track_name %></span>
<span class="text-[rgb(112,109,101)]">Track: </span><span class="ml-2"><%= schedule.track.name %></span>
</div>
</div>
</div>
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20240404161019_create_tracks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateTracks < ActiveRecord::Migration[7.1]
def change
create_table :tracks, id: :uuid, default: -> { 'gen_random_uuid()' } do |t|
t.references :event, type: :uuid, null: false, foreign_key: true
t.string :name, null: false
t.integer :position, null: false, default: 1

t.timestamps
end

add_index :tracks, [:event_id, :name], unique: true
end
end
34 changes: 34 additions & 0 deletions db/migrate/20240404161858_convert_schedules_track.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class ConvertSchedulesTrack < ActiveRecord::Migration[7.1]
def up
add_column :schedules, :track_id, :uuid

Schedule.all.each do |schedule|
track = Track.find_or_create_by!(event: schedule.event, name: schedule.track_name)
schedule.track = track
schedule.save!
end

Event.all.each do |event|
event.tracks.sort_by { _1.name.reverse }.each_with_index do |track, i|
track.position = i + 1
track.save!
end
end

remove_column :schedules, :track_name
change_column :schedules, :track_id, :uuid, null: false
add_foreign_key :schedules, :tracks
end

def down
add_column :schedules, :track_name, :string

Schedule.all.each do |schedule|
schedule.track_name = schedule.track.name
schedule.save!
end

change_column :schedules, :track_name, :string, null: false
remove_column :schedules, :track_id
end
end
15 changes: 15 additions & 0 deletions db/migrate/20240405044111_schedule_belongs_track.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ScheduleBelongsTrack < ActiveRecord::Migration[7.1]
def up
remove_column :schedules, :event_id
end

def down
add_column :schedules, :event_id, :uuid

Schedule.all.each do |schedule|
schedule.update!(event: schedule.track.event)
end

change_column :schedules, :event_id, :uuid, null: false
end
end
17 changes: 14 additions & 3 deletions db/schema.rb

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

19 changes: 9 additions & 10 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@
next # LTのデータが埋まったら対応する
else
event['talks'].each do |track_name, id| schedule = schedules_by_id[id]

track = Track.find_or_create_by!(event: base_event, name: "Track#{track_name}")
schedule.update!(
track_name: "Track#{track_name}",
track: track,
start_at: start_at,
end_at: end_at,
event: base_event
end_at: end_at
)
end
end
Expand All @@ -78,20 +77,20 @@
next
when 'lt'
event['talks'].each do |track_name, id|
track = Track.find_or_create_by!(event: base_event, name: "Track#{track_name}")
hash[id] = Schedule.find_or_initialize_by(
track_name: "Track#{track_name}",
track: track,
start_at: start_at,
end_at: end_at,
event: base_event
end_at: end_at
)
end
else
event['talks'].each do |track_name, id|
track = Track.find_or_create_by!(event: base_event, name: "Track#{track_name}")
hash[id] = Schedule.find_or_initialize_by(
track_name: "Track#{track_name}",
track: track,
start_at: start_at,
end_at: end_at,
event: base_event
end_at: end_at
)
end
end
Expand Down
Loading
Loading