diff --git a/myapp/app/controllers/tasks_controller.rb b/myapp/app/controllers/tasks_controller.rb index 73c2d214e8..5100a2abb6 100644 --- a/myapp/app/controllers/tasks_controller.rb +++ b/myapp/app/controllers/tasks_controller.rb @@ -2,11 +2,7 @@ class TasksController < ApplicationController def index - @tasks = if params[:latest] - Task.latest - else - Task.all - end + @tasks = Task.sort_tasks(params['sort_column'], params['sort_direction']) end def show @@ -60,6 +56,6 @@ def destroy private def task_params - params.require(:task).permit(:title, :description) + params.require(:task).permit(:title, :description, :end_date) end end diff --git a/myapp/app/models/task.rb b/myapp/app/models/task.rb index a6ad904fa6..99adcf7be8 100644 --- a/myapp/app/models/task.rb +++ b/myapp/app/models/task.rb @@ -1,8 +1,12 @@ # frozen_string_literal: true class Task < ApplicationRecord + SORT_COLUMN_ALLOWED = %w[created_at end_date].freeze + validates :title, presence: true, length: { maximum: 40 } validates :description, length: { maximum: 500 } - scope :latest, -> { order(created_at: :desc) } + scope :sort_tasks, lambda { |sort_column, sort_direction| + order("#{sort_column} #{sort_direction}") if sort_column.in? SORT_COLUMN_ALLOWED + } end diff --git a/myapp/app/views/tasks/_form.html.erb b/myapp/app/views/tasks/_form.html.erb index cdc491b567..15afc7670a 100644 --- a/myapp/app/views/tasks/_form.html.erb +++ b/myapp/app/views/tasks/_form.html.erb @@ -15,6 +15,11 @@ <% end %> +
+ <%= form.label :end_date %> + <%= form.datetime_field :end_date %> +
+
<%= form.submit %>
diff --git a/myapp/app/views/tasks/index.html.erb b/myapp/app/views/tasks/index.html.erb index f18d4acf43..efdc3cf6d4 100644 --- a/myapp/app/views/tasks/index.html.erb +++ b/myapp/app/views/tasks/index.html.erb @@ -11,7 +11,11 @@ <%= Task.human_attribute_name :description %> <%= Task.human_attribute_name :created_at %> - <%= link_to (I18n.t 'tasks.index.sort.latest'), tasks_path(latest: 'true') %> + <%= link_to (I18n.t 'tasks.index.sort.latest'), tasks_path(sort_column: 'created_at', sort_direction: 'desc') %> + + + <%= Task.human_attribute_name :end_date %> + <%= link_to (I18n.t 'tasks.index.sort.expiring'), tasks_path(sort_column: 'end_date', sort_direction: 'asc') %> <%= I18n.t 'ops.edit' %> @@ -21,6 +25,7 @@ <%= link_to task.title, task %> <%= task.description %> <%= I18n.l(task.created_at.to_date, format: :long) %> + <%= task.end_date ? I18n.l(task.end_date, format: :long) : '-' %> <%= link_to (I18n.t 'ops.edit'), edit_task_path(task) %> <% end %> diff --git a/myapp/app/views/tasks/show.html.erb b/myapp/app/views/tasks/show.html.erb index eccea47120..8ed39dd3d7 100644 --- a/myapp/app/views/tasks/show.html.erb +++ b/myapp/app/views/tasks/show.html.erb @@ -6,6 +6,7 @@

<%= @task.title %>

<%= "#{Task.human_attribute_name :description}: #{@task.description}" %>

+

<%= "#{Task.human_attribute_name :end_date}: #{@task.end_date ? I18n.l(@task.end_date, format: :long) : '-'}" %>

