質問編集履歴
1
具体的な質問を記述しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,3 +1,140 @@
|
|
1
1
|
Ruby on Railsの10章の演習についてですが…
|
2
2
|
リスト10.57 10.58 10.59
|
3
|
-
に黄色にハイライトされた行を変更して行うものなのでしょうか?
|
3
|
+
に黄色にハイライトされた行を変更して行うものなのでしょうか?
|
4
|
+
|
5
|
+
すみません、あまりにも質問が曖昧すぎました
|
6
|
+
具体的なコードを教えていただけますでしょうか、よろしくお願いします。
|
7
|
+
|
8
|
+
|
9
|
+
1、以下のコードの一番下にあるtest "expired token" doを修正して、期限切れのパスワード再設定のブランチの統合テストを作成せよ、という演習です
|
10
|
+
assert_match /FILL_IN/i, response.body のFILL_INを期限と比較する旨のコードを記述すると思ったのですが、どのように書けばいいかわからないです
|
11
|
+
```Ruby
|
12
|
+
#test/integration/password_resets_test.rb
|
13
|
+
require 'test_helper'
|
14
|
+
|
15
|
+
class PasswordResetsTest < ActionDispatch::IntegrationTest
|
16
|
+
def setup
|
17
|
+
ActionMailer::Base.deliveries.clear
|
18
|
+
@user = users(:michael)
|
19
|
+
end
|
20
|
+
|
21
|
+
test "password resets" do
|
22
|
+
get new_password_reset_path
|
23
|
+
assert_template 'password_resets/new'
|
24
|
+
# メールアドレスが無効
|
25
|
+
post password_resets_path, password_reset: { email: "" }
|
26
|
+
assert_not flash.empty?
|
27
|
+
assert_template 'password_resets/new'
|
28
|
+
# メールアドレスが有効
|
29
|
+
post password_resets_path, password_reset: { email: @user.email }
|
30
|
+
assert_not_equal @user.reset_digest, @user.reload.reset_digest
|
31
|
+
assert_equal 1, ActionMailer::Base.deliveries.size
|
32
|
+
assert_not flash.empty?
|
33
|
+
assert_redirected_to root_url
|
34
|
+
# パスワード再設定用フォーム
|
35
|
+
user = assigns(:user)
|
36
|
+
# メールアドレスが無効
|
37
|
+
get edit_password_reset_path(user.reset_token, email: "")
|
38
|
+
assert_redirected_to root_url
|
39
|
+
# 無効なユーザー
|
40
|
+
user.toggle!(:activated)
|
41
|
+
get edit_password_reset_path(user.reset_token, email: user.email)
|
42
|
+
assert_redirected_to root_url
|
43
|
+
user.toggle!(:activated)
|
44
|
+
# メールアドレスが正しく、トークンが無効
|
45
|
+
get edit_password_reset_path('wrong token', email: user.email)
|
46
|
+
assert_redirected_to root_url
|
47
|
+
# メールアドレスもトークンも有効
|
48
|
+
get edit_password_reset_path(user.reset_token, email: user.email)
|
49
|
+
assert_template 'password_resets/edit'
|
50
|
+
assert_select "input[name=email][type=hidden][value=?]", user.email
|
51
|
+
# 無効なパスワードと確認
|
52
|
+
patch password_reset_path(user.reset_token),
|
53
|
+
email: user.email,
|
54
|
+
user: { password: "foobaz",
|
55
|
+
password_confirmation: "barquux" }
|
56
|
+
assert_select 'div#error_explanation'
|
57
|
+
# パスワードが空
|
58
|
+
patch password_reset_path(user.reset_token),
|
59
|
+
email: user.email,
|
60
|
+
user: { password: "",
|
61
|
+
password_confirmation: "" }
|
62
|
+
assert_select 'div#error_explanation'
|
63
|
+
# 有効なパスワードと確認
|
64
|
+
patch password_reset_path(user.reset_token),
|
65
|
+
email: user.email,
|
66
|
+
user: { password: "foobaz",
|
67
|
+
password_confirmation: "foobaz" }
|
68
|
+
assert is_logged_in?
|
69
|
+
assert_not flash.empty?
|
70
|
+
assert_redirected_to user
|
71
|
+
|
72
|
+
test "expired token" do
|
73
|
+
get new_password_reset_path
|
74
|
+
post password_resets_path, password_reset: { email: @user.email }
|
75
|
+
|
76
|
+
@user = assigns(:user)
|
77
|
+
@user.update_attribute(:reset_sent_at, 3.hours.ago)
|
78
|
+
patch password_reset_path(@user.reset_token),
|
79
|
+
email: @user.email,
|
80
|
+
user: { password: "foobar",
|
81
|
+
password_confirmation: "foobar" }
|
82
|
+
assert_response :redirect
|
83
|
+
follow_redirect!
|
84
|
+
assert_match /FILL_IN/i, response.body
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
```
|
89
|
+
|
90
|
+
|
91
|
+
2、以下のindexとshowを修正して有効なユーザーのみを表示するようにせよ、という問題です
|
92
|
+
indexはUser.where(activated: FILL_IN)のFILL_INをtureにすればいいと思いました
|
93
|
+
showは「空(nil)でない限り、indexを返す」という解釈でいいのでしょうか?
|
94
|
+
```Ruby
|
95
|
+
#app/controllers/users_controller.rb
|
96
|
+
|
97
|
+
class UsersController < ApplicationController
|
98
|
+
|
99
|
+
def index
|
100
|
+
@users = User.where(activated: FILL_IN).paginate(page: params[:page])
|
101
|
+
end
|
102
|
+
|
103
|
+
def show
|
104
|
+
@user = User.find(params[:id])
|
105
|
+
redirect_to root_url and return unless FILL_IN
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
```
|
111
|
+
|
112
|
+
|
113
|
+
3、activateメソッドとcreate_reset_digestメソッドの両方でupdate_attributeを呼び出しており、それぞれのアクセスによってデータベーストランザクションが個別に発生してしまう 以下のコードを埋めて、個別のupdate_attribute呼び出しを単一のupdate_columns呼び出しに統合し、データベースアクセスが1回で済むようにせよ、という問題です
|
114
|
+
activeのFILL_INにそれぞれ、trueと登録した日時(専用に呼び出す関数あり?)が入ると思います
|
115
|
+
create_reset_digestの1つ目のFILL_INは記憶トークン、2つ目にはメールを送信した日時が入ると考えています
|
116
|
+
```Ruby
|
117
|
+
|
118
|
+
class User < ActiveRecord::Base
|
119
|
+
attr_accessor :remember_token, :activation_token, :reset_token
|
120
|
+
before_save :downcase_email
|
121
|
+
before_create :create_activation_digest
|
122
|
+
|
123
|
+
# アカウントを有効にする
|
124
|
+
def activate
|
125
|
+
update_columns(activated: FILL_IN, activated_at: FILL_IN)
|
126
|
+
end
|
127
|
+
|
128
|
+
# 有効化用のメールを送信する
|
129
|
+
def send_activation_email
|
130
|
+
UserMailer.account_activation(self).deliver_now
|
131
|
+
end
|
132
|
+
|
133
|
+
# パスワード再設定の属性を設定する
|
134
|
+
def create_reset_digest
|
135
|
+
self.reset_token = User.new_token
|
136
|
+
update_columns(reset_digest: FILL_IN,
|
137
|
+
reset_sent_at: FILL_IN)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
```
|