質問編集履歴

2

内容の追加

2020/11/06 00:43

投稿

kei__3
kei__3

スコア7

test CHANGED
File without changes
test CHANGED
@@ -174,232 +174,184 @@
174
174
 
175
175
 
176
176
 
177
+
178
+
177
- ### requests/comments_requests_spec
179
+ ### spec/models/user_spec.rb
178
-
179
-
180
-
180
+
181
+
182
+
181
- ```ここに言語名を入力
183
+ ```
184
+
185
+
182
186
 
183
187
  require 'rails_helper'
184
188
 
185
189
 
186
190
 
191
+
192
+
187
- RSpec.describe "Comments", type: :request do
193
+ RSpec.describe User, type: :model do
188
194
 
189
195
  let(:user) { FactoryBot.create(:user) }
190
196
 
191
- let(:other_user) { FactoryBot.create(:user, email: "otheruser@example.com") }
197
+ let(:other_user) { FactoryBot.create(:user, email: "other_user@example.com") }
198
+
199
+
200
+
192
-
201
+ .
202
+
203
+ .
204
+
205
+ .
206
+
207
+
208
+
209
+ # フォローしていないユーザーの投稿は表示されない
210
+
193
- let(:guest_user) { FactoryBot.create(:user, email: "guest@example.com") }
211
+ it "is posts from unfollowed users are not displayed" do
212
+
194
-
213
+ FactoryBot.create(:other_user)
214
+
195
- let(:post_image) { FactoryBot.create(:post, :post_image, user: user) }
215
+ FactoryBot.create(:post, :post_image, user: other_user)
196
-
197
- let!(:text) { FactoryBot.create(:comment, post: post_image, user: user) }
216
+
198
-
199
-
200
-
201
- describe "#create" do
202
-
203
-
204
-
205
- # ログインしているユーザー
206
-
207
- context "as an authenticated user" do
208
-
209
- # 正常なレスポンスを返すこと
210
-
211
- it "responds successfully" do
217
+ other_user.posts.each do |unfollowed|
212
-
213
- sign_in_as user
218
+
214
-
215
- post post_comments_path(post_image), params: {comment: {text: text}, user_id: user.id, post_id: post_image.id },xhr: true
216
-
217
- expect(response).to be_successful
219
+ expect(user.feed).to_not include(unfollowed)
218
-
219
- expect(response).to have_http_status "200"
220
-
221
- end
222
220
 
223
221
  end
224
222
 
225
-
226
-
227
- # ゲストユーザーの場合
228
-
229
- context "as an guest user" do
230
-
231
- # 投稿にコメントできないこと
232
-
233
- it "is can't comment on a post" do
234
-
235
- sign_in_as guest_user
236
-
237
- expect {
238
-
239
- post post_comments_path(post_image), params: {comment: {text: text}, user_id: guest_user.id, post_id: post_image.id }
240
-
241
- }.to_not change(Comment, :count)
242
-
243
- end
244
-
245
- end
246
-
247
223
  end
248
224
 
