カウントダウン表示の為に、以下の通りモデル内のメソッドに
残り ①日
残り ②時間
残り ③分
残り ④秒で分けて計算できるメソッドを作りました。
コードは機能しているのですが、全くDRYでなく、とはいえ、どうすれば綺麗になるのか分からず
綺麗な書き方をご教示頂けますと幸甚です。
ruby
1# == Schema Information 2# 3# Table name: tenders 4# 5# id :bigint(8) not null, primary key 6# seller_id :bigint(8) not null 7# tender_location_id :bigint(8) not null 8# starts_at :datetime not null 9# ends_at :datetime not null 10# preparation_status :integer default(0), not null 11# created_at :datetime not null 12# updated_at :datetime not null 13# 14 15class Tender < ApplicationRecord 16 def remaining_days 17 total_seconds = (ends_at - Time.current).round 18 total_seconds / (60 * 60 * 24) 19 end 20 21 def remaining_hours 22 total_seconds = (ends_at - Time.current).round 23 remaining_days = total_seconds / (60 * 60 * 24) 24 total_seconds / (60 * 60) - (remaining_days * 24) 25 end 26 27 def remaining_minuites 28 total_seconds = (ends_at - Time.current).round 29 remaining_days = total_seconds / (60 * 60 * 24) 30 remaining_hours = (total_seconds / (60 * 60) - (remaining_days * 24)) 31 total_seconds / 60 - (remaining_days * 24 * 60 + remaining_hours * 60) 32 end 33 34 def remaining_seconds 35 total_seconds = (ends_at - Time.current).round 36 remaining_days = total_seconds / (60 * 60 * 24) 37 remaining_hours = (total_seconds / (60 * 60) - (remaining_days * 24)) 38 remaining_miniutes = (total_seconds / 60) - (remaining_days * 24 * 60 + remaining_hours * 60) 39 total_seconds - (remaining_days * 24 * 60 * 60 + remaining_hours * 60 * 60 + remaining_miniutes * 60) 40 end 41 42end 43
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/02 13:00