Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer by sanfrecce-osaka #12

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ def show
end

def edit
stored_record = Ticket.find(@ticket.id)
redirect_to root_path, alert: '降車済みの切符です。' if stored_record.exited_gate_id
end

def update
stored_record = Ticket.find(@ticket.id)
return redirect_to root_path, alert: '降車済みの切符です。' if stored_record.exited_gate_id

if @ticket.update(ticket_update_params)
redirect_to root_path, notice: '降車しました。😄'
else
Expand Down
4 changes: 3 additions & 1 deletion app/models/gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Gate < ApplicationRecord
scope :order_by_station_number, -> { order(:station_number) }

def exit?(ticket)
true
entered_gate = Gate.find(ticket.entered_gate_id)
section = (self.station_number - entered_gate.station_number).abs
(section != 0) ? (ticket.fare >= FARES[section - 1]) : false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

section > 0 && ticket.fare >= FARES[section - 1]

end
end
6 changes: 6 additions & 0 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ class Ticket < ApplicationRecord
belongs_to :exited_gate, class_name: 'Gate', foreign_key: 'exited_gate_id', required: false
validates :fare, presence: true, inclusion: Gate::FARES
validates :entered_gate_id, presence: true
validate :check_exitable, on: :update

def check_exitable
exited_gate = Gate.find(exited_gate_id)
errors.add(:exited_gate_id, '降車駅 では降車できません。') unless exited_gate.exit?(self)
end
end
3 changes: 0 additions & 3 deletions test/models/gate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class GateTest < ActiveSupport::TestCase
end

test 'うめだで150円の切符を買って、みくにで降りる(運賃不足)' do
skip 'Please implement this!'
ticket = Ticket.create!(fare: 150, entered_gate: @umeda)
refute @mikuni.exit?(ticket)
end
Expand All @@ -46,7 +45,6 @@ class GateTest < ActiveSupport::TestCase
end

test 'みくにで150円の切符を買って、うめだで降りる(運賃不足)' do
skip 'Please implement this!'
ticket = Ticket.create!(fare: 150, entered_gate: @mikuni)
refute @umeda.exit?(ticket)
end
Expand All @@ -63,7 +61,6 @@ class GateTest < ActiveSupport::TestCase

# その他
test '同じ駅では降りられない' do
skip 'Please implement this!'
ticket = Ticket.create!(fare: 190, entered_gate: @umeda)
refute @umeda.exit?(ticket)

Expand Down
5 changes: 1 addition & 4 deletions test/system/tickets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class TicketsTest < ApplicationSystemTestCase
end

test '運賃が足りない場合' do
skip 'Please implement this!'
visit root_path
select '150円', from: '切符'
select 'うめだ', from: '乗車駅'
Expand All @@ -27,7 +26,6 @@ class TicketsTest < ApplicationSystemTestCase
end

test '同じ駅で降りる場合' do
skip 'Please implement this!'
visit root_path
select '150円', from: '切符'
select 'うめだ', from: '乗車駅'
Expand All @@ -40,7 +38,6 @@ class TicketsTest < ApplicationSystemTestCase
end

test 'すでに使用済みの切符を指定されたらトップページに移動する' do
skip 'Please implement this!'
# edit
ticket = Ticket.create!(fare: 150, entered_gate: gates(:umeda), exited_gate: gates(:juso))
visit edit_ticket_path(ticket)
Expand All @@ -67,4 +64,4 @@ class TicketsTest < ApplicationSystemTestCase
visit ticket_path(ticket)
assert_current_path edit_ticket_path(ticket)
end
end
end