Skip to content

Commit

Permalink
Merge pull request #108 from motohiro-mm/fix/delete_meals_without_del…
Browse files Browse the repository at this point in the history
…eting_meal_plans

meal_plansの削除ではなくmealsの削除になるように変更
  • Loading branch information
motohiro-mm authored Oct 5, 2024
2 parents 6432430 + dd5a4cf commit 26cfa9b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ RSpec/ExampleLength:

RSpec/MultipleExpectations:
Enabled: false

Layout/LineLength:
Max: 150
4 changes: 2 additions & 2 deletions app/controllers/meal_plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def update
end

def destroy
@meal_plan.destroy!
redirect_to meal_plans_path, notice: 'MealPlan was successfully destroyed.', status: :see_other
@meal_plan.meals.each(&:destroy!)
redirect_to meal_plans_path(meal_date: @meal_plan.meal_date), status: :see_other
end

def calendar
Expand Down
4 changes: 1 addition & 3 deletions app/models/meal_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def create_or_update_meals_by_proposal(attributes)

def destroy_unnecessary_meals(attributes)
attributes[:meals_attributes].each_value do |update_meal|
if update_meal[:id].present? && update_meal.except(:id, :timing).values.all?(&:blank?)
meals.find(update_meal[:id]).destroy
end
meals.find(update_meal[:id]).destroy if update_meal[:id].present? && update_meal.except(:id, :timing).values.all?(&:blank?)
end
end

Expand Down
9 changes: 7 additions & 2 deletions app/views/meal_plans/_meal_plan.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
<% @meal_plan.meals_sort_by_timing.each do |meal| %>
<%= render 'meals/meal', meal: %>
<% end %>
<%= link_to '編集', edit_meal_plan_path(@meal_plan),
class: 'mt-2 rounded-lg py-3 px-5 text-stone-500 bg-stone-100 inline-block' %>
<% if @meal_plan.meals.blank? %>
<%= link_to '献立登録', new_meal_plan_path(meal_date: @meal_plan.meal_date), data: { turbo_frame: '_top' },
class: 'mt-2 rounded-lg py-3 px-5 text-stone-500 bg-stone-100' %>
<% else %>
<%= link_to '編集', edit_meal_plan_path(@meal_plan),
class: 'mt-2 rounded-lg py-3 px-5 text-stone-500 bg-stone-100' %>
<% end %>
</div>
<% end %>

0 comments on commit 26cfa9b

Please sign in to comment.