add limit of post per day
This commit is contained in:
@@ -5,6 +5,8 @@ class Post < ApplicationRecord
|
||||
validates :content, length: { maximum: 777 }
|
||||
validates :content, presence: true, if: :quoted_post?
|
||||
|
||||
validate :user, :limit_of_post_per_day
|
||||
|
||||
def kind
|
||||
if quoted_post?
|
||||
:quoted_post
|
||||
@@ -24,4 +26,12 @@ class Post < ApplicationRecord
|
||||
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)
|
||||
|
||||
if posts_from_day.count >= 5
|
||||
errors.add(:base, 'You can post only 5 posts per day')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,4 +7,6 @@ class User < ApplicationRecord
|
||||
format: {
|
||||
with: /\A[a-zA-Z0-9]+\z/,
|
||||
}
|
||||
|
||||
has_many :posts
|
||||
end
|
||||
|
||||
@@ -7,6 +7,8 @@ class UserFollow < ApplicationRecord
|
||||
private
|
||||
|
||||
def user_cant_follow_himself
|
||||
errors.add(:followed, 'can\'t follow himself') if follower_id == followed_id
|
||||
if follower_id == followed_id
|
||||
errors.add(:followed, 'can\'t follow himself')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user