Sha256: 9a3fb947857d4ee02a1c3fe6018d3bf8adf74878882d3d0751c97263e053f80b
Contents?: true
Size: 1.49 KB
Versions: 1
Compression:
Stored size: 1.49 KB
Contents
# frozen_string_literal: true module Spina module Admin::Conferences::Blog # Spina::Blog::Post class Post < ApplicationRecord extend FriendlyId friendly_id :title, use: :slugged belongs_to :image, optional: true, class_name: 'Spina::Image' belongs_to :user belongs_to :category, inverse_of: :posts validates :title, :content, presence: true before_save :set_published_at # Create a 301 redirect if the slug changed after_update :rewrite_rule, if: -> { saved_change_to_slug? } scope :available, -> { where('published_at <= ?', Time.zone.now) } scope :future, -> { where('published_at >= ?', Time.zone.now) } scope :draft, -> { where(draft: true) } scope :live, -> { where(draft: false) } scope :featured, -> { where(featured: true) } scope :unfeatured, -> { where(featured: false) } translates :title, fallbacks: true translates :description, :excerpt, :content translates :seo_title, default: -> { title } def self.table_name 'spina_blog_posts' end private def set_published_at self.published_at = Time.now if !draft? && published_at.blank? end def should_generate_new_friendly_id? slug.blank? || draft_changed? || super end def rewrite_rule ::Spina::RewriteRule.create( old_path: "/blog/posts/#{slug_before_last_save}", new_path: "/blog/posts/#{slug}" ) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
spina-admin-conferences-blog-0.3.1 | app/models/spina/admin/conferences/blog/post.rb |