Skip to content

Commit

Permalink
Merge pull request #123 from motohiro-mm/feature/add_error_message
Browse files Browse the repository at this point in the history
エラーメッセージを表示するようにした
  • Loading branch information
motohiro-mm authored Oct 13, 2024
2 parents 2a382fa + 9905fb7 commit 7ac55ce
Show file tree
Hide file tree
Showing 21 changed files with 138 additions and 60 deletions.
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Rails/BulkChangeTable:
Enabled: false

Metrics/AbcSize:
Max: 20
Max: 25

Rails/I18nLocaleTexts:
Enabled: false
Expand All @@ -37,3 +37,9 @@ RSpec/MultipleExpectations:

Layout/LineLength:
Max: 200

Style/ReturnNilInPredicateMethodDefinition:
Enabled: false

Rails/Pluck:
Enabled: false
1 change: 0 additions & 1 deletion app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ input:-webkit-autofill:focus {
border-color: #ff9142;
position: relative;
border-radius: 9999px;
z-index: 10;
}

.plus_icon::before {
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/meal_plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def create
if @meal_plan.save
redirect_to @meal_plan, notice: '献立を作成しました。'
else
@meal_plan.meals_build
render :new, status: :unprocessable_entity
end
end
Expand All @@ -33,6 +34,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
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/meeting_rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ 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_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)
Expand Down
20 changes: 16 additions & 4 deletions app/controllers/remarks_controller.rb
Original file line number Diff line number Diff line change
@@ -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])
Expand All @@ -12,23 +12,35 @@ 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_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
end

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_path(@meeting_room), notice: 'Item was successfully updated.' }
# format.turbo_stream { flash.now[:notice] = "Item was successfully updated." }
end
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@remark.destroy!
# flash.now.notice = 'Remark was successfully destroyed.', status: :see_other

respond_to do |format|
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
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/meal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 17 additions & 5 deletions app/models/meal_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ class MealPlan < ApplicationRecord
has_many :meals, dependent: :destroy

validates :meal_date, presence: true, uniqueness: { scope: :family_id }
validate :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
Expand Down Expand Up @@ -53,4 +52,17 @@ def meals_sort_by_timing
def start_time
meal_date
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

def reject_timing(attributes)
attributes.except(:timing).values.all?(&:blank?)
end
end
5 changes: 4 additions & 1 deletion app/models/remark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,7 +17,7 @@ class User < ApplicationRecord
'eagle.png': 6,
'apple.png': 7,
'chimpanzee.png': 8
}
}, validate: true

