From 32031064a12ce6c22c65c1f57fb3e4b8ccce074b Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sat, 12 Oct 2024 12:08:51 +0900 Subject: [PATCH 1/9] =?UTF-8?q?remark=E3=81=AE=E5=86=85=E5=AE=B9=E3=81=8C?= =?UTF-8?q?=E6=9C=AA=E5=85=A5=E5=8A=9B=E3=81=AE=E6=99=82=E3=81=AB=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=81=8C=E5=87=BA=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/remarks_controller.rb | 21 +++++++++++++---- app/models/remark.rb | 5 +++- app/views/meeting_rooms/show.html.erb | 24 +++++++++++-------- app/views/remarks/_comment.html.erb | 28 ++++++++++++----------- app/views/remarks/_form.html.erb | 9 ++++++++ app/views/remarks/create.turbo_stream.erb | 4 ++-- 6 files changed, 61 insertions(+), 30 deletions(-) diff --git a/app/controllers/remarks_controller.rb b/app/controllers/remarks_controller.rb index 0035858..ad931b6 100644 --- a/app/controllers/remarks_controller.rb +++ b/app/controllers/remarks_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class RemarksController < ApplicationController - before_action :set_meeting_room, only: %i[new edit update destroy] + before_action :set_meeting_room, only: %i[new create edit update destroy] before_action :set_remark, only: %i[edit update destroy] def new @remark = Remark.build(remark_type: params[:remark_type]) @@ -12,7 +12,11 @@ def edit; end def create @remark = current_user.remarks.build(remark_params) if @remark.save - # flash.now.notice = 'Remark was successfully created.' + respond_to do |format| + format.html { redirect_to meeting_room_remarks_path(@meeting_room), notice: "Date was successfully created." } + # format.turbo_stream { flash.now[:notice] = "Date was successfully created." } + format.turbo_stream + end else render :new, status: :unprocessable_entity end @@ -20,7 +24,11 @@ def create def update if @remark.update(remark_params) - redirect_to meeting_room_url(@meeting_room), notice: 'Remark was successfully updated.', status: :see_other + respond_to do |format| + format.html { redirect_to meeting_room_remarks_path(@meeting_room), notice: "Item was successfully updated." } + # format.turbo_stream { flash.now[:notice] = "Item was successfully updated." } + format.turbo_stream + end else render :edit, status: :unprocessable_entity end @@ -28,7 +36,12 @@ def update def destroy @remark.destroy! - # flash.now.notice = 'Remark was successfully destroyed.', status: :see_other + + respond_to do |format| + format.html { redirect_to meeting_room_remarks_path(@meeting_room), notice: "Item was successfully destroyed.", status: :see_other } + # format.turbo_stream { flash.now[:notice] = "Date was successfully destroyed." } + format.turbo_stream + end end private diff --git a/app/models/remark.rb b/app/models/remark.rb index 5c371d2..6f2a7a1 100644 --- a/app/models/remark.rb +++ b/app/models/remark.rb @@ -4,7 +4,10 @@ class Remark < ApplicationRecord belongs_to :meeting_room belongs_to :user - enum :remark_type, { proposal: 0, comment: 1 } + validates :remark_type, presence: true + validates :content, presence: true + + enum :remark_type, { proposal: 0, comment: 1 }, validate:true def proposal? remark_type == 'proposal' diff --git a/app/views/meeting_rooms/show.html.erb b/app/views/meeting_rooms/show.html.erb index 22b9cfe..8eb753b 100644 --- a/app/views/meeting_rooms/show.html.erb +++ b/app/views/meeting_rooms/show.html.erb @@ -21,22 +21,26 @@

候補

-
- <% @remarks.filter(&:proposal?).each do |remark| %> - <%= render 'remarks/proposal', remark: %> - <% end %> -
+
+ <% @remarks.filter(&:proposal?).each do |remark| %> + <%= turbo_frame_tag "proposal" do %> + <%= render 'remarks/proposal', remark: %> + <% end %> + <% end %> +

コメント