249
-
250
-
251
- describe "#destroy" do
252
-
253
-
254
-
255
- # 違うユーザーの場合
256
-
257
- context "as other user" do
258
-
259
- # コメントを削除できない
260
-
261
- it "is can't delete comment" do
262
-
263
- sign_in_as other_user
264
-
265
- expect{
266
-
267
- delete post_comment_path(post_image.id,text)
268
-
269
- }.to_not change(Comment, :count)
270
-
271
- end
272
-
273
- end
225
+ end
226
+
227
+
228
+
229
+ ```
230
+
231
+
232
+
233
+ ###app/models/user.rb
234
+
235
+
236
+
237
+ ```
238
+
239
+ class User < ApplicationRecord
240
+
241
+ has_one_attached :icon
242
+
243
+ has_many :likes, dependent: :destroy
244
+
245
+ has_many :liked_posts, through: :likes, source: :user
246
+
247
+ has_many :posts, dependent: :destroy
248
+
249
+ has_many :comments, dependent: :destroy
250
+
251
+ has_many :active_relationships, class_name: "Relationship",
252
+
253
+ foreign_key: "follower_id",
254
+
255
+ dependent: :destroy
256
+
257
+ has_many :passive_relationships, class_name: "Relationship",
258
+
259
+ foreign_key: "followed_id",
260
+
261
+ dependent: :destroy
262
+
263
+ has_many :following, through: :active_relationships, source: :followed
264
+
265
+ has_many :followers, through: :passive_relationships
266
+
267
+ attr_accessor :remember_token, :activation_token, :reset_token
268
+
269
+ before_save :downcase_email
270
+
271
+ before_create :create_activation_digest
272
+
273
+ validates :name, presence: true, length: { maximum:15 }
274
+
275
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
276
+
277
+ validates :email, presence: true, length: {maximum:255 },
278
+
279
+ format: { with: VALID_EMAIL_REGEX },
280
+
281
+ uniqueness: true
282
+
283
+ has_secure_password
284
+
285
+ validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
286
+
287
+
288
+
289
+ ...
290
+
291
+
292
+
293
+ ```
294
+
295
+ ###app/models/post.rb
296
+
297
+ ```
298
+
299
+
300
+
301
+ class Post < ApplicationRecord
302
+
303
+ belongs_to :user
304
+
305
+ has_many :likes, dependent: :destroy
306
+
307
+ has_many :liked_users, through: :likes, source: :user
308
+
309
+ has_many :comments, dependent: :destroy
310
+
311
+ has_one_attached :image
312
+
313
+ default_scope -> { order(created_at: :desc) }
314
+
315
+ validates :user_id, presence: true
316
+
317
+ validates :name, presence: true, length: { maximum:50 }
318
+
319
+ validates :content, presence: true, length: { maximum:140 }
320
+
321
+ validates :image, presence:true,
322
+
323
+ content_type: { in: %w[image/jpeg image/gif image/png],
324
+
325
+ message: "有効な画像形式である必要があります" },
326
+
327
+ size: { less_than: 5.megabytes,
328
+
329
+ message: "5MB未満である必要があります" }
330
+
331
+ def display_image
332
+
333
+ image.variant(resize_to_fill:[280,600])
274
334
 
275
335
  end
276
336
 
277
337
 
278
338
 
279
-
339
+ def like_by?(user)
340
+
341
+ likes.where(user_id: user_id).exist?
342
+
343
+ end
344
+
345
+ def self.sort_like
346
+
347
+ Post.all.sort{|a,b| b.liked_users.count <=> a.liked_users.count}
348
+
349
+ end
280
350
 
281
351
  end
282
352
 
283
353
 
284
354
 
285
-
286
-
287
- ```
288
-
289
- ### spec/models/user_spec.rb
290
-
291
-
292
-
293
- ```
294
-
295
-
296
-
297
- require 'rails_helper'
298
-
299
-
300
-
301
-
302
-
303
- RSpec.describe User, type: :model do
304
-
305
- let(:user) { FactoryBot.create(:user) }
306
-
307
- let(:other_user) { FactoryBot.create(:user, email: "other_user@example.com") }
308
-
309
-
310
-
311
- .
312
-
313
- .
314
-
315
- .
316
-
317
-
318
-
319
- # フォローしていないユーザーの投稿は表示されない
320
-
321
- it "is posts from unfollowed users are not displayed" do
322
-
323
- FactoryBot.create(:other_user)
324
-
325
- FactoryBot.create(:post, :post_image, user: other_user)
326
-
327
- other_user.posts.each do |unfollowed|
328
-
329
- expect(user.feed).to_not include(unfollowed)
330
-
331
- end
332
-
333
- end
334
-
335
- end
336
-
337
-
338
-
339
- ```
340
-
341
-
342
-
343
- ###app/models/user.rb
344
-
345
-
346
-
347
- ```
348
-
349
- class User < ApplicationRecord
350
-
351
- has_one_attached :icon
352
-
353
- has_many :likes, dependent: :destroy
354
-
355
- has_many :liked_posts, through: :likes, source: :user
356
-
357
- has_many :posts, dependent: :destroy
358
-
359
- has_many :comments, dependent: :destroy
360
-
361
- has_many :active_relationships, class_name: "Relationship",
362
-
363
- foreign_key: "follower_id",
364
-
365
- dependent: :destroy
366
-
367
- has_many :passive_relationships, class_name: "Relationship",
368
-
369
- foreign_key: "followed_id",
370
-
371
- dependent: :destroy
372
-
373
- has_many :following, through: :active_relationships, source: :followed
374
-
375
- has_many :followers, through: :passive_relationships
376
-
377
- attr_accessor :remember_token, :activation_token, :reset_token
378
-
379
- before_save :downcase_email
380
-
381
- before_create :create_activation_digest
382
-
383
- validates :name, presence: true, length: { maximum:15 }
384
-
385
- VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
386
-
387
- validates :email, presence: true, length: {maximum:255 },
388
-
389
- format: { with: VALID_EMAIL_REGEX },
390
-
391
- uniqueness: true
392
-
393
- has_secure_password
394
-
395
- validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
396
-
397
-
398
-
399
- ...
400
-
401
-
402
-
403
355
  ```
404
356
 
405
357
 

1

内容の変更

2020/11/06 00:43

投稿

kei__3
kei__3

スコア7

test CHANGED
File without changes
test CHANGED
@@ -174,7 +174,7 @@
174
174
 
175
175
 
176
176
 
177
- ### requests/comments/requests_spec
177
+ ### requests/comments_requests_spec
178
178
 
179
179
 
180
180
 
@@ -286,6 +286,126 @@
286
286
 
287
287
  ```
