Files
astuto/app/models/post.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

2019-08-24 16:51:25 +02:00
class Post < ApplicationRecord
2022-07-18 10:47:54 +02:00
include TenantOwnable
2019-08-24 16:51:25 +02:00
belongs_to :board
belongs_to :user
belongs_to :post_status, optional: true
2019-09-27 12:32:30 +02:00
has_many :likes, dependent: :destroy
has_many :follows, dependent: :destroy
has_many :followers, through: :follows, source: :user
2019-09-24 21:16:51 +02:00
has_many :comments, dependent: :destroy
has_many :post_status_changes, dependent: :destroy
2019-08-24 16:51:25 +02:00
validates :title, presence: true, length: { in: 4..128 }
2019-09-04 21:12:07 +02:00
paginates_per Rails.application.posts_per_page
2019-09-05 17:11:07 +02:00
class << self
def find_with_post_status_in(post_statuses)
where(post_status_id: post_statuses.pluck(:id))
end
def search_by_name_or_description(s)
s = s || ''
s = sanitize_sql_like(s)
where("posts.title ILIKE ? OR posts.description ILIKE ?", "%#{s}%", "%#{s}%")
end
2024-01-26 17:43:24 +01:00
def order_by(sort_by)
case sort_by
when 'newest'
order(created_at: :desc)
when 'trending'
order(hotness: :desc)
when 'most_voted'
order(likes_count: :desc)
when 'oldest'
order(created_at: :asc)
else
order(created_at: :desc)
end
end
2019-09-05 17:11:07 +02:00
end
2019-08-24 16:51:25 +02:00
end