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

Ex1〜6 の解答を作成しました。 #15

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/tickets_controller.rb
Expand Up @@ -45,5 +45,6 @@ def ticket_update_params

def load_ticket
@ticket = Ticket.find(params[:id])
redirect_to root_path, alert: '降車済みの切符です。' if @ticket.exited_gate_id
end
end
10 changes: 9 additions & 1 deletion app/models/gate.rb
Expand Up @@ -8,6 +8,14 @@ class Gate < ApplicationRecord
scope :order_by_station_number, -> { order(:station_number) }

def exit?(ticket)
true
price = calculate(ticket)
price > 0 && ticket.fare - price >= 0
end

private

def calculate(ticket)
section = (station_number - ticket.entered_gate.station_number).abs
section > 0 ? FARES[section - 1] : 0
end
end
10 changes: 9 additions & 1 deletion app/models/ticket.rb
@@ -1,6 +1,14 @@
class Ticket < ApplicationRecord
belongs_to :entered_gate, class_name: 'Gate', foreign_key: 'entered_gate_id'
belongs_to :exited_gate, class_name: 'Gate', foreign_key: 'exited_gate_id', required: false
belongs_to :exited_gate, class_name: 'Gate', foreign_key: 'exited_gate_id', optional: true
validates :fare, presence: true, inclusion: Gate::FARES
validates :entered_gate_id, presence: true

validate :must_be_exit, if: :exited_gate_id

private

def must_be_exit
errors.add(:exited_gate, 'では降車できません。') unless exited_gate.exit?(self)
end
end
3 changes: 0 additions & 3 deletions test/models/gate_test.rb
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
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