moonleerecords/moonlee-website

View on GitHub
app/models/post.rb

Summary

Maintainability
A
0 mins
Test Coverage
class Post < ApplicationRecord
  VISIBILITY_OPTIONS = %w(public hidden).freeze
  AVAILABLE_CATEGORIES = %w(news).freeze

  extend FriendlyId
  include YoutubeVideo

  friendly_id :custom_slug, use: :slugged

  paginates_per 20

  default_scope { order('published_at DESC') }

  has_attached_file :image,
                    styles: {
                      large: '1250x1250>',
                      medium: '400x400#',
                      small: '100x100#'
                    }

  validates :title, presence: true
  validates :body, presence: true
  validates :published_at, presence: true
  validates :visibility, inclusion: { in: VISIBILITY_OPTIONS }
  validates :categories, length: { minimum: 1 }
  validate :validate_categories

  validates_attachment_content_type :image, content_type: %r{\Aimage\/.*\Z}

  scope :published, -> { where('published_at <= ?', Time.zone.now).where(visibility: 'public') }
  scope :on_records, -> { where(records: true) }
  scope :on_booking, -> { where(booking: true) }
  scope :hidden, -> { where(records: false, booking: false) }

  before_validation :assign_published_at
  before_save :assign_default_image

  def youtube_iframe(width = '560', height = '315')
    "<iframe width='#{width}' height='#{height}' src='https://www.youtube.com/embed/#{youtube_video_id(youtube_video)}?modestbranding=1&autohide=1&showinfo=0&controls=0' frameborder='0' allowfullscreen></iframe>" if youtube_video.present?
  end

  def body
    self[:body].gsub('&nbsp;', ' ') if self[:body].present?
  end

  def tags_raw
    tags.join(', ') unless tags.nil?
  end

  def tags_raw=(values)
    self.tags = values.split(',').map(&:strip)
  end

  def self.visibility_options
    VISIBILITY_OPTIONS
  end

  def self.available_categories
    AVAILABLE_CATEGORIES
  end

  private

  def custom_slug
    "#{published_at.strftime('%Y/%m/%d')} #{title}"
  end

  def assign_published_at
    self.published_at = created_at if published_at.nil?
  end

  def assign_default_image
    self.image = open("https://img.youtube.com/vi/#{youtube_video_id(youtube_video)}/0.jpg") if youtube_video.present?
  end

  def validate_categories
    categories = self.categories.reject!(&:empty?)
    invalid_categories = Array(categories) - AVAILABLE_CATEGORIES
    errors.add(:categories, 'Not all the categories are valid') if invalid_categories.count > 0
  end
end