-
- <% @remarks.filter(&:comment?).each do |remark| %> - <%= render 'remarks/comment', remark: %> - <% end %> -
+
+ <% @remarks.filter(&:comment?).each do |remark| %> + <%= turbo_frame_tag "comment" do %> + <%= render 'remarks/comment', remark: %> + <% end %> + <% end %> +
diff --git a/app/views/remarks/_comment.html.erb b/app/views/remarks/_comment.html.erb index 7154f7d..fc09f11 100644 --- a/app/views/remarks/_comment.html.erb +++ b/app/views/remarks/_comment.html.erb @@ -1,16 +1,18 @@ -<%= turbo_frame_tag remark, class: 'border-b border-dashed border-orange-950/10 pb-4' do %> -
-
- <%= image_tag remark.user.icon, class: 'icon mr-2' %> - <%= l remark.created_at, format: :long %> -
- <% if remark.editable?(current_user) %> - <%= link_to edit_meeting_room_remark_path(remark.meeting_room, remark), class: 'mr-2' do %> - +<%= turbo_frame_tag remark do %> +
+
+
+ <%= image_tag remark.user.icon, class: 'icon mr-2' %> + <%= l remark.created_at, format: :long %> +
+ <% if remark.editable?(current_user) %> + <%= link_to edit_meeting_room_remark_path(remark.meeting_room, remark), class: 'mr-2' do %> + + <% end %> <% end %> - <% end %> -
-
- <%= format_line_break(remark.content) %> +
+
+ <%= format_line_break(remark.content) %> +
<% end %> diff --git a/app/views/remarks/_form.html.erb b/app/views/remarks/_form.html.erb index 9ed2e41..93e773d 100644 --- a/app/views/remarks/_form.html.erb +++ b/app/views/remarks/_form.html.erb @@ -1,5 +1,14 @@ <%= turbo_frame_tag remark do %> <%= form_with model: [meeting_room, remark] do |form| %> + <% if remark.errors.any? %> +
+
    + <% remark.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %>
