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 totto357 #26

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
9 changes: 8 additions & 1 deletion app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class TicketsController < ApplicationController
before_action :load_ticket, only: %i(edit update show)
before_action :used_ticket, only: %i(edit update show)

def index
redirect_to root_path
Expand All @@ -26,9 +27,11 @@ def edit
end

def update
if @ticket.update(ticket_update_params)
exited_gate = Gate.find(ticket_update_params[:exited_gate_id])
if exited_gate.exit?(@ticket) && @ticket.update(ticket_update_params)
redirect_to root_path, notice: '降車しました。😄'
else
flash.now[:error] = '降車駅 では降車できません。'
render :edit
end
end
Expand All @@ -46,4 +49,8 @@ def ticket_update_params
def load_ticket
@ticket = Ticket.find(params[:id])
end

def used_ticket
redirect_to root_path, notice: '降車済みの切符です。' if @ticket.used?
end
end
32 changes: 31 additions & 1 deletion app/models/gate.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
# NOTE: Gate = 改札機のイメージ
class Gate < ApplicationRecord

FARES = [150, 190].freeze

validates :name, presence: true, uniqueness: true
validates :station_number, presence: true, uniqueness: true

scope :order_by_station_number, -> { order(:station_number) }

# 指定されたチケットで改札から出られるか
# @params [Ticket] ticket 購入したチケット
# @return [Boolean] true:出られる
def exit?(ticket)
true
return false if self.same_station? ticket&.entered_gate
calc_fare(ticket) <= ticket.fare
end

# 同一駅の改札かどうか
# @params [Gate] other_gate 比較対象の改札
# @return [Boolean] 駅番号が一致する場合にtrue
def same_station?(other_gate)
return false unless other_gate.instance_of? Gate
self.station_number == other_gate.station_number
end

private

# チケットから運賃を計算する
# @params [Ticket] ticket 乗車チケット
# @return [Integer] 運賃
def calc_fare(ticket)
FARES[distance_from(ticket.entered_gate) - 1]
end

# 指定された改札がある駅との区間距離を取得する
# @params [Gate] gate 改札機
# @return [Integer] 改札との区間
def distance_from(gate)
(station_number - gate.station_number).abs
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

# チケットが使用済みかどうか
# @return [Boolean] 使用済みの場合true
def used?
exited_gate.present?
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