<%= link_to (I18n.t 'ops.edit'), edit_task_path(@task) %> <%= link_to (I18n.t 'ops.delete'), task_path(@task), diff --git a/myapp/config/locales/common.ja.yml b/myapp/config/locales/common.ja.yml index ef50a3585f..f7d97e4faf 100644 --- a/myapp/config/locales/common.ja.yml +++ b/myapp/config/locales/common.ja.yml @@ -20,7 +20,7 @@ ja: formats: default: "%Y/%m/%d %H:%M:%S" short: "%y/%m/%d %H:%M" - long: "%Y年%m月%d日(%a) %H時%M分%S秒 %Z" + long: "%Y年%m月%d日(%a) %H時%M分%S秒" am: "午前" pm: "午後" ops: diff --git a/myapp/config/locales/model.ja.yml b/myapp/config/locales/model.ja.yml index 90b64d8fb6..f515e28c6b 100644 --- a/myapp/config/locales/model.ja.yml +++ b/myapp/config/locales/model.ja.yml @@ -10,6 +10,7 @@ ja: # view側: User.human_attribute_name :name => "名前" / t("activerecord.attributes.user.name")と同じ title: タイトル description: 詳細 + end_date: 終了期限 # 全てのmodelで共通して使用するattributesを定義 attributes: diff --git a/myapp/config/locales/views/tasks/index/ja.yml b/myapp/config/locales/views/tasks/index/ja.yml index 0c9aa6b4dc..816093f409 100644 --- a/myapp/config/locales/views/tasks/index/ja.yml +++ b/myapp/config/locales/views/tasks/index/ja.yml @@ -4,3 +4,4 @@ ja: title: 'タスク一覧' sort: latest: '新しい順' + expiring: '終了期限が近い順' diff --git a/myapp/db/migrate/20221208085448_add_column_end_date_to_tasks.rb b/myapp/db/migrate/20221208085448_add_column_end_date_to_tasks.rb new file mode 100644 index 0000000000..9c3c21a7cd --- /dev/null +++ b/myapp/db/migrate/20221208085448_add_column_end_date_to_tasks.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddColumnEndDateToTasks < ActiveRecord::Migration[6.0] + def change + add_column :tasks, :end_date, :datetime + end +end diff --git a/myapp/db/schema.rb b/myapp/db/schema.rb index 5ed9ffb8bb..c80759c5fe 100644 --- a/myapp/db/schema.rb +++ b/myapp/db/schema.rb @@ -10,13 +10,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_12_07_134939) do +ActiveRecord::Schema.define(version: 2022_12_08_085448) do create_table "tasks", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "title", limit: 40, null: false t.text "description" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.datetime "end_date" end end diff --git a/myapp/docker-compose.yml b/myapp/docker-compose.yml index 8e6597bf88..0cd75f5a69 100644 --- a/myapp/docker-compose.yml +++ b/myapp/docker-compose.yml @@ -5,6 +5,7 @@ services: environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: root + TZ: Asia/Tokyo platform: linux/amd64 ports: - "3316:3306" @@ -38,11 +39,14 @@ services: DB_NAME: myapp DB_PASSWORD: password DB_HOST: db + TZ: Asia/Tokyo chrome: image: seleniarm/standalone-chromium ports: - 4444:4444 + environment: + TZ: Asia/Tokyo volumes: db-data: diff --git a/myapp/spec/factories/tasks.rb b/myapp/spec/factories/tasks.rb index 54578985d1..03a03f0bea 100644 --- a/myapp/spec/factories/tasks.rb +++ b/myapp/spec/factories/tasks.rb @@ -4,5 +4,6 @@ factory :task do title { 'Spec' } description { 'test' } + end_date { Time.new(2022, 12, 7, 10, 30) } end end diff --git a/myapp/spec/system/tasks_spec.rb b/myapp/spec/system/tasks_spec.rb index 1efe84d296..c842481263 100644 --- a/myapp/spec/system/tasks_spec.rb +++ b/myapp/spec/system/tasks_spec.rb @@ -17,10 +17,10 @@ end context 'when there are tasks,' do - let!(:task1) { FactoryBot.create(:task) } - let!(:task2) { FactoryBot.create(:task, title: 'Spec2', description: 'test2') } - before do + FactoryBot.create(:task) + FactoryBot.create(:task, title: 'Spec2', description: 'test2', end_date: Time.new(2023, 11, 1, 10, 30)) + # 一覧画面を開く visit tasks_path end @@ -30,8 +30,11 @@ expect(page).to have_content 'タスク一覧' expect(page).to have_content 'Spec' expect(page).to have_content 'test' + expect(page).to have_content '2022年12月07日(水) 10時30分00秒' expect(page).to have_content 'Spec2' expect(page).to have_content 'test2' + expect(page).to have_content '2023年11月01日(水) 10時30分00秒' + expect(Task.count).to eq 2 end it 'sorts correctly after push sort button: latest' do @@ -41,6 +44,15 @@ # 正規表現で並び順をチェック expect(page.text).to match(/Spec2.*Spec/) end + + it 'sorts correctly after push sort button: expiring' do + click_link '終了期限が近い順' + + expect(page).to have_content 'タスク一覧' + # 正規表現で並び順をチェック + expect(page.text).to match(/Spec.*Spec2/) + expect(Task.count).to eq 2 + end end context 'when error,' do @@ -70,6 +82,7 @@ # titleとdescriptionを入力 fill_in 'task_title', with: 'Spec test new task' fill_in 'task_description', with: 'Spec test new task description' + fill_in 'task_end_date', with: Time.new(2023, 11, 1, 10, 30) # 登録 click_button 'タスクを登録する' @@ -79,6 +92,7 @@ expect(page).to have_content 'タスク詳細' expect(page).to have_content 'Spec test new task' expect(page).to have_content 'Spec test new task description' + expect(page).to have_content '2023年11月01日(水) 10時30分00秒' end end @@ -222,6 +236,7 @@ # titleとdescriptionを入力する fill_in 'task_title', with: 'Spec first task' fill_in 'task_description', with: 'Spec first task description' + fill_in 'task_end_date', with: Time.new(2023, 11, 1, 10, 30) # 更新実行 click_button 'タスクを更新する' @@ -231,6 +246,7 @@ expect(page).to have_content 'タスク詳細' expect(page).to have_content 'Spec first task' expect(page).to have_content 'Spec first task description' + expect(page).to have_content '2023年11月01日(水) 10時30分00秒' end end