class Spud::SpudPostModel < ActiveRecord::Base self.table_name = 'spud_posts' spud_searchable has_many :spud_post_categories_posts, :foreign_key => 'spud_post_id' has_many :categories, :class_name => 'SpudPostCategory', :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, :foreign_key => 'spud_post_id' has_many :pending_comments, ->{ where(:spam => [nil, false], :approved => false) }, :class_name => "SpudPostComment", :foreign_key => 'spud_post_id' has_many :visible_comments, ->{ where(:spam => [nil,false], :approved => true) }, :class_name => 'SpudPostComment', :foreign_key => 'spud_post_id' has_many :spam_comments, ->{ where(:spam => true) }, :class_name => "SpudPostComment", :foreign_key => 'spud_post_id' has_many :spud_permalinks, :as => :attachment has_many :spud_post_sites, :dependent => :destroy, :foreign_key => 'spud_post_id' scope :blog_posts, ->{ for_blog('blog') } scope :news_posts, ->{ for_blog('news') } scope :for_blog, ->(key){ where(:blog_key => key) } scope :visible, ->{ where('visible = true AND published_at <= ?', Time.now.utc) } scope :ordered, ->{ order('published_at desc') } scope :search, ->(term){ where('title LIKE ?', "%#{term}%") } scope :for_user, ->(user){ if Spud::Blog.query_for_user.present? where(Spud::Blog.query_for_user.call(user)) else where({}) end } validates_presence_of :title, :content, :published_at, :spud_user_id, :url_name validates_uniqueness_of :url_name before_validation :set_url_name after_save :set_spud_site_ids acts_as_tb_liquid_content 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.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.blog_posts.recent_posts(limit) end def self.recent_news_posts(limit=5) return self.news_posts.recent_posts(limit) end def self.from_archive(date_string) begin date = Date.strptime(date_string, "%Y-%b") return where(:published_at => date..date.end_of_month) rescue logger.debug 'fallback' return where('') end end def is_news ActiveSupport::Deprecation.warn ":is_news is deprecated. Please rely on the :blog_key column stead.", caller return self.read_attribute(:is_news) end #def self.posts_for_category_archive(category, ) # Returns an array of Date objects for months with public posts def self.months_with_public_posts # Select # Month(published_at) as published_month, # Year(published_at) as published_year # From spud_posts # Where visible = 1 # And published_at < '2012-01-30' # Group By published_month, published_year # Order By published_year desc, published_month desc records = SpudPost.select('Extract(Month from published_at) as published_month, Extract(Year from published_at) as published_year').where('visible = ? AND published_at < ?', true, DateTime.now).group('published_month, published_year').order('published_year desc, published_month desc') begin return records.collect{ |r| Date.new(r[:published_year].to_i, r[:published_month].to_i) } rescue Exception => e logger.fatal "Exception occurred while fetching post archive dates:\n #{e.to_s}" return [] 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 template = Liquid::Template.parse(self.content) self.content_processed = template.render() # end end def content_processed if read_attribute(:content_processed).blank? postprocess_content end read_attribute(:content_processed) end def content_processed=(content) write_attribute(:content_processed,content) end def display_date return published_at.strftime("%b %d, %Y") end def is_public? return (published_at < DateTime.now) && visible end def is_private? return !is_public? end def category_names return self.categories.collect{ |c| c.name }.join(', ') end # Spud site ids getter def spud_site_ids if @spud_site_ids.nil? @spud_site_ids = spud_post_sites.collect{ |site| site.spud_site_id } end return @spud_site_ids end # Spud site ids setter def spud_site_ids=(site_ids) if site_ids.is_a?(Array) @spud_site_ids = site_ids.collect{ |id| id.to_i } else raise 'Site ids must be an Array' end end private def set_url_name self.url_name = "#{self.published_at.strftime('%Y-%m-%d')}-#{self.title.parameterize}" end def set_spud_site_ids if Spud::Core.multisite_mode_enabled _spud_post_sites = [] self.spud_site_ids.each do |site_id| _spud_post_sites << SpudPostSite.new(:spud_post_id => id, :spud_site_id => site_id) end self.spud_post_sites = _spud_post_sites end end end