288
288
 
289
+ ### spec/models/user_spec.rb
290
+
291
+
292
+
293
+ ```
294
+
295
+
296
+
297
+ require 'rails_helper'
298
+
299
+
300
+
301
+
302
+
303
+ RSpec.describe User, type: :model do
304
+
305
+ let(:user) { FactoryBot.create(:user) }
306
+
307
+ let(:other_user) { FactoryBot.create(:user, email: "other_user@example.com") }
308
+
309
+
310
+
311
+ .
312
+
313
+ .
314
+
315
+ .
316
+
317
+
318
+
319
+ # フォローしていないユーザーの投稿は表示されない
320
+
321
+ it "is posts from unfollowed users are not displayed" do
322
+
323
+ FactoryBot.create(:other_user)
324
+
325
+ FactoryBot.create(:post, :post_image, user: other_user)
326
+
327
+ other_user.posts.each do |unfollowed|
328
+
329
+ expect(user.feed).to_not include(unfollowed)
330
+
331
+ end
332
+
333
+ end
334
+
335
+ end
336
+
337
+
338
+
339
+ ```
340
+
341
+
342
+
343
+ ###app/models/user.rb
344
+
345
+
346
+
347
+ ```
348
+
349
+ class User < ApplicationRecord
350
+
351
+ has_one_attached :icon
352
+
353
+ has_many :likes, dependent: :destroy
354
+
355
+ has_many :liked_posts, through: :likes, source: :user
356
+
357
+ has_many :posts, dependent: :destroy
358
+
359
+ has_many :comments, dependent: :destroy
360
+
361
+ has_many :active_relationships, class_name: "Relationship",
362
+
363
+ foreign_key: "follower_id",
364
+
365
+ dependent: :destroy
366
+
367
+ has_many :passive_relationships, class_name: "Relationship",
368
+
369
+ foreign_key: "followed_id",
370
+
371
+ dependent: :destroy
372
+
373
+ has_many :following, through: :active_relationships, source: :followed
374
+
375
+ has_many :followers, through: :passive_relationships
376
+
377
+ attr_accessor :remember_token, :activation_token, :reset_token
378
+
379
+ before_save :downcase_email
380
+
381
+ before_create :create_activation_digest
382
+
383
+ validates :name, presence: true, length: { maximum:15 }
384
+
385
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
386
+
387
+ validates :email, presence: true, length: {maximum:255 },
388
+
389
+ format: { with: VALID_EMAIL_REGEX },
390
+
391
+ uniqueness: true
392
+
393
+ has_secure_password
394
+
395
+ validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
396
+
397
+
398
+
399
+ ...
400
+
401
+
402
+
403
+ ```
404
+
405
+
406
+
407
+
408
+
289
409
 
290
410
 
291
411