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

Answers from ttwo32 #10

Open
wants to merge 4 commits into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+15 −7
Diff settings

Always

Just for now

@@ -1,5 +1,6 @@
class TicketsController < ApplicationController
before_action :load_ticket, only: %i(edit update show)
before_action :already_used_ticket?, only: %i(edit update)
def index
redirect_to root_path
@@ -46,4 +47,8 @@ def ticket_update_params
def load_ticket
@ticket = Ticket.find(params[:id])
end
def already_used_ticket?
redirect_to root_path, notice: '降車済みの切符です。' if @ticket.exited_gate_id.present?
end
end
View
@@ -8,6 +8,9 @@ class Gate < ApplicationRecord
scope :order_by_station_number, -> { order(:station_number) }
def exit?(ticket)
true
entered_gate = ticket.entered_gate
return false if self.id == entered_gate.id
fare_index = (self.station_number - entered_gate.station_number).abs - 1
Gate::FARES[fare_index] <= ticket.fare
end
end
View
@@ -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 :ticket_exit?, on: :update
private
def ticket_exit?
errors.add(:exited_gate, 'では降車できません。') unless exited_gate.exit?(self)
end
end
View
@@ -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
@@ -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
@@ -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)
@@ -14,7 +14,6 @@ class TicketsTest < ApplicationSystemTestCase
end
test '運賃が足りない場合' do
skip 'Please implement this!'
visit root_path
select '150円', from: '切符'
select 'うめだ', from: '乗車駅'
@@ -27,7 +26,6 @@ class TicketsTest < ApplicationSystemTestCase
end
test '同じ駅で降りる場合' do
skip 'Please implement this!'
visit root_path
select '150円', from: '切符'
select 'うめだ', from: '乗車駅'
@@ -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)
ProTip! Use n and p to navigate between commits in a pull request.