news_post.rb 498 B

12345678910111213141516171819
  1. class NewsPost < ApplicationRecord
  2. belongs_to :user
  3. validates :user_id, presence: true
  4. belongs_to :news_feed, polymorphic: true
  5. validates :news_feed_id, presence: true
  6. validates :name, presence: true, length: { maximum: 255 }
  7. validate :user_permission
  8. default_scope { order(created_at: :desc) }
  9. private
  10. def user_permission
  11. unless self.news_feed.can_post_news?(self.user)
  12. errors.add(:user, "does not have permission to post in this feed")
  13. end
  14. end
  15. end