Skip to content

Commit e7441de

Browse files
tksginorifumi
authored andcommitted
[WIP] refs framgia#12 論理削除機能を実装する。 (framgia#15)
* refs framgia#12 論理削除機能を実装する。 * dbにカラム追加 表示データ修正 * scopeを利用するように修正 * エラー時に404ページを表示する * リファクタリングとか * リファクタリング * 削除機能view実装 * チェックボックス実装 * チェックボックスの値をコントローラーで受け取る * unavilableが機能しない問題を修正 * ケアレスミス修正
1 parent f02beba commit e7441de

21 files changed

+308
-25
lines changed

app/assets/javascripts/application.js

-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@
1313
//= require rails-ujs
1414
//= require activestorage
1515
//= require turbolinks
16-
//= require_tree .

app/assets/javascripts/users/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function submitFnc(){
2+
const form = document.getElementById("form");
3+
// 末尾に/が追加されるブラウザ対応
4+
if(form.action.slice(-1) == "/"){
5+
form.action = form.action.slice(0, -1);
6+
}
7+
8+
const checkboxes = document.getElementsByName("id[]");
9+
10+
11+
let id_str = "/";
12+
checkboxes.forEach(checkbox => {
13+
if(checkbox.checked){
14+
if(id_str != "/"){id_str += ",";}
15+
id_str += checkbox.value;
16+
}
17+
});
18+
19+
if(id_str != "/"){
20+
const method = document.getElementsByName("_method")[0];
21+
method.setAttribute('value', 'delete');
22+
form.appendChild(method);
23+
24+
form.action += id_str;
25+
}
26+
}

app/assets/statics/404.html

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>The page you were looking for doesn't exist (404)</title>
5+
<meta name="viewport" content="width=device-width,initial-scale=1">
6+
<style>
7+
.rails-default-error-page {
8+
background-color: #EFEFEF;
9+
color: #2E2F30;
10+
text-align: center;
11+
font-family: arial, sans-serif;
12+
margin: 0;
13+
}
14+
15+
.rails-default-error-page div.dialog {
16+
width: 95%;
17+
max-width: 33em;
18+
margin: 4em auto 0;
19+
}
20+
21+
.rails-default-error-page div.dialog > div {
22+
border: 1px solid #CCC;
23+
border-right-color: #999;
24+
border-left-color: #999;
25+
border-bottom-color: #BBB;
26+
border-top: #B00100 solid 4px;
27+
border-top-left-radius: 9px;
28+
border-top-right-radius: 9px;
29+
background-color: white;
30+
padding: 7px 12% 0;
31+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32+
}
33+
34+
.rails-default-error-page h1 {
35+
font-size: 100%;
36+
color: #730E15;
37+
line-height: 1.5em;
38+
}
39+
40+
.rails-default-error-page div.dialog > p {
41+
margin: 0 0 1em;
42+
padding: 1em;
43+
background-color: #F7F7F7;
44+
border: 1px solid #CCC;
45+
border-right-color: #999;
46+
border-left-color: #999;
47+
border-bottom-color: #999;
48+
border-bottom-left-radius: 4px;
49+
border-bottom-right-radius: 4px;
50+
border-top-color: #DADADA;
51+
color: #666;
52+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53+
}
54+
</style>
55+
</head>
56+
57+
<body class="rails-default-error-page">
58+
<!-- This file lives in public/404.html -->
59+
<div class="dialog">
60+
<div>
61+
<h1>The page you were looking for doesn't exist.</h1>
62+
<p>You may have mistyped the address or the page may have moved.</p>
63+
</div>
64+
<p>If you are the application owner check the logs for more information.</p>
65+
</div>
66+
</body>
67+
</html>

app/assets/statics/422.html

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>The change you wanted was rejected (422)</title>
5+
<meta name="viewport" content="width=device-width,initial-scale=1">
6+
<style>
7+
.rails-default-error-page {
8+
background-color: #EFEFEF;
9+
color: #2E2F30;
10+
text-align: center;
11+
font-family: arial, sans-serif;
12+
margin: 0;
13+
}
14+
15+
.rails-default-error-page div.dialog {
16+
width: 95%;
17+
max-width: 33em;
18+
margin: 4em auto 0;
19+
}
20+
21+
.rails-default-error-page div.dialog > div {
22+
border: 1px solid #CCC;
23+
border-right-color: #999;
24+
border-left-color: #999;
25+
border-bottom-color: #BBB;
26+
border-top: #B00100 solid 4px;
27+
border-top-left-radius: 9px;
28+
border-top-right-radius: 9px;
29+
background-color: white;
30+
padding: 7px 12% 0;
31+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32+
}
33+
34+
.rails-default-error-page h1 {
35+
font-size: 100%;
36+
color: #730E15;
37+
line-height: 1.5em;
38+
}
39+
40+
.rails-default-error-page div.dialog > p {
41+
margin: 0 0 1em;
42+
padding: 1em;
43+
background-color: #F7F7F7;
44+
border: 1px solid #CCC;
45+
border-right-color: #999;
46+
border-left-color: #999;
47+
border-bottom-color: #999;
48+
border-bottom-left-radius: 4px;
49+
border-bottom-right-radius: 4px;
50+
border-top-color: #DADADA;
51+
color: #666;
52+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53+
}
54+
</style>
55+
</head>
56+
57+
<body class="rails-default-error-page">
58+
<!-- This file lives in public/422.html -->
59+
<div class="dialog">
60+
<div>
61+
<h1>The change you wanted was rejected.</h1>
62+
<p>Maybe you tried to change something you didn't have access to.</p>
63+
</div>
64+
<p>If you are the application owner check the logs for more information.</p>
65+
</div>
66+
</body>
67+
</html>

app/assets/statics/500.html

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>We're sorry, but something went wrong (500)</title>
5+
<meta name="viewport" content="width=device-width,initial-scale=1">
6+
<style>
7+
.rails-default-error-page {
8+
background-color: #EFEFEF;
9+
color: #2E2F30;
10+
text-align: center;
11+
font-family: arial, sans-serif;
12+
margin: 0;
13+
}
14+
15+
.rails-default-error-page div.dialog {
16+
width: 95%;
17+
max-width: 33em;
18+
margin: 4em auto 0;
19+
}
20+
21+
.rails-default-error-page div.dialog > div {
22+
border: 1px solid #CCC;
23+
border-right-color: #999;
24+
border-left-color: #999;
25+
border-bottom-color: #BBB;
26+
border-top: #B00100 solid 4px;
27+
border-top-left-radius: 9px;
28+
border-top-right-radius: 9px;
29+
background-color: white;
30+
padding: 7px 12% 0;
31+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32+
}
33+
34+
.rails-default-error-page h1 {
35+
font-size: 100%;
36+
color: #730E15;
37+
line-height: 1.5em;
38+
}
39+
40+
.rails-default-error-page div.dialog > p {
41+
margin: 0 0 1em;
42+
padding: 1em;
43+
background-color: #F7F7F7;
44+
border: 1px solid #CCC;
45+
border-right-color: #999;
46+
border-left-color: #999;
47+
border-bottom-color: #999;
48+
border-bottom-left-radius: 4px;
49+
border-bottom-right-radius: 4px;
50+
border-top-color: #DADADA;
51+
color: #666;
52+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53+
}
54+
</style>
55+
</head>
56+
57+
<body class="rails-default-error-page">
58+
<!-- This file lives in public/500.html -->
59+
<div class="dialog">
60+
<div>
61+
<h1>We're sorry, but something went wrong.</h1>
62+
</div>
63+
<p>If you are the application owner check the logs for more information.</p>
64+
</div>
65+
</body>
66+
</html>

app/assets/statics/apple-touch-icon-precomposed.png

Loading

app/assets/statics/apple-touch-icon.png

Loading

app/assets/statics/favicon.ico

Whitespace-only changes.

app/assets/statics/robots.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
+11
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
class ApplicationController < ActionController::Base
2+
#例外処理
3+
rescue_from ActiveRecord::RecordNotFound, with: :render404
4+
before_action :log_params, if: ->{Rails.env.development?}
5+
6+
def render404
7+
render file: Rails.root.join("public/404.html"), layout: false, status: :not_found
8+
end
9+
10+
def log_params
11+
Rails.logger.debug params.inspect
12+
end
213
end

