SVX日記
2023-04-30(Sun) Ruby on Rails and Factorio
我ながらRubyはネイティブ言語のように使いこなせるが、RailsはDBとくっついていて、敷居が高そうというイメージでイマイチ興味がわかなかった。しかし、Rubyでプログラマをしようと思うと、Ruby on Railsが基本らしい。
つうわけで、まずはコンテナで開発できる環境を整えようと思ったのが4/10。しかし、開発環境が整うまでに10日ほどもかかった。それから、習作のカレンダを作り始めて10日。ようやく形になった。休日と予定の登録ができる。
結局、わかったことは、必要だと思うデータ構造を、全部「rails generatescaffold XXX」して、その雛形に肉付けしていけばいい、ということだ。驚くべきは、今回の習作のカレンダは、コードにして30行くらいで済んでいる。カレンダの描画以外、休日や予定の登録や更新、削除処理は書かずにデフォルト処理で済んでしまっているのが大きい。
$ rails generate scaffold Calendar
$ rails generate scaffold Holiday day:date:index cause:string
$ rails generate scaffold Schedule day:date:index member:string note:text
diff --git a/config/routes.rb b/config/routes.rb
index c5228e5..880faaf 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,5 @@
 Rails.application.routes.draw do
+  root "calendars#index"
   resources :schedules
   resources :holidays
   resources :calendars
diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb
index ec6db2e..3c75b0f 100644
--- a/app/controllers/calendars_controller.rb
+++ b/app/controllers/calendars_controller.rb
@@ -4,6 +4,27 @@ class CalendarsController < ApplicationController
   # GET /calendars or /calendars.json
   def index
     @calendars = Calendar.all
+
+	@today = Date.today
+	@first_day = @today.beginning_of_month
+	@start_day = @first_day - @first_day.wday
+
+	@days = []; day = @start_day
+	loop {
+		7.times {
+			@days << {
+				day:			day,
+				holidays:		Holiday.where(day: day),
+				schedules:		Schedule.where(day: day),
+				attrs:			{},
+			}
+			@days.last[:attrs][:color] = day.strftime('%a').downcase
+			@days.last[:attrs][:color] = 'holiday' if(@days.last[:holidays].size > 0)
+			@days.last[:attrs][:size] = 'small' if(day.mon != @today.mon)
+			day += 1
+		}
+		break if(day.mon != @today.mon)
+	}
   end
 
   # GET /calendars/1 or /calendars/1.json
diff --git a/app/views/calendars/index.html.erb b/app/views/calendars/index.html.erb
index 069f2ce..5c44f22 100644
--- a/app/views/calendars/index.html.erb
+++ b/app/views/calendars/index.html.erb
@@ -1,5 +1,29 @@
 <p style="color: green"><%= notice %></p>
 
+<H1>Calendar</H1>
+	<H3><%= @today.year %>/<%= @today.mon %></H3>
+		<TABLE border='1' cellspacing='0' cellpadding='4'>
+			<TR align='center'><TD>
+<%				@days[0, 7].each {|day| %>
+					<TD><IMG src='/rd.png' width='128' height='1'><BR><SPAN class='<%= day[:attrs][:color] %>'><%= day[:attrs][:color].upcase %></SPAN>
+<%				}
+				@days.each {|day|
+					if(day[:day].wday == 0) %>
+						<TR valign='top'><TD><IMG src='/rd.png' width='1' height='64'>
+<%					end %>
+							<TD><SPAN class='<%= [day[:attrs][:color], day[:attrs][:size]].join(' ').strip %>'><%= link_to(day[:day].day, new_schedule_path(day: day[:day])) %></SPAN>
+<%							day[:holidays].each {|holiday| %>
+								<SPAN class='cause'><%= holiday.cause %></SPAN>
+<%							}
+							day[:schedules].each {|schedule| %>
+								<BR><SPAN class='member'><%= schedule.member %></SPAN>:
+								<SPAN class='note'><%= link_to(schedule.note, schedule) %></SPAN>
+<%							}
+				} %>
+		</TABLE>
+
+<%= link_to "Manage Holidays", holidays_path %>
+
 <h1>Calendars</h1>
 
 <div id="calendars">
diff --git a/app/views/schedules/_form.html.erb b/app/views/schedules/_form.html.erb
index 29f58c0..3662bbd 100644
--- a/app/views/schedules/_form.html.erb
+++ b/app/views/schedules/_form.html.erb
@@ -13,7 +13,7 @@
 
   <div>
     <%= form.label :day, style: "display: block" %>
-    <%= form.date_field :day %>
+    <%= form.date_field(:day, value: params[:day]) %>
   </div>
 
   <div>
diff --git a/app/assets/stylesheets/base.css b/app/assets/stylesheets/base.css
index e69de29..08a0148 100644
--- a/app/assets/stylesheets/base.css
+++ b/app/assets/stylesheets/base.css
@@ -0,0 +1,25 @@
+a {
+	color: inherit;
+}
+span.sun {
+	color: red;
+}
+span.sat {
+	color: blue;
+}
+span.holiday {
+	color: red;
+}
+span.small {
+	font-size: 60%;
+}
+span.cause {
+	font-size: 80%;
+}
+span.member {
+	font-size: 80%;
+}
+span.note {
+	font-size: 80%;
+}
+以前に任天堂スイッチのデモでチラッと見たデモが面白そうだったのだが、体験版をダウンロードしたら、いきなり病みつきになってしまった。恐ろしいことに、Linux版が手元の環境で快適に動いてしまった。これはアブない。
[ツッコミを入れる]