ICON = {
man: 'man.png',
Expand Down
9 changes: 9 additions & 0 deletions app/views/meal_plans/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<%= form_with(model: meal_plan, class: 'contents') do |form| %>
<% if meal_plan.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<ul>
<% meal_plan.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<h1 class="font-bold text-2xl"><%= l meal_plan.meal_date, format: :long %></h1>
<%= form.hidden_field :meal_date %>
Expand Down
21 changes: 7 additions & 14 deletions app/views/meal_plans/_meal_plan.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
<%= turbo_frame_tag meal_plan do %>
<%= turbo_frame_tag meal_plan, autoscroll: true, data: { autoscroll_block: 'start' } do %>
<div class="my-5">
<div class="flex justify-between">
<h1 class="font-bold text-2xl"><%= l meal_plan.meal_date, format: :long %></h1>
<% if @meal_plan.meals.present? %>
<% if meal_plan.meals.present? %>
<div class="flex items-center">
<%= 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' %>
</div>
<% end %>
</div>

<% @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? %>
<div class="text-center mt-4 p-4">
<%= link_to '', new_meal_plan_path(meal_date: @meal_plan.meal_date), data: { turbo_frame: '_top' }, class: 'plus_icon' %>
</div>
<% else %>
<div class="text-right">
<%= button_to 'この献立を削除する', @meal_plan, method: :delete, class: 'text-orange-950/70' %>
</div>
<% end %>
</div>
<% end %>
<div class="text-right">
<%= button_to 'この献立を削除する', meal_plan, method: :delete, class: 'text-orange-950/70' %>
</div>
11 changes: 10 additions & 1 deletion app/views/meals/_timing_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<%= form_with(model: meal_plan, url: meal_plan_path(meal_plan), method: 'patch') do |form| %>
<% if meal_plan.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<ul>
<% meal_plan.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-6">
<%= form.hidden_field :meal_date %>
<%= form.fields_for :meals, meal do |meal_form| %>
<%= meal_form.hidden_field :id %>
<div class="checkbox-3 flex justify-around">
<% 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 %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/meals/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="mx-auto p-8 w-full">
<div class="text-center mb-8">
<%= 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' %>
</div>
<div class="mb-8">
<h2 class="my-4 font-bold text-2xl items-center">
Expand Down
24 changes: 14 additions & 10 deletions app/views/meeting_rooms/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@
<h2 class="font-bold text-xl">
<span><i class="fa-solid fa-utensils mr-2"></i>候補</span>
</h2>
<div id="proposals" class="inner">
<% @remarks.filter(&:proposal?).each do |remark| %>
<%= render 'remarks/proposal', remark: %>
<% end %>
</div>
<div id="proposals" class="inner">
<% @remarks.filter(&:proposal?).each do |remark| %>
<%= turbo_frame_tag 'proposal' do %>
<%= render 'remarks/proposal', remark: %>
<% end %>
<% end %>
</div>
</div>
<div class="mt-6">
<h2 class="font-bold text-xl mb-4">
<i class="fa-regular fa-comment fa-flip-horizontal"></i>
<span>コメント</span>
</h2>
<div id="comments" class="grid gap-y-2">
<% @remarks.filter(&:comment?).each do |remark| %>
<%= render 'remarks/comment', remark: %>
<% end %>
</div>
<div id="comments" class="grid gap">
<% @remarks.filter(&:comment?).each do |remark| %>
<%= turbo_frame_tag 'comment' do %>
<%= render 'remarks/comment', remark: %>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
28 changes: 15 additions & 13 deletions app/views/remarks/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<%= turbo_frame_tag remark, class: 'border-b border-dashed border-orange-950/10 pb-4' do %>
<div class="flex justify-between my-3">
<div class="flex items-center">
<%= image_tag remark.user.icon, class: 'icon mr-2' %>
<%= l remark.created_at, format: :long %>
</div>
<% if remark.editable?(current_user) %>
<%= link_to edit_meeting_room_remark_path(remark.meeting_room, remark), class: 'mr-2' do %>
<i class="fa-solid fa-pen"></i>
<%= turbo_frame_tag remark do %>
<div class="my-1 border-b border-dashed border-orange-950/10 pb-4">
<div class="flex justify-between my-3">
<div class="flex items-center">
<%= image_tag remark.user.icon, class: 'icon mr-2' %>
<%= l remark.created_at, format: :long %>
</div>
<% if remark.editable?(current_user) %>
<%= link_to edit_meeting_room_remark_path(remark.meeting_room, remark), class: 'mr-2' do %>
<i class="fa-solid fa-pen"></i>
<% end %>
<% end %>
<% end %>
</div>
<div class="mx-auto ml-6 md:w-3/4 break-all font-medium">
<%= format_line_break(remark.content) %>
</div>
<div class="mx-auto ml-6 md:w-3/4 break-all font-medium">
<%= format_line_break(remark.content) %>
</div>
</div>
<% end %>
9 changes: 9 additions & 0 deletions app/views/remarks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<%= turbo_frame_tag remark do %>
<%= form_with model: [meeting_room, remark] do |form| %>
<% if remark.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<ul>
<% remark.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="flex justify-between">
<%= form.hidden_field :remark_type %>
<%= form.hidden_field :meeting_room_id, value: meeting_room.id %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/remarks/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -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, "" %>
9 changes: 9 additions & 0 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<% end %>
</div>
<%= form_with(model: @user, class: 'contents') do |form| %>
<% if @user.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<ul>
<% @user.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :name, class: 'block mb-1' %>
<%= form.text_field :name,
Expand Down
Loading

0 comments on commit 7ac55ce

Please sign in to comment.