質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

2回答

1046閲覧

model内記載のメソッドを綺麗にしたい

innjera

総合スコア132

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2019/03/02 02:22

カウントダウン表示の為に、以下の通りモデル内のメソッドに

残り ①日
残り ②時間
残り ③分
残り ④秒で分けて計算できるメソッドを作りました。

コードは機能しているのですが、全く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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

Railsのビューヘルパーにdistance_of_time_in_wordsというのがあり
表示だけなら便利にやれます。

日本語資料としては、引数を減らしたtime_ago_in_wordsの方が探しやすいかもしれません。


また、ActiveSupport::Duration.buildという手段もあります。

ruby

1class Tender 2 def remaining 3 ActiveSupport::Duration.build(ends_at - Time.current).parts 4 end 5 6 def remaining_hours; remaining[:hours] end 7 def remaining_minuites; remaining[:minutes] end 8 def remaining_seconds; remaining[:seconds] end 9 def remaining_days 10 rem = remaining 11 rem[:days] + 7 * rem[:weeks] 12 end 13end

追記: 1月以上になるとこの方法だと問題が出てきます

投稿2019/03/02 12:50

編集2019/03/02 13:05
asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

innjera

2019/03/02 13:00

ありがとうございます!そのヘルパー知りませんでした。参考になります!
guest

0

ベストアンサー

既存のメソッドを呼ぶようにすれば、物凄くシンプルになります。

ruby

1class Tender < ApplicationRecord 2 def total_seconds 3 (ends_at - Time.current).round 4 end 5 6 def remaining_days 7 total_seconds / (60 * 60 * 24) 8 end 9 10 def remaining_hours 11 total_seconds / (60 * 60) - (remaining_days * 24) 12 end 13 14 def remaining_minuites 15 total_seconds / 60 - (remaining_days * 24 * 60 + remaining_hours * 60) 16 end 17 18 def remaining_seconds 19 total_seconds - (remaining_days * 24 * 60 * 60 + remaining_hours * 60 * 60 + remaining_miniutes * 60) 20 end 21 22end

投稿2019/03/02 02:40

maisumakun

総合スコア145184

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

innjera

2019/03/02 04:48

言われてみれば単純な話も、1人では分からないレベルなので、大変助かります! ありがとうございました、かなりスッキリしました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問