implement current_user
This commit is contained in:
@@ -1,2 +1,5 @@
|
||||
class ApplicationController < ActionController::API
|
||||
def current_user
|
||||
@current_user ||= User.find_by(id: request.cookies['user_id'])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,6 +27,9 @@ class PostsController < ApplicationController
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
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
|
||||
|
||||
@@ -21,6 +21,9 @@ class UserFollowsController < ApplicationController
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
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
|
||||
|
||||
@@ -3,30 +3,26 @@ class Post < ApplicationRecord
|
||||
belongs_to :quoted_post, optional: true, class_name: 'Post'
|
||||
|
||||
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
|
||||
|
||||
def repost?
|
||||
kind == :quoted_post
|
||||
end
|
||||
|
||||
def kind
|
||||
if quoted_post?
|
||||
:quoted_post
|
||||
elsif repost?
|
||||
if quoted_post_id.blank?
|
||||
:post
|
||||
elsif content.blank? && quoted_post_id.present?
|
||||
:repost
|
||||
else
|
||||
:post
|
||||
:quoted_post
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def quoted_post?
|
||||
content.present? && quoted_post_id
|
||||
end
|
||||
|
||||
def repost?
|
||||
content.blank? && quoted_post_id
|
||||
end
|
||||
|
||||
def limit_of_post_per_day
|
||||
posts_from_day = user&.posts&.where('created_at >= ?', Time.zone.now.beginning_of_day)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user