teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

内容の追加

2020/11/06 00:43

投稿

kei__3
kei__3

スコア7

title CHANGED
File without changes
body CHANGED
@@ -86,62 +86,7 @@
86
86
  ...まだ続きます
87
87
  ```
88
88
 
89
- ### requests/comments_requests_spec
90
89
 
91
- ```ここに言語名を入力
92
- require 'rails_helper'
93
-
94
- RSpec.describe "Comments", type: :request do
95
- let(:user) { FactoryBot.create(:user) }
96
- let(:other_user) { FactoryBot.create(:user, email: "otheruser@example.com") }
97
- let(:guest_user) { FactoryBot.create(:user, email: "guest@example.com") }
98
- let(:post_image) { FactoryBot.create(:post, :post_image, user: user) }
99
- let!(:text) { FactoryBot.create(:comment, post: post_image, user: user) }
100
-
101
- describe "#create" do
102
-
103
- # ログインしているユーザー
104
- context "as an authenticated user" do
105
- # 正常なレスポンスを返すこと
106
- it "responds successfully" do
107
- sign_in_as user
108
- post post_comments_path(post_image), params: {comment: {text: text}, user_id: user.id, post_id: post_image.id },xhr: true
109
- expect(response).to be_successful
110
- expect(response).to have_http_status "200"
111
- end
112
- end
113
-
114
- # ゲストユーザーの場合
115
- context "as an guest user" do
116
- # 投稿にコメントできないこと
117
- it "is can't comment on a post" do
118
- sign_in_as guest_user
119
- expect {
120
- post post_comments_path(post_image), params: {comment: {text: text}, user_id: guest_user.id, post_id: post_image.id }
121
- }.to_not change(Comment, :count)
122
- end
123
- end
124
- end
125
-
126
- describe "#destroy" do
127
-
128
- # 違うユーザーの場合
129
- context "as other user" do
130
- # コメントを削除できない
131
- it "is can't delete comment" do
132
- sign_in_as other_user
133
- expect{
134
- delete post_comment_path(post_image.id,text)
135
- }.to_not change(Comment, :count)
136
- end
137
- end
138
- end
139
-
140
-
141
- end
142
-
143
-
144
- ```
145
90
  ### spec/models/user_spec.rb
146
91
 
147
92
  ```
@@ -200,11 +145,42 @@
200
145
  ...
201
146
 
202
147
  ```
148
+ ###app/models/post.rb
149
+ ```
203
150
 
151
+ class Post < ApplicationRecord
152
+ belongs_to :user
153
+ has_many :likes, dependent: :destroy
154
+ has_many :liked_users, through: :likes, source: :user
155
+ has_many :comments, dependent: :destroy
156
+ has_one_attached :image
157
+ default_scope -> { order(created_at: :desc) }
158
+ validates :user_id, presence: true
159
+ validates :name, presence: true, length: { maximum:50 }
160
+ validates :content, presence: true, length: { maximum:140 }
161
+ validates :image, presence:true,
162
+ content_type: { in: %w[image/jpeg image/gif image/png],
163
+ message: "有効な画像形式である必要があります" },
164
+ size: { less_than: 5.megabytes,
165
+ message: "5MB未満である必要があります" }
166
+ def display_image
167
+ image.variant(resize_to_fill:[280,600])
168
+ end
204
169
 
170
+ def like_by?(user)
171
+ likes.where(user_id: user_id).exist?
172
+ end
173
+ def self.sort_like
174
+ Post.all.sort{|a,b| b.liked_users.count <=> a.liked_users.count}
175
+ end
176
+ end
205
177
 
178
+ ```
206
179
 
207
180
 
181
+
182
+
183
+
208
184
  ### 補足情報(FW/ツールのバージョンなど)
209
185
  Rails 6.0.3.2
210
186
  ruby 2.7.1

1

内容の変更

2020/11/06 00:43

投稿

kei__3
kei__3

スコア7

title CHANGED
File without changes
body CHANGED
@@ -86,7 +86,7 @@
86
86
  ...まだ続きます
87
87
  ```
88
88
 
89
- ### requests/comments/requests_spec
89
+ ### requests/comments_requests_spec
90
90
 
91
91
  ```ここに言語名を入力
92
92
  require 'rails_helper'
@@ -142,9 +142,69 @@
142
142
 
143
143
 
144
144
  ```
145
+ ### spec/models/user_spec.rb
145
146
 
147
+ ```
146
148
 
149
+ require 'rails_helper'
147
150
 
151
+
152
+ RSpec.describe User, type: :model do
153
+ let(:user) { FactoryBot.create(:user) }
154
+ let(:other_user) { FactoryBot.create(:user, email: "other_user@example.com") }
155
+
156
+ .
157
+ .
158
+ .
159
+
160
+ # フォローしていないユーザーの投稿は表示されない
161
+ it "is posts from unfollowed users are not displayed" do
162
+ FactoryBot.create(:other_user)
163
+ FactoryBot.create(:post, :post_image, user: other_user)
164
+ other_user.posts.each do |unfollowed|
165
+ expect(user.feed).to_not include(unfollowed)
166
+ end
167
+ end
168
+ end
169
+
170
+ ```
171
+
172
+ ###app/models/user.rb
173
+
174
+ ```
175
+ class User < ApplicationRecord
176
+ has_one_attached :icon
177
+ has_many :likes, dependent: :destroy
178
+ has_many :liked_posts, through: :likes, source: :user
179
+ has_many :posts, dependent: :destroy
180
+ has_many :comments, dependent: :destroy
181
+ has_many :active_relationships, class_name: "Relationship",
182
+ foreign_key: "follower_id",
183
+ dependent: :destroy
184
+ has_many :passive_relationships, class_name: "Relationship",
185
+ foreign_key: "followed_id",
186
+ dependent: :destroy
187
+ has_many :following, through: :active_relationships, source: :followed
188
+ has_many :followers, through: :passive_relationships
189
+ attr_accessor :remember_token, :activation_token, :reset_token
190
+ before_save :downcase_email
191
+ before_create :create_activation_digest
192
+ validates :name, presence: true, length: { maximum:15 }
193
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
194
+ validates :email, presence: true, length: {maximum:255 },
195
+ format: { with: VALID_EMAIL_REGEX },
196
+ uniqueness: true
197
+ has_secure_password
198
+ validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
199
+
200
+ ...
201
+
202
+ ```
203
+
204
+
205
+
206
+
207
+
148
208
  ### 補足情報(FW/ツールのバージョンなど)
149
209
  Rails 6.0.3.2
150
210
  ruby 2.7.1