学習のためRuby on Railsで出退勤管理のアプリを作っています。
休憩入りや休憩終了時、出勤退勤時の時間を記録するアプリなのですが、下記の要件をどうプログラミングで表現すればいいのか迷っています。
要件1 出勤されていたら、出勤ボタンを非活性化
要件2 休憩を開始していたら、休憩開始ボタンを非活性化し、休憩終了ボタンを活性化する
要件3 休憩を開始していて、かつ休憩終了ボタンが押されていなかったら退勤ボタンを非活性化する。
要件4 休憩開始と終了をすでにしていて、もう一度休憩開始を実行した場合、休憩開始ボタンを非活性化して、休憩終了ボタンを活性化させる。
この機能を実現するために、下記のようなAttendance(出退勤を記録するデータベースモデル)に対するdecoratorを定義しました。
ruby
module AttendanceDecorator def work_in_button button_to "出勤", attendances_work_in_path , {:disabled=> work_in != nil} end def break_in_button if work_in == nil button_to "休憩開始", attendances_break_in_path, {:disabled=> true } elsif break_in != nil && break_out != nil && break_out < break_in # 一回休憩開始と終了を押して、休憩開始のみを押している場合 # 休憩開始していない button_to "休憩開始", attendances_break_in_path, {:disabled=> true } elsif break_in == nil # 休憩開始をしていない button_to "休憩開始", attendances_break_in_path, {:disabled=> false } elsif break_out == nil # 休憩開始しているけど休憩終了していない button_to "休憩開始", attendances_break_in_path, {:disabled=> true } else# 休憩開始もしていないし休憩終了もしていない button_to "休憩開始", attendances_break_in_path, {:disabled=> false } end end def break_out_button if work_in == nil button_to "休憩終了", attendances_break_out_path, {:disabled=> true } elsif break_in != nil && break_out != nil && break_out < break_in # 一回休憩開始と終了を押して、休憩開始のみを押している場合 button_to "休憩完了", attendances_break_out_path, {:disabled=> false} elsif break_in == nil # 休憩開始をしていない button_to "休憩完了", attendances_break_out_path, {:disabled=> true} elsif break_out != nil #休憩開始してるし、完了もしている button_to "休憩完了", attendances_break_out_path, {:disabled=> true} else #休憩開始してるが、完了をしていない button_to "休憩完了", attendances_break_out_path, {:disabled=> false} end end def work_out_button if work_in == nil button_to "退勤", attendances_work_out_path, {:disabled=> true } elsif !break_in && break_out button_to "退勤", attendances_work_out_path, {:disabled=> true } else button_to "退勤", attendances_work_out_path, {:disabled=> false } end end end
下記がattendancesテーブルです。
create_table "attendances", charset: "utf8mb3", force: :cascade do |t| t.datetime "work_in" t.datetime "break_in" t.datetime "break_out" t.datetime "work_out" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.bigint "user_id" t.index ["user_id"], name: "index_attendances_on_user_id" end
上記のコードは少し条件分岐が複雑でもっとシンプルに表現できないかなと思っているのですが、何かいい案はありますでしょうか?
まだ回答がついていません
会員登録して回答してみよう