implement current_user
This commit is contained in:
@@ -1,2 +1,5 @@
|
|||||||
class ApplicationController < ActionController::API
|
class ApplicationController < ActionController::API
|
||||||
|
def current_user
|
||||||
|
@current_user ||= User.find_by(id: request.cookies['user_id'])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class PostsController < ApplicationController
|
|||||||
|
|
||||||
# Only allow a list of trusted parameters through.
|
# Only allow a list of trusted parameters through.
|
||||||
def post_params
|
def post_params
|
||||||
params.require(:post).permit(:content, :user_id, :quoted_post_id)
|
{
|
||||||
|
user: current_user,
|
||||||
|
**params.require(:post).permit(:content, :quoted_post_id)
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ class UserFollowsController < ApplicationController
|
|||||||
|
|
||||||
# Only allow a list of trusted parameters through.
|
# Only allow a list of trusted parameters through.
|
||||||
def user_follow_params
|
def user_follow_params
|
||||||
params.require(:user_follow).permit(:follower_id, :followed_id)
|
{
|
||||||
|
follower: current_user,
|
||||||
|
**params.require(:user_follow).permit(:followed_id)
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,30 +3,26 @@ class Post < ApplicationRecord
|
|||||||
belongs_to :quoted_post, optional: true, class_name: 'Post'
|
belongs_to :quoted_post, optional: true, class_name: 'Post'
|
||||||
|
|
||||||
validates :content, length: { maximum: 777 }
|
validates :content, length: { maximum: 777 }
|
||||||
validates :content, presence: true, if: :quoted_post?
|
validates :content, presence: true, unless: :repost?
|
||||||
|
|
||||||
validate :user, :limit_of_post_per_day
|
validate :user, :limit_of_post_per_day
|
||||||
|
|
||||||
|
def repost?
|
||||||
|
kind == :quoted_post
|
||||||
|
end
|
||||||
|
|
||||||
def kind
|
def kind
|
||||||
if quoted_post?
|
if quoted_post_id.blank?
|
||||||
:quoted_post
|
:post
|
||||||
elsif repost?
|
elsif content.blank? && quoted_post_id.present?
|
||||||
:repost
|
:repost
|
||||||
else
|
else
|
||||||
:post
|
:quoted_post
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def quoted_post?
|
|
||||||
content.present? && quoted_post_id
|
|
||||||
end
|
|
||||||
|
|
||||||
def repost?
|
|
||||||
content.blank? && quoted_post_id
|
|
||||||
end
|
|
||||||
|
|
||||||
def limit_of_post_per_day
|
def limit_of_post_per_day
|
||||||
posts_from_day = user&.posts&.where('created_at >= ?', Time.zone.now.beginning_of_day)
|
posts_from_day = user&.posts&.where('created_at >= ?', Time.zone.now.beginning_of_day)
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ RSpec.describe Post, type: :model do
|
|||||||
context 'when post have other post reference and a content' do
|
context 'when post have other post reference and a content' do
|
||||||
it 'returns :quoted_post' do
|
it 'returns :quoted_post' do
|
||||||
quoted_post = create(:post)
|
quoted_post = create(:post)
|
||||||
post = create(:post, quoted_post: quoted_post)
|
post = build(:post, quoted_post: quoted_post)
|
||||||
|
|
||||||
expect(post.kind).to eq(:quoted_post)
|
expect(post.kind).to eq(:quoted_post)
|
||||||
end
|
end
|
||||||
@@ -14,7 +14,7 @@ RSpec.describe Post, type: :model do
|
|||||||
context 'when post have other post reference and dont have a content' do
|
context 'when post have other post reference and dont have a content' do
|
||||||
it 'returns :repost' do
|
it 'returns :repost' do
|
||||||
quoted_post = create(:post)
|
quoted_post = create(:post)
|
||||||
post = create(:post, quoted_post: quoted_post, content: nil)
|
post = build(:post, quoted_post: quoted_post, content: nil)
|
||||||
|
|
||||||
expect(post.kind).to eq(:repost)
|
expect(post.kind).to eq(:repost)
|
||||||
end
|
end
|
||||||
@@ -22,7 +22,7 @@ RSpec.describe Post, type: :model do
|
|||||||
|
|
||||||
context 'when post dont have other post reference and have a content' do
|
context 'when post dont have other post reference and have a content' do
|
||||||
it 'returns :post' do
|
it 'returns :post' do
|
||||||
post = create(:post)
|
post = build(:post)
|
||||||
|
|
||||||
expect(post.kind).to eq(:post)
|
expect(post.kind).to eq(:post)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,18 +13,17 @@ require 'rails_helper'
|
|||||||
# sticking to rails and rspec-rails APIs to keep things simple and stable.
|
# sticking to rails and rspec-rails APIs to keep things simple and stable.
|
||||||
|
|
||||||
RSpec.describe "/posts", type: :request do
|
RSpec.describe "/posts", type: :request do
|
||||||
let(:quoted_post) { create(:post) }
|
let(:user) { create(:user) }
|
||||||
|
|
||||||
let(:valid_attributes) {
|
let(:valid_attributes) {
|
||||||
{
|
{
|
||||||
content: "Quo dolorem recusandae. Vero laborum deleniti. Qui ipsam illum.",
|
content: "Quo dolorem recusandae. Vero laborum deleniti. Qui ipsam illum.",
|
||||||
user_id: create(:user).id,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:invalid_attributes) {
|
let(:invalid_attributes) {
|
||||||
{
|
{
|
||||||
content: "Quo dolorem recusandae. Vero laborum deleniti. Qui ipsam illum."
|
content: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,12 +32,14 @@ RSpec.describe "/posts", type: :request do
|
|||||||
# PostsController, or in your router and rack
|
# PostsController, or in your router and rack
|
||||||
# middleware. Be sure to keep this updated too.
|
# middleware. Be sure to keep this updated too.
|
||||||
let(:valid_headers) {
|
let(:valid_headers) {
|
||||||
{}
|
{
|
||||||
|
Cookie: "user_id=#{user.id}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
describe "GET /index" do
|
describe "GET /index" do
|
||||||
it "renders a successful response" do
|
it "renders a successful response" do
|
||||||
Post.create! valid_attributes
|
create(:post)
|
||||||
get posts_url, headers: valid_headers, as: :json
|
get posts_url, headers: valid_headers, as: :json
|
||||||
expect(response).to be_successful
|
expect(response).to be_successful
|
||||||
end
|
end
|
||||||
@@ -46,7 +47,7 @@ RSpec.describe "/posts", type: :request do
|
|||||||
|
|
||||||
describe "GET /show" do
|
describe "GET /show" do
|
||||||
it "renders a successful response" do
|
it "renders a successful response" do
|
||||||
post = Post.create! valid_attributes
|
post = create(:post)
|
||||||
get post_url(post), as: :json
|
get post_url(post), as: :json
|
||||||
expect(response).to be_successful
|
expect(response).to be_successful
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,19 +7,19 @@ RSpec.describe "/user_follows", type: :request do
|
|||||||
let(:valid_attributes) {
|
let(:valid_attributes) {
|
||||||
{
|
{
|
||||||
followed_id: followed.id,
|
followed_id: followed.id,
|
||||||
follower_id: follower.id
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:invalid_attributes) {
|
let(:invalid_attributes) {
|
||||||
{
|
{
|
||||||
followed_id: followed.id,
|
followed_id: 'invalid_id',
|
||||||
follower_id: 'follower.id'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:valid_headers) {
|
let(:valid_headers) {
|
||||||
{}
|
{
|
||||||
|
Cookie: "user_id=#{follower.id}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
describe "POST /create" do
|
describe "POST /create" do
|
||||||
@@ -58,7 +58,7 @@ RSpec.describe "/user_follows", type: :request do
|
|||||||
|
|
||||||
describe "DELETE /destroy" do
|
describe "DELETE /destroy" do
|
||||||
it "destroys the requested user_follow" do
|
it "destroys the requested user_follow" do
|
||||||
user_follow = UserFollow.create! valid_attributes
|
user_follow = create(:user_follow, follower: follower, followed: followed)
|
||||||
expect {
|
expect {
|
||||||
delete user_follow_url(user_follow), headers: valid_headers, as: :json
|
delete user_follow_url(user_follow), headers: valid_headers, as: :json
|
||||||
}.to change(UserFollow, :count).by(-1)
|
}.to change(UserFollow, :count).by(-1)
|
||||||
|
|||||||
Reference in New Issue
Block a user