diff --git a/app/controllers/modals_controller.rb b/app/controllers/modals_controller.rb
index 37b937e..7cdfe77 100644
--- a/app/controllers/modals_controller.rb
+++ b/app/controllers/modals_controller.rb
@@ -1,5 +1,6 @@
class ModalsController < ApplicationController
def open
+ @schedule = Schedule.new
render partial: "partials/modals_open", locals: { type: params[:type], close_button: params[:close_button] }
end
end
diff --git a/app/controllers/modbus_controller.rb b/app/controllers/modbus_controller.rb
index cdaf8d3..fb3f7a7 100644
--- a/app/controllers/modbus_controller.rb
+++ b/app/controllers/modbus_controller.rb
@@ -1,53 +1,11 @@
class ModbusController < ApplicationController
- def index
- @schedule = Schedule.all
- @modbus_running = Modbus::PollingService.running?
- end
-
- def destroy
- schedule = Schedule.find_by(id: params[:id])
-
- if schedule.destroy
- redirect_to schedule_edit_modbus_index_path, notice: "스케줄이 삭제되었습니다."
- else
- redirect_to schedule_edit_modbus_index_path, alert: "스케줄 삭제에 실패했습니다."
- end
- end
-
def start
Modbus::PollingService.start
- redirect_to modbus_index_path
+ redirect_to schedules_path
end
def stop
Modbus::PollingService.stop
- redirect_to modbus_index_path
- end
-
- def schedule_edit
- @schedule = Schedule.all
- end
-
- def schedule_edit_update
- error_time = []
-
- params[:schedule].each do |id, attributes|
- schedule = Schedule.find_by(id: id)
- unless schedule.update(
- hour: attributes[:hour],
- minute: attributes[:minute],
- is_active: attributes[:is_active],
- temperature: attributes[:temperature]
- )
-
- error_time << "#{schedule.hour}시 #{schedule.minute}분"
- end
- end
-
- if error_time.any?
- redirect_to modbus_index_path, alert: "#{error_time.join(', ')}의 온도 업데이트에 실패하였습니다."
- else
- redirect_to modbus_index_path, notice: "스케줄이 성공적으로 업데이트되었습니다."
- end
+ redirect_to schedules_path
end
end
diff --git a/app/controllers/schedules_controller.rb b/app/controllers/schedules_controller.rb
new file mode 100644
index 0000000..3a29118
--- /dev/null
+++ b/app/controllers/schedules_controller.rb
@@ -0,0 +1,68 @@
+class SchedulesController < ApplicationController
+ def index
+ @schedule = Schedule.order(:hour, :minute)
+ @modbus_running = Modbus::PollingService.running?
+ end
+
+ def new
+ @schedule = Schedule.new
+ end
+
+ def create
+ @schedule = Schedule.new(schedule_params)
+
+ if @schedule.save
+ redirect_to schedule_edit_schedules_path, notice: "스케쥴이 추가 되었습니다."
+ else
+ error_messages = @schedule.errors.full_messages.join(", ")
+ redirect_to schedule_edit_schedules_path, alert: "스케쥴 추가 실패: #{error_messages}"
+ end
+ end
+
+ def destroy
+ @schedule = Schedule.find_by(id: params[:id])
+
+ if @schedule.destroy
+ redirect_to schedule_edit_schedules_path, notice: "스케줄이 삭제되었습니다."
+ else
+ error_messages = @schedule.errors.full_messages.join(", ")
+ redirect_to schedule_edit_schedules_path, alert: "스케쥴 삭제 실패: #{error_messages}"
+ end
+ end
+
+ def schedule_edit
+ @schedule = Schedule.order(:hour, :minute)
+ end
+
+ def schedule_edit_update
+ error_messages = []
+
+ params[:schedule].each do |id, attributes|
+ schedule = Schedule.find_by(id: id)
+ next unless schedule
+
+ unless schedule.update(
+ hour: attributes[:hour],
+ minute: attributes[:minute],
+ is_active: attributes[:is_active],
+ temperature: attributes[:temperature]
+ )
+
+ error_detail = schedule.errors.full_messages.join(", ")
+ time_label = "#{attributes[:hour]}시 #{attributes[:minute]}분"
+ error_messages << "#{time_label} - #{error_detail}"
+ end
+ end
+
+ if error_messages.any?
+ redirect_to schedules_path, alert: error_messages.join("
")
+ else
+ redirect_to schedules_path, notice: "스케줄이 성공적으로 업데이트되었습니다."
+ end
+ end
+
+ private
+ def schedule_params
+ params.require(:schedule).permit(:hour, :minute, :is_active, :temperature)
+ end
+end
diff --git a/app/helpers/schedules_helper.rb b/app/helpers/schedules_helper.rb
new file mode 100644
index 0000000..86e05a4
--- /dev/null
+++ b/app/helpers/schedules_helper.rb
@@ -0,0 +1,2 @@
+module SchedulesHelper
+end
diff --git a/app/javascript/controllers/modals_controller.js b/app/javascript/controllers/modals_controller.js
index 014e1b0..844e913 100644
--- a/app/javascript/controllers/modals_controller.js
+++ b/app/javascript/controllers/modals_controller.js
@@ -45,10 +45,7 @@ export default class extends Controller {
setTimeout(() => {
const modalsFrame = document.querySelector("turbo-frame#modals")
- if (modalsFrame) {
- modalsFrame.innerHTML = ""
- window.location.reload()
- }
+ modalsFrame.innerHTML = ""
}, 300) // 닫힘 애니메이션 시간과 동일하게 300ms 지연 후 모달 제거
}
}
diff --git a/app/models/schedule.rb b/app/models/schedule.rb
index 1bc1090..b8d23ca 100644
--- a/app/models/schedule.rb
+++ b/app/models/schedule.rb
@@ -1,9 +1,9 @@
class Schedule < ApplicationRecord
- validates :hour, presence: true
- validates :minute, presence: true
+ validates :hour, presence: { message: "시간을 입력하세요" }
+ validates :minute, presence: { message: "분을 입력하세요" }
validates :temperature,
- presence: true,
- numericality: true,
- format: { with: /\A\d+(\.\d)?\z/ }
- validates :minute, uniqueness: { scope: :hour }
+ presence: { message: "온도를 입력하세요" },
+ numericality: { message: "온도는 숫자여야 합니다" },
+ format: { with: /\A\d+(\.\d)?\z/, message: "온도는 소수점 한 자리까지 입력 가능합니다" }
+ validates :minute, uniqueness: { scope: :hour, message: "같은 시:분의 스케쥴이 이미 존재합니다" }
end
diff --git a/app/services/modbus/polling_service.rb b/app/services/modbus/polling_service.rb
index 9b5d5c0..79f153d 100644
--- a/app/services/modbus/polling_service.rb
+++ b/app/services/modbus/polling_service.rb
@@ -29,9 +29,8 @@ module Modbus
begin
now = Time.now
current = [ now.hour, now.min ]
-
if current != last_logged
- puts "시간 변경 감지됨: #{current.join(':')}"
+ puts "시간 변경 감지됨: #{format('%02d:%02d', current[0], current[1])}"
schedule = Schedule.find_by(hour: current[0], minute: current[1])
apply_schedule(schedule) if schedule
last_logged = current
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 14ced63..ec6429d 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -31,11 +31,11 @@
<% if flash[:notice] %>
- <%= flash[:notice] %>
+ <%= raw flash[:notice] %>
<% elsif flash[:alert] %>
- <%= flash[:alert] %>
+ <%= raw flash[:alert] %>
<% end %>
<%= yield %>
diff --git a/app/views/partials/modals/_add_schedule.html.erb b/app/views/partials/modals/_add_schedule.html.erb
index 4f0d83f..828d52f 100644
--- a/app/views/partials/modals/_add_schedule.html.erb
+++ b/app/views/partials/modals/_add_schedule.html.erb
@@ -1 +1,39 @@
-스케쥴 추가
+<%= form_with model: @schedule, method: :post, data: { turbo: false }, class: 'flex flex-col h-full divide-y divide-border-table-border' do |form| %>
+
+
+
+
+ | 시간 |
+ 분 |
+ 사용여부 |
+ 온도 |
+
+
+
+
+ |
+ <%= form.select :hour,
+ options_for_select((0..23).map { |h| [h.to_s.rjust(2, '0'), h] }, @schedule.hour),
+ {},
+ class: "input-style" %>
+ |
+
+ <%= form.number_field :minute,
+ min: 0, max: 59, step: 1, inputmode: "decimal", class: "input-style" %>
+ |
+
+ <%= check_box_tag "schedule[is_active]", "1", true %>
+ |
+
+ <%= form.number_field :temperature,
+ step: "0.1", inputmode: "decimal", class: "input-style" %>
+ |
+
+
+
+
+
+
+ <%= submit_tag "추가", class: "btn bg-primary" %>
+
+<% end %>
\ No newline at end of file
diff --git a/app/views/modbus/index.html.erb b/app/views/schedules/index.html.erb
similarity index 93%
rename from app/views/modbus/index.html.erb
rename to app/views/schedules/index.html.erb
index 9268ef3..444599f 100644
--- a/app/views/modbus/index.html.erb
+++ b/app/views/schedules/index.html.erb
@@ -37,6 +37,6 @@
- <%= link_to "수정", "/modbus/schedule_edit", class: "btn bg-default-slate" %>
+ <%= link_to "수정", schedule_edit_schedules_path, class: "btn bg-default-slate" %>
diff --git a/app/views/modbus/schedule_edit.html.erb b/app/views/schedules/schedule_edit.html.erb
similarity index 89%
rename from app/views/modbus/schedule_edit.html.erb
rename to app/views/schedules/schedule_edit.html.erb
index ef26832..737d0ba 100644
--- a/app/views/modbus/schedule_edit.html.erb
+++ b/app/views/schedules/schedule_edit.html.erb
@@ -1,4 +1,4 @@
-<%= form_with url: schedule_edit_update_modbus_index_path, method: :post, class: 'flex flex-col h-full divide-y divide-border-table-border' do %>
+<%= form_with url: schedule_edit_update_schedules_path, method: :post, class: 'flex flex-col h-full divide-y divide-border-table-border' do %>
@@ -22,7 +22,7 @@
| <%= check_box_tag "schedule[#{s.id}][is_active]", "1", s.is_active == true || s.is_active == 1 %> |
<%= number_field_tag "schedule[#{s.id}][temperature]", s.temperature, step: "0.1", inputmode: "decimal", class: "input-style" %> |
- <%= link_to "삭제", modbus_path(s),
+ <%= link_to "삭제", schedule_path(s),
data: {
turbo_method: :delete,
turbo_confirm: "정말 삭제하시겠습니까?"
diff --git a/config/routes.rb b/config/routes.rb
index d60f13e..3d06b4e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -10,7 +10,15 @@ Rails.application.routes.draw do
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
# Defines the root path route ("/")
- root "modbus#index"
+
+ root "schedules#index"
+
+ resources :schedules do
+ collection do
+ get "schedule_edit"
+ post "schedule_edit_update"
+ end
+ end
resources :modbus do
collection do
diff --git a/test/controllers/schedules_controller_test.rb b/test/controllers/schedules_controller_test.rb
new file mode 100644
index 0000000..be3f5ce
--- /dev/null
+++ b/test/controllers/schedules_controller_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class SchedulesControllerTest < ActionDispatch::IntegrationTest
+ # test "the truth" do
+ # assert true
+ # end
+end
|