Skip to content

Instantly share code, notes, and snippets.

@valakirka
Created August 7, 2012 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valakirka/3287274 to your computer and use it in GitHub Desktop.
Save valakirka/3287274 to your computer and use it in GitHub Desktop.
Sphinx scope
class Contents::Post < Contents::Base
has_many :content_members, :as => :memberable, :dependent => :destroy
has_many :members, :through => :content_members, :class_name => "Contents::Member"
belongs_to :kt_blog, :class_name => 'Kontent::KtBlog', :touch => true
has_many :blog_categories, :through => :kt_blog, :class_name => "Kontent::KtBlogCategory"
validates_presence_of :title
validates_presence_of :summary, :unless => Proc.new {|post| post.body.present?}
validates_presence_of :kt_blog_id
default_scope :order => 'posts.original_published_at DESC'
define_index_ci do
indexes summary
indexes body
indexes :title
indexes kt_blog.name
has kt_blog(:id), :as => :blog
has content.blog_categories(:id), :as => :categories
has content.sections(:id), :as => :sections
has content.tags(:id), :as => :tags
has content.published_at, :as => :events_section_date
has content.parent_content_relations(:id), :as => :parent_relations
has content.visits_count, :as => :visits_count
has content.rss_presence, :as => :rss_presence
has original_published_at, :as => :post_published_at
end
named_scope :archive, lambda {|blog| {
:select => "year(kt_contents.published_at) as year, "\
"month(kt_contents.published_at) as month, "\
"kt_contents.published_at as published_at",
:joins => [:content],
:order => "kt_contents.published_at ASC",
:conditions => ["posts.kt_blog_id = ? "\
"AND kt_contents.published_at IS NOT NULL", blog.id],
:group => ["year(kt_contents.published_at), month(kt_contents.published_at)"]
}}
named_scope :section, lambda {|section| {
:joins => [:content => :content_sections],
:conditions => {
:kt_contents => {
:kt_content_sections => {
:kt_section_id => section.id } } } } }
sphinx_scope :section do |section, page, per_page|
published = "*, if((published_at <> 0) AND (published_at <= NOW()), 1, 0) as published"
{
:sphinx_select => published,
:with => {:sections => [section.id], :published => 1},
:order => "published_at desc",
:match_mode => :extended,
:page => page,
:per_page => per_page
}
end
sphinx_scope :rss_presences do
{ :with => { :rss_presence => true } }
end
sphinx_scope :max_matches do |max|
{ :max_matches => max }
end
sphinx_scope :with_publication_date do |date|
{:with => {:published_at => date.beginning_of_day..date.end_of_day}}
end
sphinx_scope :by_blog do |kt_blog, page, per_page|
published = "*, if((published_at <> 0) AND (published_at <= NOW()), 1, 0) as published"
{
:sphinx_select => published,
:with => {:blog => [kt_blog.id], :published => 1},
:order => "published_at desc",
:match_mode => :extended,
:page => page,
:per_page => per_page
}
end
def self.latest_planta29(n = 8)
Kontent::KtContent.all(:limit => n,
:joins => "JOIN posts ON posts.kt_content_id = kt_contents.id JOIN kt_blogs on kt_blogs.id = posts.kt_blog_id",
:order => "kt_contents.published_at DESC",
:conditions => ["kt_blogs.normalized_name = 'planta29' AND kt_contents.published_at > 0 AND kt_contents.published_at <= ?", Time.now])
end
def self.latest(source, n = 8)
self.all(:limit => n, :order => 'posts.original_published_at DESC', :include => :content, :conditions => ["kt_source_id = ? AND kt_contents.published_at > 0 AND kt_contents.published_at <= ?", source.id, Time.now])
end
def next(section = nil)
section ||= self.kt_blog
@next ||= self.content.next(section).try(:contentable)
end
def previous(section = nil)
section ||= self.kt_blog
@previous ||= self.content.previous(section).try(:contentable)
end
def html_title
title
end
def html_description
summary.blank? ? body : summary
end
def related_image(uid = 'imagen-de-post')
Kontent::KtContentRelation.by_content(self.content).
by_uid(uid).first
end
def has_related_image?(uid = 'imagen-de-post')
!self.related_image(uid).nil?
end
def belongs_to?(blog_name)
blog = site.blogs.find_by_normalized_name(blog_name)
return if blog.nil?
(self.kt_blog_id == blog.id) || (self.content.detail_template && self.content.detail_template.include?(blog_name))
end
def members_list=(members_list)
self.members = []
members = members_list.map{|m| m.strip}
members.each do |member|
m = Contents::Member.find_by_id(member)
self.members << m
end
self.save!
end
def members_list
self.members.map(&:name).join(', ')
end
def cache_pages
self.kt_blog.rss
end
def cache_fragments
return [] unless self.sections
self.sections.inject([]) do |fragments, section|
fragments << [section.normalized_name, 'sidebar', CACHE_EXPIRATION_KEY]
[self.previous(section), self.next(section)].each do |post|
fragments << [section.normalized_name, 'paginator', post.cache_key] if post
end
fragments
end
end
end
class Contents::Post
sphinx_scope :by_blog do |kt_blog, page, per_page|
published = "*, if((published_at <> 0) AND (published_at <= NOW()), 1, 0) as published"
{
:sphinx_select => published,
:with => {:blog => [kt_blog.id], :published => 1},
:order => "published_at desc",
:match_mode => :extended,
:page => page,
:per_page => per_page
}
end
sphinx_scope :with_publication_date do |date|
{:with => {:published_at => date.beginning_of_day..date.end_of_day}}
end
end
Contents::Post.by_blog(blog, 1, 10).with_publication_date(Date.today)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment