Sha256: 3f2123741b95ce9864d6f3e3a2a3477a90160dd4be1f26ef2ec07d36db052ea7

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

class Comfy::Blog::Post < ActiveRecord::Base
  
  self.table_name = 'comfy_blog_posts'
  
  # -- Relationships --------------------------------------------------------
  belongs_to :blog
  
  has_many :comments,
    :dependent => :destroy
  
  # -- Validations ----------------------------------------------------------
  validates :blog_id, :title, :slug, :year, :month, :content,
    :presence   => true
  validates :slug,
    :uniqueness => { :scope => [:blog_id, :year, :month] },
    :format     => { :with => /\A\w[a-z0-9_-]*\z/i }
  
  # -- Scopes ---------------------------------------------------------------
  default_scope -> {
    order('published_at DESC')
  }
  scope :published, -> {
    where(:is_published => true)
  }
  scope :for_year, -> year {
    where(:year => year)
  }
  scope :for_month, -> month {
    where(:month => month)
  }
  
  # -- Callbacks ------------------------------------------------------------
  before_validation :set_slug,
                    :set_published_at,
                    :set_date
  
protected
  
  def set_slug
    self.slug ||= self.title.to_s.downcase.slugify
  end
  
  def set_date
    self.year   = self.published_at.year
    self.month  = self.published_at.month
  end
  
  def set_published_at
    self.published_at ||= Time.zone.now
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
comfy_blog-1.12.2 app/models/comfy/blog/post.rb
comfy_blog-1.12.1 app/models/comfy/blog/post.rb
comfy_blog-1.12.0 app/models/comfy/blog/post.rb