Skip to content

Commit 8b03bd9

Browse files
committed
wip Add update bento action
1 parent f2a4954 commit 8b03bd9

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

app/controllers/admin/lunchboxes_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ def create
3232

3333
# PATCH/PUT /lunchboxes/1
3434
def update
35+
3536
if @lunchbox.update(lunchbox_params)
3637
redirect_to admin_lunchboxes_path, notice: 'Lunchbox was successfully updated.'
3738
else
38-
render :edit
39+
puts("#########")
40+
puts("#########")
41+
puts("#########")
42+
render :edit, notice: '更新できませんでした'
3943
end
4044
end
4145

app/models/lunchbox.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,27 @@
1010
#
1111

1212
class Lunchbox < ApplicationRecord
13+
has_many :order_items
14+
has_many :orders, through: :order_items
15+
16+
# validation
17+
18+
validate :prevent_future_reserved_lunchbox, on: :update
19+
20+
private
21+
def prevent_future_reserved_lunchbox
22+
# 弁当に紐づくオーダーを取ってくる
23+
# 未来日のオーダーに自分が含まれているかを確認
24+
# 含まれている場合は更新させない
25+
# puts orders.where("date > ?", Date.current).present?
26+
# puts orders.first.date
27+
# puts self.name
28+
# puts Date.current
29+
# puts"###################"
30+
31+
if orders.where("date > ?", Date.current).present?
32+
errors.add(:future_date, "未来日に予約された弁当は更新できません")
33+
end
34+
end
35+
1336
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'rails_helper'
2+
3+
RSpec.feature '未来日に予約されている弁当の更新を行う時にエラーにする', type: :feature do
4+
given!(:order) { create(:order, date: Time.zone.local(2017, 2, 4)) }
5+
given!(:lunchbox) { create(:lunchbox) }
6+
7+
scenario '未来日に予約を入れた状態で、その弁当を更新する' do
8+
Timecop.freeze(Time.zone.local(2017, 2, 1)) do
9+
create(:order_item, lunchbox_id: lunchbox.id, order: order)
10+
11+
visit edit_admin_lunchbox_path(lunchbox)
12+
13+
fill_in 'Name', with: "new_lunchbox_name"
14+
15+
click_button('Update Lunchbox')
16+
17+
expect(page).to have_text('未来日に予約された弁当は更新できません')
18+
# expect(page).not_to have_text('new_lunchbox_name')
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)