Sha256: 7c232a112b2c7c7eb73ede5371db63601ac2588960532fc1aa750692971dd365

Contents?: true

Size: 1.72 KB

Versions: 8

Compression:

Stored size: 1.72 KB

Contents

class Post < ActiveRecord::Base
  # Scopes, Attachments, etc.
  attr_accessor :created_at_date, :created_at_time
  scope :posted, lambda { where("created_at <= ? AND published = ?", Time.now, true) }
  scope :for_archive, lambda { |year, month| where(Post.for_archive_conditions(year, month)) }
  default_scope { order("created_at DESC") }

  # Relationships
  has_and_belongs_to_many :post_categories
  can_have_comments

  # Validations
  validates_presence_of :title, :content, :excerpt
  validates_format_of :created_at_time, :with => /\d{1,2}:\d{2}[AaPp][Mm]/i, :message => "must be a valid 12-hour time."
  validates_format_of :created_at_date, :with => /\d{4}\-\d{1,2}\-\d{2}/, :message => "must match format of YYYY-MM-DD"

  before_save :set_created_at

  # open up everything for mass assignment
  attr_protected

  def to_param
    "#{id}-#{title.parameterize}"
  end

  def self.submenu
    "posts"
  end

  def category_list
    self.post_categories.map { |c| "<a href='/posts/#{c.id}/category'>#{c.title}</a>" }.join(', ').html_safe
  end

  # Achive month handling
  def archive_title
    self.created_at.strftime("%B %Y")
  end

  def archive_link
    self.created_at.strftime("/posts/%m/%Y")
  end

  def self.get_archive_months
    posts = self.posted.all
    return posts.map{|p| {:title => p.archive_title, :link => p.archive_link} }.uniq
  end

  # previous and next posts for mobile post nav
  def next
    Post.where("created_at > ?", created_at)[-1]
  end

  def previous
    Post.where("created_at < ?", created_at)[0]
  end

  def posted?
    self.published && self.created_at <= Time.now
  end

  private
  def set_created_at
    created_at_date && created_at_time && self.created_at = "#{created_at_date} #{created_at_time}"
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
forge-cli-0.1.10 lib/forge/app/models/post.rb
forge-cli-0.1.9 lib/forge/app/models/post.rb
forge-cli-0.1.8 lib/forge/app/models/post.rb
forge-cli-0.1.7 lib/forge/app/models/post.rb
forge-cli-0.1.6 lib/forge/app/models/post.rb
forge-cli-0.1.5 lib/forge/app/models/post.rb
forge-cli-0.1.4 lib/forge/app/models/post.rb
forge-cli-0.1.3 lib/forge/app/models/post.rb