<%= form.hidden_field :remark_type %> <%= form.hidden_field :meeting_room_id, value: meeting_room.id %> diff --git a/app/views/remarks/create.turbo_stream.erb b/app/views/remarks/create.turbo_stream.erb index b71bba7..fee3e80 100644 --- a/app/views/remarks/create.turbo_stream.erb +++ b/app/views/remarks/create.turbo_stream.erb @@ -1,7 +1,7 @@ <% if @remark.proposal? %> - <%= turbo_stream.prepend "proposals", partial: "proposal", locals: {remark: @remark} %> + <%= turbo_stream.prepend "proposal", partial: "remarks/proposal", locals: {remark: @remark} %> <% else %> - <%= turbo_stream.prepend "comments", partial: "comment", locals: {remark: @remark} %> + <%= turbo_stream.prepend "comment", partial: "remarks/comment", locals: {remark: @remark} %> <% end %> <%= turbo_stream.update Remark.new, "" %> From 38cb3ae10fb9e999dd785436ce6d93426d4e33ae Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sat, 12 Oct 2024 12:32:50 +0900 Subject: [PATCH 2/9] =?UTF-8?q?User=E9=96=A2=E9=80=A3=E3=81=AE=E3=83=90?= =?UTF-8?q?=E3=83=AA=E3=83=87=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E3=81=97=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=8C?= =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E3=81=95=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 4 ++-- app/models/user.rb | 5 ++++- app/views/users/edit.html.erb | 9 +++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a650d3a..c732c0e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -6,7 +6,7 @@ def edit; end def update if @user.update(user_params) - redirect_to meal_plans_path, notice: 'User was successfully updated.', status: :see_other + redirect_to meal_plans_path, notice: 'User was successfully updated.' else render :edit, status: :unprocessable_entity end @@ -16,7 +16,7 @@ def destroy current_user.destroy! current_user.family.destroy_family_having_no_user reset_session - redirect_to root_path, notice: 'User was successfully deleted.' + redirect_to root_path, notice: 'User was successfully deleted.', status: :see_other end private diff --git a/app/models/user.rb b/app/models/user.rb index 2f47aa7..46d8d66 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,9 @@ class User < ApplicationRecord belongs_to :family has_many :remarks, dependent: :nullify + validates :name, presence: true + validates :name, length: {maximum: 20} + enum :icon, { 'man.png': 0, 'king.png': 1, @@ -14,7 +17,7 @@ class User < ApplicationRecord 'eagle.png': 6, 'apple.png': 7, 'chimpanzee.png': 8 - } + }, validate: true ICON = { man: 'man.png', diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 74d692f..cfb3ad6 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -7,6 +7,15 @@ <% end %>
<%= form_with(model: @user, class: 'contents') do |form| %> + <% if @user.errors.any? %> +
+
    + <% @user.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %>
<%= form.label :name, class: 'block mb-1' %> <%= form.text_field :name, From 89f91413e3f2b08d36bcae2e74328e952655990b Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sat, 12 Oct 2024 12:40:39 +0900 Subject: [PATCH 3/9] =?UTF-8?q?remark=E3=81=AEupdate=E3=82=A2=E3=82=AF?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/remarks_controller.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/remarks_controller.rb b/app/controllers/remarks_controller.rb index ad931b6..4362927 100644 --- a/app/controllers/remarks_controller.rb +++ b/app/controllers/remarks_controller.rb @@ -13,7 +13,7 @@ def create @remark = current_user.remarks.build(remark_params) if @remark.save respond_to do |format| - format.html { redirect_to meeting_room_remarks_path(@meeting_room), notice: "Date was successfully created." } + format.html { redirect_to meeting_room_path(@meeting_room), notice: 'Date was successfully created.' } # format.turbo_stream { flash.now[:notice] = "Date was successfully created." } format.turbo_stream end @@ -25,9 +25,8 @@ def create def update if @remark.update(remark_params) respond_to do |format| - format.html { redirect_to meeting_room_remarks_path(@meeting_room), notice: "Item was successfully updated." } + format.html { redirect_to meeting_room_path(@meeting_room), notice: 'Item was successfully updated.' } # format.turbo_stream { flash.now[:notice] = "Item was successfully updated." } - format.turbo_stream end else render :edit, status: :unprocessable_entity @@ -38,7 +37,7 @@ def destroy @remark.destroy! respond_to do |format| - format.html { redirect_to meeting_room_remarks_path(@meeting_room), notice: "Item was successfully destroyed.", status: :see_other } + format.html { redirect_to meeting_room_path(@meeting_room), notice: 'Item was successfully destroyed.', status: :see_other } # format.turbo_stream { flash.now[:notice] = "Date was successfully destroyed." } format.turbo_stream end From 24316a34166639f6d41538a248a7d48bf9b38b69 Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sat, 12 Oct 2024 12:40:50 +0900 Subject: [PATCH 4/9] =?UTF-8?q?lint=E3=82=92=E5=AE=9F=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/remark.rb | 2 +- app/models/user.rb | 2 +- app/views/meeting_rooms/show.html.erb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/remark.rb b/app/models/remark.rb index 6f2a7a1..badce5b 100644 --- a/app/models/remark.rb +++ b/app/models/remark.rb @@ -7,7 +7,7 @@ class Remark < ApplicationRecord validates :remark_type, presence: true validates :content, presence: true - enum :remark_type, { proposal: 0, comment: 1 }, validate:true + enum :remark_type, { proposal: 0, comment: 1 }, validate: true def proposal? remark_type == 'proposal' diff --git a/app/models/user.rb b/app/models/user.rb index 46d8d66..125a67f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,7 +5,7 @@ class User < ApplicationRecord has_many :remarks, dependent: :nullify validates :name, presence: true - validates :name, length: {maximum: 20} + validates :name, length: { maximum: 20 } enum :icon, { 'man.png': 0, diff --git a/app/views/meeting_rooms/show.html.erb b/app/views/meeting_rooms/show.html.erb index 8eb753b..37d9a8e 100644 --- a/app/views/meeting_rooms/show.html.erb +++ b/app/views/meeting_rooms/show.html.erb @@ -23,7 +23,7 @@
<% @remarks.filter(&:proposal?).each do |remark| %> - <%= turbo_frame_tag "proposal" do %> + <%= turbo_frame_tag 'proposal' do %> <%= render 'remarks/proposal', remark: %> <% end %> <% end %> @@ -36,7 +36,7 @@
<% @remarks.filter(&:comment?).each do |remark| %> - <%= turbo_frame_tag "comment" do %> + <%= turbo_frame_tag 'comment' do %> <%= render 'remarks/comment', remark: %> <% end %> <% end %> From f01e9f6a709fb9e649a36608d030cb825334522e Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sat, 12 Oct 2024 16:12:11 +0900 Subject: [PATCH 5/9] =?UTF-8?q?meal=E3=81=AE=E3=83=90=E3=83=AA=E3=83=87?= =?UTF-8?q?=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E8=BF=BD=E5=8A=A0=E3=80=81?= =?UTF-8?q?view=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/meal_plans_controller.rb | 3 +++ app/controllers/meeting_rooms_controller.rb | 10 ++++++++-- app/models/meal.rb | 2 +- app/models/meal_plan.rb | 19 ++++++++++++++----- app/views/meal_plans/_form.html.erb | 9 +++++++++ app/views/meal_plans/_meal_plan.html.erb | 19 ++++++------------- 6 files changed, 41 insertions(+), 21 deletions(-) diff --git a/app/controllers/meal_plans_controller.rb b/app/controllers/meal_plans_controller.rb index 4105640..ff977d3 100644 --- a/app/controllers/meal_plans_controller.rb +++ b/app/controllers/meal_plans_controller.rb @@ -22,9 +22,11 @@ def create @meal_plan = current_user.family.meal_plans.find_or_initialize_by(meal_date: meal_plan_params[:meal_date]) @meal_plan.assign_attributes(meal_plan_params) + if @meal_plan.save redirect_to @meal_plan, notice: '献立を作成しました。' else + @meal_plan.meals_build render :new, status: :unprocessable_entity end end @@ -33,6 +35,7 @@ def update if @meal_plan.update_meal_plan(meal_plan_params) redirect_to @meal_plan else + @meal_plan.meals_build render :edit, status: :unprocessable_entity end end diff --git a/app/controllers/meeting_rooms_controller.rb b/app/controllers/meeting_rooms_controller.rb index ec0e81a..eaaf50a 100644 --- a/app/controllers/meeting_rooms_controller.rb +++ b/app/controllers/meeting_rooms_controller.rb @@ -7,9 +7,15 @@ def show end def create - meal_plan = current_user.family.meal_plans.find_or_create_by(id: params[:meal_plan]) do |new_meal_plan| - new_meal_plan.meal_date = params[:meal_date] + # meal_plan = current_user.family.meal_plans.find_or_create_by(id: params[:meal_plan]) do |new_meal_plan| + # new_meal_plan.meal_date = params[:meal_date] + # end + meal_plan = current_user.family.meal_plans.find_by(id: params[:meal_plan]) + if meal_plan.nil? + meal_plan = current_user.family.meal_plans.new(meal_date: params[:meal_date]) + meal_plan.save(validate: false) end + meeting_room = current_user.family.meeting_rooms.find_or_create_by(meal_plan:) redirect_to meeting_room_path(meeting_room) end diff --git a/app/models/meal.rb b/app/models/meal.rb index 77b392a..d7f1f41 100644 --- a/app/models/meal.rb +++ b/app/models/meal.rb @@ -3,7 +3,7 @@ class Meal < ApplicationRecord belongs_to :meal_plan - enum :timing, { breakfast: 0, lunch: 1, dinner: 2 } + enum :timing, { breakfast: 0, lunch: 1, dinner: 2 }, validate: true validates :timing, presence: true, uniqueness: { scope: :meal_plan_id } end diff --git a/app/models/meal_plan.rb b/app/models/meal_plan.rb index 34819c2..c26efd6 100644 --- a/app/models/meal_plan.rb +++ b/app/models/meal_plan.rb @@ -6,18 +6,16 @@ class MealPlan < ApplicationRecord has_many :meals, dependent: :destroy validates :meal_date, presence: true, uniqueness: { scope: :family_id } + validate :has_at_least_a_meal accepts_nested_attributes_for :meals, allow_destroy: true, reject_if: :reject_timing - def reject_timing(attributes) - attributes.except(:timing).values.all?(&:blank?) - end - def update_meal_plan(attributes) if attributes[:meals_attributes].keys.size == 1 create_or_update_meals_by_proposal(attributes) else - update(attributes) + assign_attributes(attributes) + return unless save destroy_unnecessary_meals(attributes) end end @@ -53,4 +51,15 @@ def meals_sort_by_timing def start_time meal_date end + def has_at_least_a_meal + if meals.map{|meal|meal[:name]}.join.blank? + errors.add(:base, "料理名を最低1つ入力してください") + end + end + + private + + def reject_timing(attributes) + attributes.except(:timing).values.all?(&:blank?) + end end diff --git a/app/views/meal_plans/_form.html.erb b/app/views/meal_plans/_form.html.erb index 1592d29..b7d39b4 100644 --- a/app/views/meal_plans/_form.html.erb +++ b/app/views/meal_plans/_form.html.erb @@ -1,4 +1,13 @@ <%= form_with(model: meal_plan, class: 'contents') do |form| %> + <% if meal_plan.errors.any? %> +
+
    + <% meal_plan.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %>

<%= l meal_plan.meal_date, format: :long %>

<%= form.hidden_field :meal_date %> diff --git a/app/views/meal_plans/_meal_plan.html.erb b/app/views/meal_plans/_meal_plan.html.erb index 1514928..d7643d8 100644 --- a/app/views/meal_plans/_meal_plan.html.erb +++ b/app/views/meal_plans/_meal_plan.html.erb @@ -2,24 +2,17 @@

<%= l meal_plan.meal_date, format: :long %>

- <% if @meal_plan.meals.present? %> + <% if meal_plan.meals.present? %>
- <%= link_to '編集', edit_meal_plan_path(@meal_plan), class: 'text-l inline-block rounded-full py-2 px-4 flex items-center bg-orange-100/80' %> + <%= link_to '編集', edit_meal_plan_path(meal_plan), class: 'text-l inline-block rounded-full py-2 px-4 flex items-center bg-orange-100/80' %>
<% end %>
- - <% @meal_plan.meals_sort_by_timing.each do |meal| %> + <% meal_plan.meals_sort_by_timing.each do |meal| %> <%= render 'meals/meal', meal: %> <% end %> - <% if @meal_plan.meals.blank? %> -
- <%= link_to '', new_meal_plan_path(meal_date: @meal_plan.meal_date), data: { turbo_frame: '_top' }, class: 'plus_icon' %> -
- <% else %> -
- <%= button_to 'この献立を削除する', @meal_plan, method: :delete, class: 'text-orange-950/70' %> -
- <% end %>
<% end %> +
+ <%= button_to 'この献立を削除する', meal_plan, method: :delete, class: 'text-orange-950/70' %> +
From 430c05e31efe59fa05a5ac5c1013ad186237e552 Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sat, 12 Oct 2024 21:29:46 +0900 Subject: [PATCH 6/9] =?UTF-8?q?lint=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .rubocop.yml | 8 +++++++- app/assets/stylesheets/application.css | 1 - app/controllers/meal_plans_controller.rb | 1 - app/controllers/meeting_rooms_controller.rb | 4 ---- app/models/meal_plan.rb | 13 ++++++++----- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 69dbe2f..69d43b3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -21,7 +21,7 @@ Rails/BulkChangeTable: Enabled: false Metrics/AbcSize: - Max: 20 + Max: 25 Rails/I18nLocaleTexts: Enabled: false @@ -37,3 +37,9 @@ RSpec/MultipleExpectations: Layout/LineLength: Max: 200 + +Style/ReturnNilInPredicateMethodDefinition: + Enabled: false + +Rails/Pluck: + Enabled: false diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index b745a0b..eef8330 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -49,7 +49,6 @@ input:-webkit-autofill:focus { border-color: #ff9142; position: relative; border-radius: 9999px; - z-index: 10; } .plus_icon::before { diff --git a/app/controllers/meal_plans_controller.rb b/app/controllers/meal_plans_controller.rb index ff977d3..3538f44 100644 --- a/app/controllers/meal_plans_controller.rb +++ b/app/controllers/meal_plans_controller.rb @@ -22,7 +22,6 @@ def create @meal_plan = current_user.family.meal_plans.find_or_initialize_by(meal_date: meal_plan_params[:meal_date]) @meal_plan.assign_attributes(meal_plan_params) - if @meal_plan.save redirect_to @meal_plan, notice: '献立を作成しました。' else diff --git a/app/controllers/meeting_rooms_controller.rb b/app/controllers/meeting_rooms_controller.rb index eaaf50a..e4017de5 100644 --- a/app/controllers/meeting_rooms_controller.rb +++ b/app/controllers/meeting_rooms_controller.rb @@ -7,15 +7,11 @@ def show end def create - # meal_plan = current_user.family.meal_plans.find_or_create_by(id: params[:meal_plan]) do |new_meal_plan| - # new_meal_plan.meal_date = params[:meal_date] - # end meal_plan = current_user.family.meal_plans.find_by(id: params[:meal_plan]) if meal_plan.nil? meal_plan = current_user.family.meal_plans.new(meal_date: params[:meal_date]) meal_plan.save(validate: false) end - meeting_room = current_user.family.meeting_rooms.find_or_create_by(meal_plan:) redirect_to meeting_room_path(meeting_room) end diff --git a/app/models/meal_plan.rb b/app/models/meal_plan.rb index c26efd6..393925f 100644 --- a/app/models/meal_plan.rb +++ b/app/models/meal_plan.rb @@ -6,7 +6,7 @@ class MealPlan < ApplicationRecord has_many :meals, dependent: :destroy validates :meal_date, presence: true, uniqueness: { scope: :family_id } - validate :has_at_least_a_meal + validate :at_least_a_meal? accepts_nested_attributes_for :meals, allow_destroy: true, reject_if: :reject_timing @@ -16,6 +16,7 @@ def update_meal_plan(attributes) else assign_attributes(attributes) return unless save + destroy_unnecessary_meals(attributes) end end @@ -51,10 +52,12 @@ def meals_sort_by_timing def start_time meal_date end - def has_at_least_a_meal - if meals.map{|meal|meal[:name]}.join.blank? - errors.add(:base, "料理名を最低1つ入力してください") - end + + def at_least_a_meal? + meal_names = meals.map { |meal| meal[:name] } + return if meal_names.any?(&:present?) + + errors.add(:base, '料理名を最低1つ入力してください') end private From 23f9b648d2427758321d50b68694eb6aa3809ff2 Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sat, 12 Oct 2024 22:29:55 +0900 Subject: [PATCH 7/9] =?UTF-8?q?timing=5Fform=E3=82=92=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E3=80=81timing=E3=81=AB=E5=88=9D=E6=9C=9F=E7=8A=B6=E6=85=8B?= =?UTF-8?q?=E3=82=92=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/meals/_timing_form.html.erb | 11 ++++++++++- app/views/meals/new.html.erb | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/views/meals/_timing_form.html.erb b/app/views/meals/_timing_form.html.erb index b34b7f5..3066d86 100644 --- a/app/views/meals/_timing_form.html.erb +++ b/app/views/meals/_timing_form.html.erb @@ -1,11 +1,20 @@ <%= form_with(model: meal_plan, url: meal_plan_path(meal_plan), method: 'patch') do |form| %> + <% if meal_plan.errors.any? %> +
+
    + <% meal_plan.errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+ <% end %>
<%= form.hidden_field :meal_date %> <%= form.fields_for :meals, meal do |meal_form| %> <%= meal_form.hidden_field :id %>
<% Meal.timings.keys.each do |timing| %> - <%= meal_form.radio_button :timing, timing, class: 'checkbox-3' %> + <%= meal_form.radio_button :timing, timing, checked: (timing == 'lunch'), class: 'checkbox-3' %> <%= meal_form.label "timing_#{timing}", Meal.human_attribute_name("timing.#{timing}") %> <% end %>
diff --git a/app/views/meals/new.html.erb b/app/views/meals/new.html.erb index 42336b5..314fd67 100644 --- a/app/views/meals/new.html.erb +++ b/app/views/meals/new.html.erb @@ -1,7 +1,7 @@
<%= link_to '会議へ戻る', meeting_room_path(@meal_plan.meeting_room), - class: 'rounded-full py-2 px-3 bg-orange-100/70' %> + class: 'rounded-full py-3 px-3 bg-orange-100/70' %>

From 3d23038dc4f7502d27e109eef95a7bda3350e5dc Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sun, 13 Oct 2024 10:15:47 +0900 Subject: [PATCH 8/9] =?UTF-8?q?meal=5Fplan=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E6=99=82=E3=81=AB=E3=83=88=E3=83=83=E3=83=97=E3=81=AB=E6=88=BB?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86autoscroll=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/meal_plans/_meal_plan.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/meal_plans/_meal_plan.html.erb b/app/views/meal_plans/_meal_plan.html.erb index d7643d8..8a3d259 100644 --- a/app/views/meal_plans/_meal_plan.html.erb +++ b/app/views/meal_plans/_meal_plan.html.erb @@ -1,4 +1,4 @@ -<%= turbo_frame_tag meal_plan do %> +<%= turbo_frame_tag meal_plan, autoscroll: true, data: { autoscroll_block: "start" } do %>

<%= l meal_plan.meal_date, format: :long %>

From 9905fb764a83f6724c5b8cfc8a657064899c1879 Mon Sep 17 00:00:00 2001 From: motohiro-mm Date: Sun, 13 Oct 2024 11:10:54 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/meal_plans/_meal_plan.html.erb | 2 +- spec/factories/meal_plans.rb | 4 ++++ spec/system/meal_plans_spec.rb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/views/meal_plans/_meal_plan.html.erb b/app/views/meal_plans/_meal_plan.html.erb index 8a3d259..fa5143d 100644 --- a/app/views/meal_plans/_meal_plan.html.erb +++ b/app/views/meal_plans/_meal_plan.html.erb @@ -1,4 +1,4 @@ -<%= turbo_frame_tag meal_plan, autoscroll: true, data: { autoscroll_block: "start" } do %> +<%= turbo_frame_tag meal_plan, autoscroll: true, data: { autoscroll_block: 'start' } do %>

<%= l meal_plan.meal_date, format: :long %>

diff --git a/spec/factories/meal_plans.rb b/spec/factories/meal_plans.rb index 543a4a5..e5281eb 100644 --- a/spec/factories/meal_plans.rb +++ b/spec/factories/meal_plans.rb @@ -4,5 +4,9 @@ factory :meal_plan do sequence(:meal_date) { |n| Time.zone.today.days_since(n) } family + + trait :skip_validation do + to_create { |meal_plan| meal_plan.save(validate: false) } + end end end diff --git a/spec/system/meal_plans_spec.rb b/spec/system/meal_plans_spec.rb index 9951913..2d93b2d 100644 --- a/spec/system/meal_plans_spec.rb +++ b/spec/system/meal_plans_spec.rb @@ -4,7 +4,7 @@ RSpec.describe 'MealPlans', type: :system do let(:user) { create(:user) } - let(:meal_plan) { create(:meal_plan, family: user.family) } + let(:meal_plan) { create(:meal_plan, :skip_validation, family: user.family) } let!(:meal_breakfast) { create(:meal, :breakfast, meal_plan:) } let!(:meal_lunch) { create(:meal, :lunch, meal_plan:) } let!(:meal_dinner) { create(:meal, :dinner, meal_plan:) }