Sha256: 17a617b1cd0ae7452d89cde29441eaa15a4913672601eba88c60d8afafcb978a

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

class Post < ActiveRecord::Base

  belongs_to :user
  
  santey_vote
  santey_view
  
  # tags examples: http://github.com/mbleigh/acts-as-taggable-on
  acts_as_taggable
  #acts_as_taggable_on :tags
  
  validates_presence_of :title, :message => "Please enter title"
  validates_presence_of :content, :message => "Please enter content"
  
  scope :published, where("#{self.table_name}.published = true")
  scope :published_since, lambda { |ago|
    published.where("#{self.table_name}.created_at >= ?", ago)
  }
  scope :recent, order("#{self.table_name}.created_at DESC")
  scope :top, joins([:votes]).select("distinct #{self.table_name}.*, count(votes.vote) AS count_votes").where("votes.vote = true").group(self.column_names.map{|i| "#{self.table_name}.#{i}"}.join(',')).order("count_votes DESC, #{self.table_name}.created_at DESC")
  
  scope :archives, published.select("COUNT(#{self.table_name}.id) AS count_posts, YEAR(#{self.table_name}.created_at) AS year, MONTH(#{self.table_name}.created_at) AS month").group("year, month").order("year DESC, month DESC")
  
  scope :with_dates, lambda { |y,m,d|
    where("#{self.table_name}.created_at between ? and ?", 
    			Time.parse("#{y || "2000"}-#{m || "01"}-#{d || "01"}"), 
    			Time.parse("#{y || Time.now.year}-#{m || "12"}-#{d || "31"}"))
  }
  
  
  class << self
    # example: Post.search('w').published_since(10.days.ago).collect(&:title)
    def search(q)
      [:title, :content].inject(scoped) do |combined_scope, attr|
        combined_scope.where("#{self.table_name}.#{attr} LIKE ?", "%#{q}%")
      end
    end
  end
  
  def before_save
    self.slug = title.gsub(/[^A-Za-z0-9\.\-]/, '_').downcase+"-#{rand(1000)}" if self.slug.nil?
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
santey_blog-0.2.1 app/models/post.rb
santey_blog-0.2.0 app/models/post.rb
santey_blog-0.1.2 app/models/post.rb