app/models/spud_post.rb in tb_blog-1.0.4 vs app/models/spud_post.rb in tb_blog-1.1.0
- old
+ new
@@ -1,58 +1,67 @@
class SpudPost < ActiveRecord::Base
spud_searchable
- has_and_belongs_to_many :categories,
+ has_many :spud_post_categories_posts
+ has_many :categories,
:class_name => 'SpudPostCategory',
- :join_table => 'spud_post_categories_posts',
- :foreign_key => 'spud_post_id'
+ :through => :spud_post_categories_posts,
+ :source => :spud_post_category
+
belongs_to :author, :class_name => 'SpudUser', :foreign_key => 'spud_user_id'
has_many :comments, :class_name => 'SpudPostComment', :inverse_of => :post
- has_many :pending_comments, :class_name => "SpudPostComment", :conditions => {:spam => [nil, false], :approved => false}
- has_many :visible_comments, :class_name => 'SpudPostComment',:conditions => {:spam => [nil,false], :approved => true}
- has_many :spam_comments, :class_name => "SpudPostComment", :conditions => {:spam => true}
+ has_many :pending_comments, ->{ where(:spam => [nil, false], :approved => false) }, :class_name => "SpudPostComment"
+ has_many :visible_comments, ->{ where(:spam => [nil,false], :approved => true) }, :class_name => 'SpudPostComment'
+ has_many :spam_comments, ->{ where(:spam => true) }, :class_name => "SpudPostComment"
has_many :spud_permalinks,:as => :attachment
has_many :spud_post_sites, :dependent => :destroy
- scope :publicly, where('visible = true AND published_at <= ?', Time.now.utc).order('published_at desc')
- scope :future_posts, where('visible = true AND published_at > ?', Time.now.utc)
+ scope :publicly, ->{ where('visible = true AND published_at <= ?', Time.now.utc).order('published_at desc') }
+ scope :future_posts, ->{ where('visible = true AND published_at > ?', Time.now.utc) }
+
+ scope :blog_posts, ->{ where(:is_news => false) }
+ scope :news_posts, ->{ where(:is_news => true) }
+ scope :visible, ->{ where('visible = true AND published_at <= ?', Time.now.utc) }
+ scope :ordered, ->{ order('published_at desc') }
+
validates_presence_of :title, :content, :published_at, :spud_user_id, :url_name
validates_uniqueness_of :url_name
before_validation :set_url_name
- before_save :postprocess_content
-
after_save :set_spud_site_ids
+ acts_as_spud_liquid_content
- attr_accessible :is_news,:published_at,:title,:content,:spud_user_id,:url_name,:visible,:comments_enabled,:meta_keywords,:meta_description,:category_ids, :spud_site_ids, :content_format
attr_accessor :spud_site_ids
def self.for_spud_site(spud_site_id)
return joins(:spud_post_sites).where(:spud_post_sites => {:spud_site_id => spud_site_id})
end
def self.public_posts(page, per_page)
+ ActiveSupport::Deprecation.warn "SpudPost.public_posts is deprecated. Please the :visible, and :ordered scopes instead.", caller
return where('visible = ? AND published_at <= ?', true,Time.now.utc).order('published_at desc').paginate(:page => page, :per_page => per_page)
end
def self.public_blog_posts(page, per_page)
+ ActiveSupport::Deprecation.warn "SpudPost.public_blog_posts is deprecated. Please the :blog_posts, :visible, and :ordered scopes instead.", caller
return self.public_posts(page, per_page).where(:is_news => false)
end
def self.public_news_posts(page, per_page)
+ ActiveSupport::Deprecation.warn "SpudPost.public_news_posts is deprecated. Please the :news_posts, :visible, and :ordered scopes instead.", caller
return self.public_posts(page, per_page).where(:is_news => true)
end
def self.recent_posts(limit=5)
return where('visible = ? AND published_at <= ?', true, Time.now.utc).order('published_at desc').limit(limit)
end
def self.recent_blog_posts(limit=5)
- return self.recent_posts(limit).where(:is_news => false)
+ return self.blog_posts.recent_posts(limit)
end
def self.recent_news_posts(limit=5)
- return self.recent_posts(limit).where(:is_news => true)
+ return self.news_posts.recent_posts(limit)
end
def self.from_archive(date_string)
begin
date = Date.strptime(date_string, "%Y-%b")
@@ -100,19 +109,19 @@
logger.fatal "Exception occurred while fetching post archive dates:\n #{e.to_s}"
end
end
def postprocess_content
- if self.content_format == 'Markdown'
- require 'redcarpet'
- renderer = Redcarpet::Render::HTML.new
- extensions = {fenced_code_blocks: true}
- redcarpet = Redcarpet::Markdown.new(renderer, extensions)
- self.content_processed = redcarpet.render self.content
- else
+ # if self.content_format == 'Markdown'
+ # require 'redcarpet'
+ # renderer = Redcarpet::Render::HTML.new
+ # extensions = {fenced_code_blocks: true}
+ # redcarpet = Redcarpet::Markdown.new(renderer, extensions)
+ # self.content_processed = redcarpet.render self.content
+ # else
template = Liquid::Template.parse(self.content)
self.content_processed = template.render()
- end
+ # end
end
def content_processed
if read_attribute(:content_processed).blank?
postprocess_content