Skip to content
Open
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
68fd066
新規Railsアプリの作成
MasafumiKondo07 Sep 11, 2022
ae5de6d
model、migrationファイルの追加
MasafumiKondo07 Sep 20, 2022
2eddab0
マスターデータ用のCSVの作成と、seedファイルの記述
MasafumiKondo07 Sep 20, 2022
82630c7
プランごとの電気料金を返すAPIの作成
MasafumiKondo07 Sep 20, 2022
45a0e14
重量料金計算ロジックを重量料金を扱うモデルへ移動
MasafumiKondo07 Oct 2, 2022
9b7a2c7
nil判定を後置ifから「 ||=」に変更
MasafumiKondo07 Oct 2, 2022
ac8e8e6
従量料金の計算方法の修正
MasafumiKondo07 Oct 3, 2022
152a767
電気料金算出ロジックをPlanモデルに寄せる
MasafumiKondo07 Oct 3, 2022
6d4fa21
従量料金を求めるロジックの修正
MasafumiKondo07 Oct 8, 2022
096506e
アプリの中で、「会社」の命名に一貫性を持たせるための修正
MasafumiKondo07 Oct 8, 2022
ca23a42
不要な項目を削除(CSV)
MasafumiKondo07 Oct 8, 2022
b550e4c
クラスメソッドの修正
MasafumiKondo07 Oct 9, 2022
6290631
rspec、factory_botの導入
MasafumiKondo07 Oct 9, 2022
21ff83c
ElectricityFeeモデルのクラスメソッドテスト用factoryの作成
MasafumiKondo07 Oct 9, 2022
2172b6e
従量料金計算クラスメソッドのテスト作成
MasafumiKondo07 Oct 9, 2022
14d1c7f
n + 1の解消
MasafumiKondo07 Oct 9, 2022
c92c96a
ファイル名が不適切だったため修正
MasafumiKondo07 Oct 9, 2022
3d03e55
基本料金のfactory作成
MasafumiKondo07 Oct 9, 2022
d87fc4d
controllerのテスト追加
MasafumiKondo07 Oct 9, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ class ElectricityFee < ApplicationRecord
belongs_to :plan

def calc(amount_used)
price * amount_used
target_electricity_fees = self.class.where(plan_id: plan_id).order(classification_min: :asc)
result = 0
target_electricity_fees.each do |instance|
break if (amount_used < instance.classification_min)
usage_lower_limit = instance.classification_min == 0 ? 0 : instance.classification_min - 1
usage_upper_limit = instance.classification_max.present? && instance.classification_max < amount_used ? instance.classification_max : amount_used
result += (usage_upper_limit - usage_lower_limit) * instance.price
end
result
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここのロジックが一番複雑になるので、テストをしっかり書いてほしいです。期待値がなんであるか、その通りに出力されているか、テストに書いてあることでレビュワーは実際に動かさなくても結果が見えてきやすいので業務を効率よく行うためにテストは不可欠です。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

electricity_fee_instance.calcで呼び出していると思うのですが、そのelectricity_fee_instanceって既にplanから呼び出しているものなのに、ここでまたplan_idから引っ張ってくるのって二度手間ではないですか?
find_byとwhereで似たようなことを2回してしまっていると思いました。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

再度ご確認いただきましてありがとうございます🙏
従量料金を求めるロジックを下記のように修正させていただきました。
6d4fa21
b550e4c

またテストが書けていなかったことについて、申し訳ございませんでした。
従量料金計算テストと、controllerのテストを追加させていただきました。
2172b6e

d87fc4d

end
end