app/controllers/users_controller.rb

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
class UsersController < ApplicationController
2-
before_action :set_user, only: [:show, :edit, :update, :destroy]
2+
before_action :set_user!, only: [:show, :edit, :update]
33

44
# GET /users
55
# GET /users.json
66
def index
7-
@users = User.all
7+
@users = User.available.all
8+
@users.each do |user|
9+
user.archived = false
10+
end
811
end
912

1013
# GET /users/1
@@ -54,17 +57,22 @@ def update
5457
# DELETE /users/1
5558
# DELETE /users/1.json
5659
def destroy
57-
@user.destroy
58-
respond_to do |format|
59-
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
60-
format.json { head :no_content }
60+
params[:id].split(',').each do |id|
61+
User.find(id).unavailable!
6162
end
63+
redirect_to action: :index
64+
65+
# @user.destroy
66+
# respond_to do |format|
67+
# format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
68+
# format.json { head :no_content }
69+
# end
6270
end
6371

6472
private
6573
# Use callbacks to share common setup or constraints between actions.
66-
def set_user
67-
@user = User.find(params[:id])
74+
def set_user!
75+
@user = User.available.find(params[:id])
6876
end
6977

7078
# Never trust parameters from the scary internet, only allow the white list through.

app/models/user.rb

+17
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
class User < ApplicationRecord
2+
attr_accessor :archived
3+
4+
scope :available, ->{where archived_at: nil}
5+
6+
def available?
7+
return true unless archived_at
8+
false
9+
end
10+
11+
def unavailable
12+
self.archived_at = Time.zone.now unless archived_at
13+
end
14+
15+
def unavailable!
16+
unavailable
17+
save!
18+
end
219
end

app/views/users/index.html.erb

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1+
<%= javascript_include_tag 'users/index.js' %>
12
<p id="notice"><%= notice %></p>
23

34
<h1>Users</h1>
45

6+
<%= form_tag users_path, id: :form do %>
7+
<input type="hidden" name="_method" value="get" />
58
<table>
69
<thead>
710
<tr>
811
<th>Name</th>
912
<th>Email</th>
10-
<th colspan="3"></th>
13+
<th colspan="2"></th>
14+
<th>Delete</th>
1115
</tr>
1216
</thead>
1317

1418
<tbody>
15-
<% @users.each do |user| %>
16-
<tr>
17-
<td><%= user.name %></td>
18-
<td><%= user.email %></td>
19-
<td><%= link_to 'Show', user %></td>
20-
<td><%= link_to 'Edit', edit_user_path(user) %></td>
21-
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
22-
</tr>
23-
<% end %>
19+
<% @users.each do |user| %>
20+
<tr>
21+
<td><%= user.name %></td>
22+
<td><%= user.email %></td>
23+
<td><%= link_to 'Show', user %></td>
24+
<td><%= link_to 'Edit', edit_user_path(user) %></td>
25+
<td><%= check_box_tag 'id[]', user.id, user.archived %></td>
26+
</tr>
27+
<% end %>
2428
</tbody>
2529
</table>
2630

2731
<br>
2832

2933
<%= link_to 'New User', new_user_path %>
34+
<button type="button" onclick="submitFnc();submit()">Delete</button>
35+
<% end %>

config/environments/development.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@
5252
# Suppress logger output for asset requests.
5353
config.assets.quiet = true
5454

55+
# precompileするJSファイル
56+
config.assets.precompile += ['common/**/*']
57+
5558
# Raises error for missing translations
5659
# config.action_view.raise_on_missing_translations = true
5760

5861
# Use an evented file watcher to asynchronously detect changes in source code,
5962
# routes, locales, etc. This feature depends on the listen gem.
6063
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
61-
62-
config.Logger = Logger.new(STDOUT)
63-
64+
logger = ActiveSupport::Logger.new(STDOUT)
65+
logger.formatter = config.log_formatter
66+
config.logger = ActiveSupport::TaggedLogging.new(logger)
6467
end

config/environments/production.rb

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
# Use default logging formatter so that PID and timestamp are not suppressed.
8080
config.log_formatter = ::Logger::Formatter.new
8181

82+
# precompileするJSファイル
83+
config.assets.precompile += ['common/**/*']
84+
8285
# Use a different logger for distributed setups.
8386
# require 'syslog/logger'
8487
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

0 commit comments

Comments
 (0)