Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
177 lines (143 sloc) 5.08 KB
class Post < ActiveRecord::Base
include ActionView::Helpers::SanitizeHelper
extend ActionView::Helpers::SanitizeHelper::ClassMethods
#I forget why add below lines, but it we add the following lines.
#You will get trouble on your technolgoy_spec.rb
#Also get trouble when create the technology in controller.
#It drive me crazy, if life again, never use single table inhermitance.
#require_dependency 'post'
#require_dependency 'notification'
#require_dependency 'comment'
#require_dependency 'fts'
#require_dependency 'technology'
#require_dependency 'video'
validates_presence_of :user
validates_presence_of :blog,
:if => Proc.new{|s| s.type != Notification.name }
belongs_to :blog
belongs_to :user
has_many :comments, :class_name => "Comment", :foreign_key => "parent_id"
has_many :photos
has_attached_file :shoot, :styles => { :medium => "500>", :thumb => "100" }
before_create :set_def_published_on
after_save :save_tag, :broadcast, :update_blog_latest_update
before_save :format_tag, :clean_tag, :default_nil
#A time stamp on the blog, it would use to sort the blog to find who is just updated the blog befoe
#Only the author update the blog, it would be update the date, not the comments or any other things
def update_blog_latest_update
if not blog.nil? and not blog.user.nil? and not self.user.nil?
if self.user.name == self.blog.user.name
blog.latest_update = Time.now
blog.save
end
end
end
def format_tag
return if self.tag.nil?
self.tag = self.tag.gsub(" ",",").gsub(";",",").gsub("/",",").gsub(":",",").gsub("?",",").gsub("\\",",");
self.tag = self.tag.split(",").reject(&:empty?).uniq.join(",")
end
def default_nil
logger.debug "handling nil"
self.title = "" if title.nil?
self.content = "" if content.nil?
end
attr_reader :per_page
searchable do
text :title, :default_boost => 2
text :content, :stored => true do
strip_tags self.content
end
string :type
end
def set_def_published_on
self.published_on = Time.now
end
def clean_tag
logger.debug "cleaning tag"
if self.id.nil?
return
end
old_post = Post.find(self.id)
return if old_post.nil?
tags = Tag.where(:name => old_post.tag_list)
tags.each do |t|
t.untag_post(old_post)
end
logger.debug "cleaning tag done"
end
def save_tag
logger.debug "saving tag"
return if self.tag.nil?
tags = Tag.all
self.tag.split(",").each do |t|
t = t.strip
tag = Tag.find_or_create_by_name(t)
tag.tag_post(self)
end
logger.debug "saving tag done"
end
def broadcast
#Magic code here, we can migrate this code to user model follow!(blog) method
#Notification is also the sub class of the post. But it should not broadcast to any one. Besides, the notificaiton has no blog id/blog model. It would cause error if you find the relative blog to broadcast it.
logger.debug "broadcast"
return if self.type == Notification.name
p "Add to myself first"
self.user.add_newsfeeds(self)
p "after Add to myself first"
puts "before call add newsfeeds"
puts "followers id #{self.user.followers.collect(&:id)}"
puts "post id #{self.id}"
User.add_newsfeeds(self.user.followers.collect(&:id), self)
puts "after call add newsfeeds"
#self.blog.followers.each do |user|
# user.add_newsfeeds(self.id)
#end
logger.debug "broadcast done"
end
def self.viewed(post)
$redis.incr( post.redis_key('view') )
end
def viewed_count
count = $redis.get ( self.redis_key('view') )
return 0 if count.nil?
count.to_i
end
def redis_key(str)
"post:#{self.id}:#{str}"
end
def tag_list
tags = self.tag.nil?? Array.new : tag.split(",")
end
scope :only_posts_and_fts, where("type is NULL or type = ? ", Fts.name )
scope :only_posts_list, where("type is NULL or type = ? or type = ?", Fts.name , Video.name)
scope :only_tech_list, where("is_deleted is NULL and ( type = ? or type = ? ) ", Video.name, Technology.name)
scope :exclude_comments, where("is_deleted is NULL and (type is NULL or type != ? )", "Comment" )
scope :exclude_notification, where("type is NULL or type != ? ", "Notification" )
scope :only_technology, where("type = ? ", Technology.name )
scope :only_fts, where("type = ? ", Fts.name )
scope :only_video, where("type = ? ", Video.name )
scope :only_posts, where("type is NULL")
scope :exclude_deleted, where("is_deleted is NULL")
end
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# title :string(255)
# source_url :string(255)
# content :string(255)
# tag :string(255)
# blog_id :integer
# user_id :integer
# created_at :datetime
# updated_at :datetime
# published_on :datetime
# type :string(255)
# shoot_file_name :string(255)
# shoot_content_type :string(255)
# shoot_file_size :integer
# shoot_updated_at :datetime
# parent_id :integer
#
Something went wrong with that request. Please try again.