Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kumo raku step12 add end date #1459

Merged
merged 8 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 2 additions & 6 deletions myapp/app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion myapp/app/models/task.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions myapp/app/views/tasks/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<% end %>
</div>

<div>
<%= form.label :end_date %>
<%= form.datetime_field :end_date %>
</div>

<div>
<%= form.submit %>
</div>
Expand Down
7 changes: 6 additions & 1 deletion myapp/app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
<th><%= Task.human_attribute_name :description %></th>
<th>
<span><%= Task.human_attribute_name :created_at %></span>
<small><%= link_to (I18n.t 'tasks.index.sort.latest'), tasks_path(latest: 'true') %></small>
<small><%= link_to (I18n.t 'tasks.index.sort.latest'), tasks_path(sort_column: 'created_at', sort_direction: 'desc') %></small>
</th>
<th>
<span><%= Task.human_attribute_name :end_date %></span>
<small><%= link_to (I18n.t 'tasks.index.sort.expiring'), tasks_path(sort_column: 'end_date', sort_direction: 'asc') %></small>
</th>
<th><%= I18n.t 'ops.edit' %></th>
</tr>
Expand All @@ -21,6 +25,7 @@
<td><%= link_to task.title, task %></td>
<td><%= task.description %></td>
<td><%= I18n.l(task.created_at.to_date, format: :long) %></td>
<td><%= task.end_date ? I18n.l(task.end_date, format: :long) : '-' %></td>
<td><%= link_to (I18n.t 'ops.edit'), edit_task_path(task) %></td>
</tr>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions myapp/app/views/tasks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

<%= link_to (I18n.t 'ops.edit'), edit_task_path(@task) %>
<%= link_to (I18n.t 'ops.delete'), task_path(@task),
Expand Down
2 changes: 1 addition & 1 deletion myapp/config/locales/common.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions myapp/config/locales/model.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ja:
# view側: User.human_attribute_name :name => "名前" / t("activerecord.attributes.user.name")と同じ
title: タイトル
description: 詳細
end_date: 終了期限

# 全てのmodelで共通して使用するattributesを定義
attributes:
Expand Down
1 change: 1 addition & 0 deletions myapp/config/locales/views/tasks/index/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ ja:
title: 'タスク一覧'
sort:
latest: '新しい順'
expiring: '終了期限が近い順'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddColumnEndDateToTasks < ActiveRecord::Migration[6.0]
def change
add_column :tasks, :end_date, :datetime
end
end
3 changes: 2 additions & 1 deletion myapp/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions myapp/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: root
TZ: Asia/Tokyo
platform: linux/amd64
ports:
- "3316:3306"
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions myapp/spec/factories/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
factory :task do
title { 'Spec' }
description { 'test' }
end_date { Time.new(2022, 12, 7, 10, 30) }
end
end
22 changes: 19 additions & 3 deletions myapp/spec/system/tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 'タスクを登録する'
Expand All @@ -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

Expand Down Expand Up @@ -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 'タスクを更新する'
Expand All @@ -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

Expand Down